浙大JAVA实验题答案14answer.doc
实验14 类的设计和实验题库中未做的题1. 程序填空题,不要改变与输入输出有关的语句。30009 求一元二次方程的根(选做)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*xb*xc0的根,结果保留2位小数(如果答案为3.50,则直接显示为3.5)。按情况分别输出以下内容(文字中的标点为西文标点,无空格。括号内为说明):(1)a=b=c=0,meaningless(2)a=b=0,c!=0,error(3)x=值 (值为-c/b)(4)x1=值1 (值1为(-b+Math.sqrt(d)/(2*a)) x2=值2 (值2为(-b-Math.sqrt(d)/(2*a))(5)x1=值1+值2i x2=值1-值2i (值1为-b/(2*a),值2为Math.sqrt(-d)/(2*a))输入输出示例:括号内为说明输入:5 (repeat=5)0 0 0 (a=0,b=0,c=0)0 0 1 (a=0,b=0,c=1)0 2 5 (a=0,b=2,c=5)21 89 35 (a=21,b=89,c=35)2 2 3 (a=2,b=2,c=3)输出:a=b=c=0,meaninglessa=b=0,c!=0,errorx=-2.5x1=-0.44x2=-3.8x1=-0.5+1.12ix2=-0.5-1.12iimport java.util.Scanner;public class Test30009 public static void main(String args) int repeat, ri; int a,b,c,d; double x1,x2; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri = 1; ri <= repeat; ri+) a=in.nextInt();b=in.nextInt();c=in.nextInt(); /*-*/ if(a=0&&b=0&&c=0) System.out.println("a=b=c=0,meaningless"); else if(a=0&&b=0&&c!=0) System.out.println("a=b=0,c!=0,error"); else if(a=0&&b!=0) x1=-(double)c/b; System.out.println("x="+Math.round(x1*100)/100.0); else d=b*b-4*a*c; if(d>=0) x1=(-b+Math.sqrt(d)/(2*a); x2=(-b-Math.sqrt(d)/(2*a); System.out.println("x1="+Math.round(x1*100)/100.0); System.out.println("x2="+Math.round(x2*100)/100.0); else x1=-(double)b/(2*a); x2=Math.sqrt(-d)/(2*a); System.out.println("x1="+Math.round(x1*100)/100.0+"+"+Math.round(x2*100)/100.0+"i"); System.out.println("x2="+Math.round(x1*100)/100.0+"-"+Math.round(x2*100)/100.0+"i"); 说明:为防止出错,在编程前应考虑周全² 当a=b=c=0时,方程有无数解;² 当a=b=0,c!=0时,方程无解;² 当a=0,b!=0时,方程解为x=-c/b;² 当a!=0时,求:d=b*b-4*a*c若d>=0,有两个不同实根:x=(-b±Math.sqrt(d)/(2*a)若d<0,有两个不同复根:x=-b/(2*a)±Math.sqrt(-d)/(2*a)i或: if(a=0) if(b=0) if(c=0) System.out.println("a=b=c=0,meaningless"); else System.out.println("a=b=0,c!=0,error"); else x1=-(double)c/b; System.out.println("x="+Math.round(x1*100)/100.0); else d=b*b-4*a*c; if(d>=0) x1=(-b+Math.sqrt(d)/(2*a); x2=(-b-Math.sqrt(d)/(2*a); System.out.println("x1="+Math.round(x1*100)/100.0); System.out.println("x2="+Math.round(x2*100)/100.0); else x1=-(double)b/(2*a); x2=Math.sqrt(-d)/(2*a); System.out.println("x1="+Math.round(x1*100)/100.0+"+"+Math.round(x2*100)/100.0+"i"); System.out.println("x2="+Math.round(x1*100)/100.0+"-"+Math.round(x2*100)/100.0+"i"); 40033简单计算器 输入一个正整数repeat (0<repeat<10),做repeat次下列运算:程序模拟简单运算器的工作:输入一个算式,遇等号"="说明输入结束,输出结果。假设计算器只能进行加减乘除运算,运算数和结果都是整数,4种运算符的优先级相同,按从左到右的顺序计算。例:括号内是说明输入2 (repeat=2)15 + 2 / 3 = (数字与运算符之间有一空格)1 + 2 * 10 - 10 / 2 = (数字与运算符之间有一空格)输出510 import java.util.Scanner;public class T40033 public static void main(String args) int ri, repeat; int op1, op2, res; char operator; Scanner in=new Scanner(System.in); repeat=in.nextInt(); for(ri=1; ri<=repeat; ri+) op1=in.nextInt(); operator=(in.next().charAt(0); /先读入一个单词(字符串类型),再从中取出第0个字符,作为运算符 /*-*/res=op1;while(operator!='=') op2=in.nextInt();switch(operator) case '+': res+=op2;break;case '-': res-=op2;break;case '*': res*=op2;break;case '/': res/=op2;break;operator=(in.next().charAt(0); System.out.println(res); 以下题目练习类的设计:70011 设计一个Circle的子类圆柱体Cylinder输入一个圆柱体底圆的圆心坐标、圆半径、高度,分别计算并输出圆柱体的体积和表面积。要求在Circle类的后面定义一个它的子类Cylinder类(圆柱体),该类包含以下成员:成员变量h:高度,双精度浮点型构造方法成员方法calcArea():计算圆柱体表面积,返回双精度浮点数成员方法calcV():计算圆柱体体积,返回双精度浮点数成员方法getH():获得高度,返回双精度浮点数输入输出示例(括号内是说明)输入:2 3 4 5 (圆心坐标x=2,y=3;半径r=4;高度h=5)输出:r=4.0h=5.0V=251.33 (圆柱体体积)S=226.19 (圆柱体表面积)import java.util.*;public class Test70011 public static void main(String args) Scanner in=new Scanner(System.in);double x,y,r,h;x=in.nextDouble();y=in.nextDouble();r=in.nextDouble();h=in.nextDouble();Cylinder cy=new Cylinder(x,y,r,h);System.out.println("r="+cy.getR();System.out.println("h="+cy.getH();System.out.println("V="+Math.round(cy.calcV()*100)/100.);System.out.println("S="+Math.round(cy.calcArea()*100)/100.);class Circleprivate double x,y,r;public Circle(double x,double y,double r)this.x=x;this.y=y;this.r=r;public double perimeter() /计算圆周长return 2*Math.PI*r;public double area() /计算圆面积return Math.PI*r*r;public double getR()return r;/*-*/class Cylinder extends Circlepublic double h;public Cylinder(double x,double y,double r,double h)super(x,y,r);this.h=h;public double calcArea()return perimeter()*h+2*area();public double calcV()return area()*h;public double getH()return h;70012 编写重载方法,求二个数或三个数的最大值输入一个正整数 n (n>0),做 n 次下列运算:输入a、b、c三个整数,分别算出前2个数的最大值,和3个数的最大值。要求在main()方法所在类的下方,定义一个Compare类,该类中定义两个类成员方法max(a,b)和max(a,b,c),用来分别获得两个数的最大值、三个数的最大值。输入输出示例(括号内是说明):输入:2 (n=2)4 34 66 (第一批3个数)3 23 4 (第二批3个数)输出:max(a,b)=34 (第一批中前两个数的最大值)max(a,b,c)=66 (第一批中的最大值)max(a,b)=23 (第二批中前两个数的最大值)max(a,b,c)=23 (第二批中的最大值)import java.util.*;public class Test70012 public static void main(String args) int i,n,a,b,c; Scanner in=new Scanner(System.in); n=in.nextInt();for(i=1;i<=n;i+) a=in.nextInt();b=in.nextInt();c=in.nextInt();System.out.println("max(a,b)="+Compare.max(a,b);System.out.println("max(a,b,c)="+Compare.max(a,b,c); /*-*/class Comparepublic static int max(int a,int b)return Math.max(a,b);public static int max(int a,int b,int c) return Math.max(Math.max(a, b), c);