浙大JAVA 实验题答案14answer
浙大JAVA-实验题答案14answer
实验14 类的设计和实验题库中未做的题1.程序填空题,不要改变与输入输出有关的语句。
30009 求一元二次方程的根(选做)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*x+b*x+c=0的根,结果保留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+值2ix2=值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 23 (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");elseSystem.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次下列运算:程序模拟简单运算器的工作:输入一个算式,遇等号"="说明输入结束,输出结果。
浙大java练习题答案
40001import class Test40001 {public static void main(String[] args) { int ri, repeat;int i, n;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40002import class Test40002 {public static void main(String[] args) { int ri, repeat;int i, n;double fact;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40003import class Test40003 {int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();n=();/*--------------------*/}}}40004import class Test40004 {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40005import class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+(2*i-1));}}}}40006import class Test40006 {public static void main(String[] args) { int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){eps=();/*--------------------*/}}}40007import class Test40007 {public static void main(String[] args){ int ri, repeat;int begin, c, end, f;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){begin=();end=();"Celsius Fahrenheit");/*--------------------*/" "+f);}}}}40008import class Test40008 {public static void main(String[] args){ int ri, repeat;int x, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){x=();/*--------------------*/}}}}40009import class Test40009 {public static void main(String[] args){ int ri, repeat;int i , max, n, x;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}}40010import class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/"number="+number+", sum="+sum);}}}40011import class Test40011 {public static void main(String[] args) { int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40012import class Test40012{public static void main(String args[]){int i, n, a, sn, tn;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){a=();n=();/*--------------------*/}}}40013import class Test40013{public static void main(String args[]){ int ri, repeat;boolean flag=true;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();/*--------------------*/}if(flag) "YES");else"NO");}}}40014import .*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner;int gcd, lcm, m, n,r;int repeat, ri;repeat=();for(ri = 1; ri <= repeat; ri++){m=();n=();if(m <= 0 || n <= 0)"m <= 0 or n <= 0");else{/*---------*/"the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import class Test40021{public static void main(String args[]){ int ri, repeat;int i,n;float s,t;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){n=();/*--------------------*/}}}40022import class Test40022{public static void main(String args[]){ int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*------------------*/}}40023import class Test40023{public static void main(String args[]){int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){m=();n=();/*---------------------*/"count="+count+", sum="+sum);}}40031import class Test40031{public static void main(String []args ){ int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner;repeat=()).charAt(0)-'0'; harAt(0);/*---------*/}}}40034import class Test40034{public static void main(String []args){ int year,m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){m=();n=();/*---------*/}}}40035import class Test40035{public static void main(String []args){ int m,n,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();m=();/*---------*/}}}40036import class Test40036{public static void main(String []args){ int days,repeat,ri;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){}}}40037import class Test40037{public static void main(String []args){ int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner;n=();for(ri=1;ri<=n;ri++){/。
浙大JAVA实验题答案13answer
实验13字符串处理(二)和类的设计1.程序填空题,不要改变与输入输出有关的语句。
以下题目请采用字符串处理来实现:40010求整数的位数以及各位数之和输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个整数,输出它的位数以及各位数之和。
例:括号内是说明输入4 (repeat=4)123456 -100 -1 99输出number=6, sum=21 (123456的位数是6,各位数之和是21)number=3, sum=1 (-100的位数是3,各位数之和是1)number=1, sum=1 (-1的位数是1,各位数之和是1)number=2, sum=18 (99的位数是2,各位数之和是18)import java.util.Sca nner;public class Test40010 {public static void main( Stri ng[] args){int ri, repeat;int nu mber, sum,n;Scanner in=new Scann er(System.i n);repeat=in.n extI nt();for(ri=1; ri<=repeat; ri++){n=in.n extI nt();}}}50003统计一个整数中数字的个数输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入1个整数,统计并输出该数中2的个数。
要求定义并调用函数countdigit(number,digit) ,它的功能是统计整数number中数字digit的个数。
例如,countdigit(10090,0) 的返回值是3。
例:括号内是说明输入:3 (repeat=3)-219022345543输出:count=2 (-21902 中有2 个2)count=1 ( 有1 个2)count=0 (345543 中没有2)import java.util.Sca nner;public class Test50003{public static void main( Stri ngargs[]){ int ri, repeat;int count;long n;Scanner in=new Sca nn er(System.i n); repeat=in.n extI nt();for(ri=1; ri<=repeat; ri++){n=in.n extI nt();n=Math. abs (n);count= coun tdigit (n ,2);System.out.pri ntl n("cou nt="+co unt);50009将一个整数逆序输出输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个整数,将它逆序输出。
Java语言程序设计第14章习题参考答案.docx
习题十四参考答案1.什么是Web,其工作原理如何?答:Web是一种浏览器/服务器技术。
Web服务器分布在世界各地,存储着各种各样的信息。
Web的浏览器用来请求任何服务器上的信息,并负责显示这些信息。
Web工作原理:(1)用户在浏览器中输入要访问网页的URL地址(如:),向服务器发送浏览请求。
(2)Web服务器接收到请求后,把响应结果返回到浏览器,浏览器解析HTML,把内内容呈现给用户。
(3)通信完成,关闭连接。
2.JSP的工作原理是什么?答:JSP的工作过程如下:(1)当一个JSP页面第一次被请求或在上次调用后被修改过,服务器首先会把JSP页面转换成Servleto(2)在转换的过程中,若JSP页面存在语法错误,转换会被终止,并向服务器和客户端输出错误信息。
如果转换成功,转换后的Servlet会被编译成相应的类文件。
服务器加载该类文件并允许其实例完成需要的工作,把响应返回客户端浏览器。
(3)如果被请求的JSP文件在上次调用后没有被修改过,服务器直接调用已经转换编译过的类文件实例完成工作,把响应返回客户端浏览器。
(4)Sen-let被处理完毕以后,其生命周期结束,被Java虚拟机的垃圾回收器回收。
3.JSP的构成元素有哪些?答:一个JSP页面由模板元素和JSP元素构成。
模板元素指的是JSP引擎不处理的部分, 比如HTML. JavaScript和CSS等。
JSP元素指的是由JSP引擎直接处理的部分,这一部分必须符合JSP 语法,否则会导致编译错误。
JSP元素包括注释、指令元素、脚本元素和动作元素。
4.什么是内置对象? JSP的内置对象有哪些,各有何功能?答:所谓JSP内置对象就是町以不加声明就在JSP页面脚本中使用的成员变量。
卜面介绍常见的内置对象。
常用的内置对象有:(I)request对象。
该对象封装了用户提交的信息,通过调用该对象相应的方法可以获取封装的信息,即使用该对象可以获取用户提交信息。
浙大JAVA-实验题答案08answerdoc资料
浙大J A V A-实验题答案08a n s w e r实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句。
50001 求1 + 1/2! +....+ 1/n!输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明输入:2 (repeat=2)2 (n=2)10 (n=10)输出:1.51.7183import java.util.Scanner;public class Test50001 {public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();/*-----------*/s=0;for(i=1;i<=n;i++)s+=1.0/fact(i);System.out.println((long)(s*10000+0.5)/10000.);}}/*---------------*/static double fact(int n) {int i;double f=1;for(i=1;i<=n;i++)f*=i;return f;}}50002 求a+aa+aaa+aa…a输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
java实验期末考试题及答案
java实验期末考试题及答案一、选择题(每题2分,共20分)1. Java中,下面哪个关键字用于声明一个类?A. classB. interfaceC. structD. enum答案:A2. 下列哪个选项不是Java的基本数据类型?A. intB. floatC. doubleD. String答案:D3. 在Java中,哪个关键字用于捕获异常?A. tryB. catchC. throwD. finally答案:B4. Java中,哪个关键字用于定义一个方法?A. functionB. methodC. defD. void5. 在Java中,下列哪个关键字用于实现多态?A. extendsB. implementsC. interfaceD. abstract答案:A6. Java中,哪个关键字用于声明一个接口?A. classB. interfaceC. abstractD. final答案:B7. Java中,哪个关键字用于声明一个常量?A. finalB. constC. staticD. volatile答案:A8. 在Java中,哪个关键字用于声明一个同步方法?A. synchronizedB. volatileC. transientD. strictfp答案:A9. Java中,哪个关键字用于声明一个内部类?B. nestedC. staticD. private答案:A10. 在Java中,哪个关键字用于声明一个泛型?A. genericB. templateC. typeD. generic答案:D二、填空题(每题2分,共20分)1. Java程序的执行是由____开始的。
答案:main方法2. Java中,____关键字用于声明一个类的成员变量为常量。
答案:final3. Java中,____关键字用于声明一个类的方法不生成字节码。
答案:native4. 在Java中,____关键字用于声明一个类只能被同一个包中的其他类访问。
浙江大学java上机参考答案48页word文档
一、求两个数的和与差。
程序填空,不要改变与输入输出有关的语句。
输入整数a和b,计算并输出a、b的和与差。
import java.io.*;import java.util.Scanner;public class Test20001{public static void main(String args[]){int a, b, sum, diff;Scanner in=new Scanner(System.in);a=in.nextInt();b=in.nextInt();sum=a+b;diff=a-b;System.out.println("The sum is "+sum);System.out.println("The difference is "+diff);二、求平方根。
程序填空,不要改变与输入输出有关的语句。
输入1个实数x,计算并输出其平方根。
例:输入1.21输出The square root of 1.21 is 1.1import java.io.*;import java.util.Scanner;public class Test20002{public static void main(String args[]){double x, root;Scanner in=new Scanner(System.in);x=in.nextDouble();r oot=Math.sqrt(x);System.out.println("The square root of "+x+" is "+root);三、华氏温度转换为摄氏温度。
程序填空,不要改变与输入输出有关的语句。
输入华氏温度f,计算并输出相应的摄氏温度c。
c = 5/9(f-32).例:括号内是说明:输入17.2 (华氏温度)输出The temprature is -8.222222222222223 import java.util.Scanner;public class Test20003 {public static void main(String[] args) {Scanner in=new Scanner(System.in);double f, c;f=in.nextDouble();c=5.0/9*(f-32);System.out.println("The temprature is "+c);四、计算旅途时间。
浙大JAVA 实验题答案11answer
实验11 数组作为方法的参数1.程序填空题,不要改变与输入输出有关的语句。
60030 编写排序函数(方法)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入一个正整数n,再输入n个整数,将它们从小到大排序后输出。
要求将排序编写为一个sort()方法。
例:括号内是说明输入3 (repeat=3)4 5 1 7 63 1 2 35 5 4 3 2 1输出1 5 6 71 2 31 2 3 4 5import java.util.Scanner;public class Test60030{public static void main(String []args){int ri, repeat;int i, n, a[];Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();a=new int[n];for(i=0; i<n; i++)a[i]=in.nextInt();sort(a);for(i=0; i<n; i++)System.out.print(a[i]+" ");System.out.println();}}/*---------*///说明:数组作参数时,传递的是地址,形参数组和实参数组共用同一块内存,//方法sort()中对形参数组b排序,实际上就是对实参数组a排序,//所以不需要返回值。
static void sort(int b[]){int i,j,k,temp;for(i=0;i<b.length-1;i++) { //选择法递增排序k=i;for(j=i+1;j<b.length;j++)if(b[k]>b[j]) k=j;if(i!=k){temp=b[i];b[i]=b[k]; b[k]=temp;}}}}60031 判断两个矩阵是否相同(方法)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:读入 1 个正整数n(1≤n≤6), 再读入2个 n 阶方阵 a和b , 判断2个方阵是否相同。
java实验及答案
}
public abstract void disp();
}
2、package P14;
public class Student extends GeneralPeople {
private String school;
public Student() {
super();
(5)设置性别的公有方法
(6)读取性别的公有方法public String getSex( )
如果为true,返回male,否则返回female
(7)设置学校的公有方法
(8)读取学校的公有方法
(9)实现方法disp(),将学生的姓名、性别、所在学校输出到屏幕
3、根据学生类Student派生出大学生类CollegeStudent,要求如下:
return "male";
else
return "female";
}
public void setAge(int a)
{
age=a;
}
public int getAge()
{
return age;
}
public void disp()
{
System.out.println("Name:"+getName()+" Sex:"+getSex()+" Age:"+getAge());
(2)构造方法People (String n, boolean s, int a)
(3)设置姓名的公有方法
(4)读取姓名的公有方法
(5)设置性别的公有方法
浙江大学Java语言程序设计实验答案全集
} }
System.out.println(" *");
-2-
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电,通力根1保过据护管生高线产中敷工资设艺料技高试术中卷0资不配料仅置试可技卷以术要解是求决指,吊机对顶组电层在气配进设置行备不继进规电行范保空高护载中高与资中带料资负试料荷卷试下问卷高题总中2体2资,配料而置试且时卷可,调保需控障要试各在验类最;管大对路限设习度备题内进到来行位确调。保整在机使管组其路高在敷中正设资常过料工程试况1卷中下安,与全要过,加度并强工且看作尽护下可1都关能可于地以管缩正路小常高故工中障作资高;料中对试资于卷料继连试电接卷保管破护口坏进处范行理围整高,核中或对资者定料对值试某,卷些审弯异核扁常与度高校固中对定资图盒料纸位试,置卷编.工保写况护复进层杂行防设自腐备动跨与处接装理地置,线高尤弯中其曲资要半料避径试免标卷错高调误等试高,方中要案资求,料技编试术写5、卷交重电保底要气护。设设装管备备置线4高、调动敷中电试作设资气高,技料课中并3术试、件资且中卷管中料拒包试路调试绝含验敷试卷动线方设技作槽案技术,、以术来管及避架系免等统不多启必项动要方高式案中,;资为对料解整试决套卷高启突中动然语过停文程机电中。气高因课中此件资,中料电管试力壁卷高薄电中、气资接设料口备试不进卷严行保等调护问试装题工置,作调合并试理且技利进术用行,管过要线关求敷运电设行力技高保术中护。资装线料置缆试做敷卷到设技准原术确则指灵:导活在。。分对对线于于盒调差处试动,过保当程护不中装同高置电中高压资中回料资路试料交卷试叉技卷时术调,问试应题技采,术用作是金为指属调发隔试电板人机进员一行,变隔需压开要器处在组理事在;前发同掌生一握内线图部槽 纸故内资障,料时强、,电设需回备要路制进须造行同厂外时家部切出电断具源习高高题中中电资资源料料,试试线卷卷缆试切敷验除设报从完告而毕与采,相用要关高进技中行术资检资料查料试和,卷检并主测且要处了保理解护。现装场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
浙大JAVA实验习题答案08answer
浙大JAVA实验习题答案08answer实验8 Method的使用1.程序填空题,不要改变与输入输出有关的语句。
50001输入一个正整数repeat (0<repeat<="" 的前n项的和(保留=""></repeats = 1 + 1/2! +....+ 1/n!要求定义并调用函数fact(n)计算n的阶乘。
例:括号内是说明int i;double f=1;for(i=1;i<=n;i++)f*=i;return f;}}50002输入一个正整数repeat (0<repeat<=""></repeat要求定义并调用函数fn(a,n),它的功能是返回aa…a(n个a)。
例如,fn(3,2)的返回值是33。
例:括号内是说明输入2 (repeat=2)2 3 (a=2, n=3)8 5 (a=8, n=5)输出246 (2+22+222)98760 (8+88+888+8888+88888)imponner;public class Test50002{}}读入1 个整数,统计并输出该数中2的个数。
要求定义并调用函数countdigit(number,digit),它的功能是统计整数number中数字digit 的个数。
例如,countdigit(10090,0)的返回值是3。
例:括号内是说明输入:3 (repeat=3)-219022345543输出:count=2 (-21902中有2个2)count=1 (有1个2)count=0 (345543中没有2)public class Test50003{public static void main(String args[]){int ri, repeat;int count;long n;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){}和自身输入4 (repeat=4)1 2 9 17输出NO (1不是素数)YES (2是素数)NO (9不是素数)YES (17是素数)public class Test50004{public static void main(String args[]){int ri, repeat,n;boolean flag;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*---------*/flag=prime(n);if(flag)}}}import java.util.Scanner;public class Test50005{public static void main(String args[]){ int ri, repeat; int count, i, m, n, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------*/count=0;sum=0;for(i=m;i<=n;i++)if(prime(i)){count++; sum+=i;} }}/*------------*/static boolean prime(int m){ boolean flag=true;if(m==1)flag=false;}是int i, m, n;long f;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------*/i=1;f=1;while(f<=n){if(f>=m) System.out.print(f+" ");i++;f=fib(i);}}}/*------------*/sta ti c long fib(int n){ //返回第n项Fibonacci数int i;}public static void main(String args[]){int ri,repeat;int i, m, n;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/for(i=m;i<=n;i++)if(i==factorsum(i))System.out.print(i+" ");}}/*---------*/static int factorsum(int number){ //返回number的因子和int sum=0;if(number==1)sum=1;for(int i=1;i<=number-1;i++)if(number%i==0)sum+=i;}1/*---------*/for(i=m;i<=n;i++)if(is(i))System.out.print(i+" ");}}/*---------*///判断number的各位数字之立方和是否等于它本身static boolean is(int number){int sum=0,n,digit;n=number;while(n>0){digit=n%10;n=n/10;sum+=digit*digit*digit;}if(number==sum)return true;else return false;}}50009-1-299}/*---------*/static long reverse(long number){//返回number的逆序数int flag=1;long a=0,digit;if(number<0){flag=-1;number=-number;}while(number>0){digit=number%10; //分离出个位数字a=a*10+digit; //形成当前的逆序数number=number/10;}return flag*a;}}50011输入/*-----------------*/maximun=maximun(a,b,c);Sy}}/*-------------------*/static int maximun(int a,int b,int c){ int max=a; if(max< bdsfid="297" p=""><>if(max< bdsfid="299" p=""><>return max; }}。
浙大java语言程序设计编程答案
实验9-1 Method 的使用(二)1. 将一个整数逆序输出输入一个正整数repeat (0<repeat<10) ,做repeat 次下列运算:输入一个整数,将它逆序输出。
要求定义并调用函数reverse(number) ,它的功能是返回number 的逆序数。
例如reverse(12345) 的返回值是54321。
例:括号内是说明输入4 (repeat=4)123456 -100 -2 99 输出654321-1-299import class Test50009{public static void main(String args[]){ int ri,repeat; long n, res;Scanner in=new Scanner; repeat=(); for(ri=1;ri<=repeat;ri++){ n=();res=reverse(n);}}static long reverse(long number){int flag=1;long a=0,digit; if(number<0){ flag=-1; number=-number;} while(number>0){ digit=number%10; // 分离出个位数字a=a*10+digit; // 形成当前的逆序数number=number/10;}return flag*a;2. 十进制转换二进制输入一个正整数repeat (0<repeat<10) ,做repeat 次下列运算:输入1个正整数n,将其转换为二进制后输出。
要求定义并调用函数dectobin(n) ,它的功能是输出n 的二进制。
例如,调用dectobin(10) ,输出1010。
输出语句://t 为某位二进制数例:括号内是说明输入:3 (repeat=3)15100输出:11111100100import class Test50010{public static void main(String args[]){ int ri,repeat;int i,n;Scanner in=new Scanner; repeat=(); for(ri=1;ri<=repeat;ri++){ n=();dectobin(n);}}static void dectobin(int n){String t=""; // 保存二进制数do {t=n%2+t; //n 除2 后的余数拼接到t 的前面n=n/2; // 获得除2 后的商}while(n>0);// 本方法无返回值,需要在方法体中输出结果说明:本题中方法dectobin(n) 的输出虽然与要求有所出入,但上传是正确的3. 用函数求三个数的最大值输入一个正整数repeat (0<repeat<10) ,做repeat 次下列运算:输入三个整数a、b和c,输出其中较大的数。
java语言知识学习程序设计基本篇第十版第十四章理解练习答案解析
01import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_01 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);ImageView imageView1 = new ImageView("image/uk.gif");ImageView imageView2 = new ImageView("image/ca.gif");ImageView imageView3 = new ImageView("image/china.gif");ImageView imageView4 = new ImageView("image/us.gif");pane.add(imageView1, 0, 0);pane.add(imageView2, 1, 0);pane.add(imageView3, 0, 1);pane.add(imageView4, 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_01"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}02import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.GridPane;import javafx.stage.Stage;import javafx.scene.image.ImageView;import javafx.scene.image.Image;public class Exercise14_02 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Image imageX = new Image("image/x.gif");Image imageO = new Image("image/o.gif");GridPane pane = new GridPane();pane.setAlignment(Pos.CENTER);pane.setHgap(5);pane.setVgap(5);for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {int status = (int)(Math.random() * 3);if (status == 0) {pane.add(new ImageView(imageX), j, i);}else if (status == 1) {pane.add(new ImageView(imageO), j, i);}}}// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_02"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}03import java.util.ArrayList;import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.HBox;import javafx.stage.Stage;import javafx.scene.image.ImageView;public class Exercise14_03 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {// There are two ways for shuffling. One is to use the hint in the book.// The other way is to use the static shuffle method in the java.util.Collections class.ArrayList<Integer> list = new ArrayList<>();for (int i = 1; i <= 52; i++) {list.add(i);}java.util.Collections.shuffle(list);HBox pane = new HBox(5);pane.setAlignment(Pos.CENTER);pane.getChildren().add(new ImageView("image/card/" + list.get(0) + ".png"));pane.getChildren().add(new ImageView("image/card/" + list.get(1) + ".png"));pane.getChildren().add(new ImageView("image/card/" + list.get(2) + ".png"));// Create a scene and place it in the stageScene scene = new Scene(pane);primaryStage.setTitle("Exercise14_03"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}04import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import yout.HBox;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.T ext;import javafx.stage.Stage;public class Exercise14_04 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {HBox pane = new HBox();pane.setAlignment(Pos.CENTER);Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22);for (int i = 0; i < 5; i++) {Text txt = new Text("Java");txt.setRotate(90);txt.setFont(font);txt.setFill(new Color(Math.random(), Math.random(), Math.random(), Math.random()));pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 100);primaryStage.setTitle("Exercise14_04"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}05import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_05 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length();Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}05import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.text.Font;import javafx.scene.text.FontPosture;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_05 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);String s = "WELCOME TO JAVA ";double radius = 80;for (int i = 0; i < s.length(); i++) {double alpha = 2 * Math.PI * (s.length() - i) / s.length();Text txt = new Text(radius * Math.cos(alpha) + 120,120 - radius * Math.sin(alpha), s.charAt(i) + "");txt.setFont(font);txt.setRotate(360 * i / s.length() + 90);pane.getChildren().add(txt);}// Create a scene and place it in the stageScene scene = new Scene(pane, 240, 240);primaryStage.setTitle("Exercise14_05"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line. */public static void main(String[] args) {launch(args);}}06import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.scene.shape.Rectangle;public class Exercise14_06 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;Pane pane = new Pane();for (int i = 0; i < 8; i++) {boolean isWhite = i % 2 == 0;for (int j = 0; j < 8; j++) {Rectangle rectangle = new Rectangle(i * WIDTH / 8,j * HEIGHT / 8, WIDTH / 8, HEIGHT / 8);rectangle.setStroke(Color.BLACK);if (isWhite) {rectangle.setFill(Color.WHITE);}else {rectangle.setFill(Color.BLACK);}isWhite = !isWhite;pane.getChildren().add(rectangle);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_06"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}07import javafx.application.Application;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.T extField;import yout.GridPane;import javafx.stage.Stage;public class Exercise14_07 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {double WIDTH = 200;double HEIGHT = 200;GridPane pane = new GridPane();for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {TextField tf = new TextField((int)(Math.random() + 0.5) + "");tf.setPrefColumnCount(1);tf.setAlignment(Pos.CENTER);pane.add(tf, j, i);}}// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_07"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}08import javafx.application.Application;import javafx.scene.Scene;import javafx.scene.image.ImageView;import yout.GridPane;import javafx.stage.Stage;public class Exercise14_08 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();for (int i = 0; i < 6; i++) {for (int j = 0; j < 9; j++) {pane.add(new ImageView("image/card/" + (i * 6 + j + 1) + ".png"), j, i);}}// Create a scene and place it in the stageScene scene = new Scene(pane, 600, 600);primaryStage.setTitle("Exercise14_08"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}09import javafx.application.Application;import javafx.scene.Scene;import yout.GridPane;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.ArcType;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class Exercise14_09 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {GridPane pane = new GridPane();pane.add(new FanPane(), 0, 0);pane.add(new FanPane(), 1, 0);pane.add(new FanPane(), 0, 1);pane.add(new FanPane(), 1, 1);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_09"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}class FanPane extends Pane {double radius = 50;public FanPane() {Circle circle = new Circle(60, 60, radius);circle.setStroke(Color.BLACK);circle.setFill(Color.WHITE);getChildren().add(circle);Arc arc1 = new Arc(60, 60, 40, 40, 30, 35);arc1.setFill(Color.RED); // Set fill colorarc1.setType(ArcType.ROUND);Arc arc2 = new Arc(60, 60, 40, 40, 30 + 90, 35);arc2.setFill(Color.RED); // Set fill colorarc2.setType(ArcType.ROUND);Arc arc3 = new Arc(60, 60, 40, 40, 30 + 180, 35);arc3.setFill(Color.RED); // Set fill colorarc3.setType(ArcType.ROUND);Arc arc4 = new Arc(60, 60, 40, 40, 30 + 270, 35);arc4.setFill(Color.RED); // Set fill colorarc4.setType(ArcType.ROUND);getChildren().addAll(arc1, arc2, arc3, arc4);}}10import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Ellipse;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_10 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Ellipse ellipse = new Ellipse(100, 40, 50, 20);ellipse.setFill(Color.WHITE);ellipse.setStroke(Color.BLACK);Arc arc1 = new Arc(100, 140, 50, 20, 0, 180);arc1.setFill(Color.WHITE);arc1.setStroke(Color.BLACK);arc1.getStrokeDashArray().addAll(6.0, 21.0);Arc arc2 = new Arc(100, 140, 50, 20, 180, 180);arc2.setFill(Color.WHITE);arc2.setStroke(Color.BLACK);pane.getChildren().addAll(ellipse, arc1, arc2,new Line(50, 40, 50, 140), new Line(150, 40, 150, 140));// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_10"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}11import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Circle;import javafx.scene.shape.Ellipse;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_11 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Circle circle = new Circle(100, 100, 80);circle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);Circle circle1 = new Circle(70, 70, 10);Circle circle2 = new Circle(130, 70, 10);Ellipse ellipse1 = new Ellipse(70, 70, 20, 15); ellipse1.setFill(Color.WHITE);ellipse1.setStroke(Color.BLACK);Ellipse ellipse2 = new Ellipse(130, 70, 20, 15); ellipse2.setFill(Color.WHITE);ellipse2.setStroke(Color.BLACK);Line line1 = new Line(100, 80, 80, 120);Line line2 = new Line(80, 120, 120, 120);Line line3 = new Line(120, 120, 100, 80);Arc arc = new Arc(100, 130, 40, 15, 180, 180); arc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);pane.getChildren().addAll(circle, ellipse1, ellipse2, circle1, circle2, line1, line2, line3, arc);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_11"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}12import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_12 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();double height = 300;double paneHeight = 150;Rectangle r1 = new Rectangle(10, paneHeight - height * 0.2, 100, height * 0.2);r1.setFill(Color.RED);Text text1 = new Text(10, paneHeight - height * 0.2 - 10, "Project -- 20%");Rectangle r2 = new Rectangle(10 + 110, paneHeight - height * 0.1, 100, height * 0.1);r2.setFill(Color.BLUE);Text text2 = new Text(10 + 110, paneHeight - height * 0.1 - 10, "Quiz -- 10%");Rectangle r3 = new Rectangle(10 + 220, paneHeight - height * 0.3, 100, height * 0.3);r3.setFill(Color.GREEN);Text text3 = new Text(10 + 220, paneHeight - height * 0.3 - 10, "Midterm --30%");Rectangle r4 = new Rectangle(10 + 330, paneHeight - height * 0.4, 100, height * 0.4);r4.setFill(Color.ORANGE);Text text4 = new Text(10 + 330, paneHeight - height * 0.4 - 10, "Final -- 40%");pane.getChildren().addAll(r1, text1, r2, text2, r3, text3, r4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 500, paneHeight);primaryStage.setTitle("Exercise14_12"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}13import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.ArcType;import javafx.scene.text.Text;import javafx.stage.Stage;public class Exercise14_13 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Arc arc1 = new Arc(100, 100, 80, 80, 0, 360 * 0.2);arc1.setFill(Color.RED);arc1.setType(ArcType.ROUND);Text text1 = new Text(110, 80, "Project -- 20%");Arc arc2 = new Arc(100, 100, 80, 80, 360 * 0.2, 360 * 0.1);arc2.setFill(Color.BLUE);arc2.setType(ArcType.ROUND);Text text2 = new Text(80, 15, "Quiz -- 10%");Arc arc3 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1, 360 * 0.3);arc3.setFill(Color.GREEN);arc3.setType(ArcType.ROUND);Text text3 = new Text(5, 100, "Midterm -- 30%");Arc arc4 = new Arc(100, 100, 80, 80, 360 * 0.2 + 360 * 0.1 + 360 * 0.3, 360 * 0.4); arc4.setFill(Color.ORANGE);arc4.setType(ArcType.ROUND);Text text4 = new Text(100, 180, "Final -- 40%");pane.getChildren().addAll(arc1, text1, arc2, text2, arc3, text3, arc4, text4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_13"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}14import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Line;import javafx.scene.shape.Rectangle;import javafx.stage.Stage;public class Exercise14_14 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();double paneWidth = 200;double paneHeight = 200;double width = paneWidth * 0.90 - 30;double height = paneWidth * 0.90 - 60;double diff = Math.min(width, height) * 0.4;// Draw the front rectangleRectangle r1 = new Rectangle(10, 60, width, height);r1.setFill(new Color(1, 1, 1, 0));r1.setStroke(Color.BLACK);// Draw the back rectangleRectangle r2 = new Rectangle(30, 60 - diff, width, height);r2.setFill(new Color(1, 1, 1, 0));r2.setStroke(Color.BLACK);// Connect the cornersLine line1 = new Line(10, 60, 30, 60 - diff);Line line2 = new Line(10, 60 + height, 30, 60 - diff + height);Line line3 = new Line(10 + width, 60, 30 + width, 60 - diff);Line line4 = new Line(10 + width, 60 + height, 30 + width, 60 - diff + height);pane.getChildren().addAll(r1, r2, line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, paneWidth, paneHeight);primaryStage.setTitle("Exercise14_14"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}15import javafx.application.Application;import javafx.collections.ObservableList;import javafx.scene.Scene;import yout.StackPane;import javafx.scene.paint.Color;import javafx.stage.Stage;import javafx.scene.shape.Polygon;import javafx.scene.text.Font;import javafx.scene.text.Text;public class Exercise14_15 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {// Create a pane, a polygon, and place polygon to paneStackPane pane = new StackPane();Polygon polygon = new Polygon();polygon.setFill(Color.RED);ObservableList<Double> list = polygon.getPoints();final double WIDTH = 200, HEIGHT = 200;double centerX = WIDTH / 2, centerY = HEIGHT / 2;double radius = Math.min(WIDTH, HEIGHT) * 0.4;// Add points to the polygon listfor (int i = 0; i < 8; i++) {list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8 - Math.PI / 8));list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8 - Math.PI / 8)); }Text text = new Text("STOP");text.setFill(Color.WHITE);text.setFont(Font.font("Times New Roman", 40));pane.getChildren().addAll(polygon, text);// Create a scene and place it in the stageScene scene = new Scene(pane, WIDTH, HEIGHT);primaryStage.setTitle("Exercise14_15"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}16import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Line;import javafx.stage.Stage;public class Exercise14_16 extends Application {@Override // Override the start method in the Application class public void start(Stage primaryStage) {Pane pane = new Pane();Line line1 = new Line(0, 0, 0, 0);line1.startYProperty().bind(pane.heightProperty().divide(3));line1.endXProperty().bind(pane.widthProperty());line1.endYProperty().bind(pane.heightProperty().divide(3));line1.setStroke(Color.BLUE);Line line2 = new Line(0, 0, 0, 0);line2.startYProperty().bind(pane.heightProperty().multiply(2).divide(3)); line2.endXProperty().bind(pane.widthProperty());line2.endYProperty().bind(pane.heightProperty().multiply(2).divide(3)); line2.setStroke(Color.BLUE);Line line3 = new Line(0, 0, 0, 0);line3.startXProperty().bind(pane.widthProperty().divide(3));line3.endXProperty().bind(pane.widthProperty().divide(3));line3.endYProperty().bind(pane.heightProperty());line3.setStroke(Color.RED);Line line4 = new Line(0, 0, 0, 0);line4.startXProperty().bind(pane.widthProperty().multiply(2).divide(3)); line4.endXProperty().bind(pane.widthProperty().multiply(2).divide(3)); line4.endYProperty().bind(pane.heightProperty());line4.setStroke(Color.RED);pane.getChildren().addAll(line1, line2, line3, line4);// Create a scene and place it in the stageScene scene = new Scene(pane, 200, 200);primaryStage.setTitle("Exercise14_16"); // Set the stage titleprimaryStage.setScene(scene); // Place the scene in the stageprimaryStage.show(); // Display the stage}/*** The main method is only needed for the IDE with limited* JavaFX support. Not needed for running from the command line.*/public static void main(String[] args) {launch(args);}}17import javafx.application.Application;import javafx.scene.Scene;import yout.Pane;import javafx.scene.paint.Color;import javafx.scene.shape.Arc;import javafx.scene.shape.Line;import javafx.scene.shape.Circle;import javafx.stage.Stage;public class Exercise14_17 extends Application {@Override // Override the start method in the Application classpublic void start(Stage primaryStage) {Pane pane = new Pane();Arc arc = new Arc(60, 240, 40, 20, 0, 180); // Draw the basearc.setFill(Color.WHITE);arc.setStroke(Color.BLACK);Line line1 = new Line(20 + 40, 220, 20 + 40, 20); // Draw the poleLine line2 = new Line(20 + 40, 20, 20 + 40 + 100, 20); // Draw the hangerLine line3 = new Line(20 + 40 + 100, 20, 20 + 40 + 100, 40); // Draw the hanger// Draw the circleint radius = 20;Circle circle = new Circle(20 + 40 + 100, 40 + radius, radius); // Draw the hanger circle.setFill(Color.WHITE);circle.setStroke(Color.BLACK);// Draw the left armLine line4 = new Line(20 + 40 + 100 - radius * Math.cos(Math.toRadians(45)),40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 - 60, 40 + radius + 60);// Draw the right armLine line5 = new Line(20 + 40 + 100 + radius * Math.cos(Math.toRadians(45)),40 + radius + radius * Math.sin(Math.toRadians(45)),20 + 40 + 100 + 60, 40 + radius + 60);// Draw the bodyLine line6 = new Line(20 + 40 + 100, 40 + 2 * radius,20 + 40 + 100, 40 + radius + 80);// Draw the left legLine line7 = new Line(20 + 40 + 100, 40 + radius + 80, 20 + 40 + 100 - 40, 40 + radius + 80 + 40);。
浙江大学java上机参考答案
import java.util.Scanner; public class Test20005 {
public static void main(String[] args) { Scanner in=new Scanner(System.in); int number, digit1, digit2, digit3, digit4, newnum; number=in.nextInt(); /*------------------*/
c=5.0/9*(f-32);
System.out.println("The temprature is "+c); }
} 四、计算旅途时间。程序填空,不要改变与输入输出有关的语句。
输入 2 个整数 time1 和 time2 ,表示火车的出发时间和到达时间, 计算并输出旅途时间。 有效的时间范围是 0000 到 2359 ,不需要考虑出发时间晚于到达时间的情况。 例:括号内是说明 :输入 712 1411 (出发时间是 7 :12 ,到达时间是 14 :11 )
/*------------------*/
/*计算两个时间之间的小时数和分钟数 */ hours=time2/100-time1/100; mins=time2%100-time1%100;
/*当计算得到的分钟数为负数时进行如下处理 */ hours=mins>0?hours:hours-1; mins=mins>0?mins:mins+60;
import java.util.Scanner; public class Test20003 {
public static void main(String[] args) { Scanner in=new Scanner(System.in);
JAVA 综合实验及练习(参考答案)
参考:这一题考虑使用组合布局,将上面控件放置在 JPanel 上,再将 JPanel 放到内容 面板的中间。下面四个按钮先放在某 JPanel 再放置在内容面板的 South 部分
9、以 JDBC 技术创建一个通讯录应用程序,要求通讯录中必须含有编号、姓名,性别、电
话、地址、Email 等等。实现该类并包含添加、删除、修改、按姓名查等几个方法。编写主 程序测试。 参考如下: 第一步:编写一个 Person 联系人类 public class Person { private int pid; //编号 // 姓名 // 性别 // 电话 //email private String name; private String sex; private String tel; private String email; /*构造函数以及 set-get 函数*/ } 第二步:编写一个 PersonDao 封装对联系人类的有关操作 public class PersonDao { // 添加 联系人 public void addPerson(Person p){ String sql=””; DBHelper. executeUpdate(sql); }
14、关于 length()这个方法的表述正确的是哪个? ( ) A.数组有 length()这个方法,string 也有 length()这个方法。 B.数组有 length()有这个方法,string 没有 length()这个方法。 C.数组没有 length()这个方法,string 有 length()这个方法。 D.数组没有 length()这个方法,string 也没有 length()这个方法。 15、选择排序的思想是,将数据序列划分为两个子列,一个子列是排好序的,另 一个是尚未排序的。 现若想将数据序列由小到大排序,则每次放到有序子列尾部 位置的元素,应从无序序列中选择( )。 A)最大的 B)最小的 C)任意的 D)头上的
浙江大学Java语言程序设计实验答案全集
Java答案全集实验汇总。
实验2 数据类型和变量的使用一、程序填空,在屏幕上显示一个短句“Programming in Java is fun!”import java.io.*;public class Test10001{public static void main(String args[]){/*------------------------*/}}二、程序填空,在屏幕上显示如下网格。
+---+---+| | || | |+---+---+import java.io.*;public class Test10002{public static void main(String args[]){/*------------------------*/}}三、编写程序,在屏幕上显示如下图案。
(要求:第1行行首无空格,每行行尾无空格)* * * ** * ** **public class Test10003{public static void main(String args[]){/*------------------------*/}}实验3 运算符和表达式的使用1、运行结果:m=2 k=1x=1.0 y=2.0 z=-3.0ch1=-A ch2=Ach1=-A ch2=aHello,Welcome to core Java!思考题:(1)字符'A'的Unicode码比字符'a'的Unicode码小32。
(2)假设字符型变量ch中保存一个大写字母,执行ch+=('a'-'A' );后,ch中是相应的小写字母。
例:若ch='B',执行后ch='b'。
2、运行结果:m=3 n=2. m大于n吗?truem=2 n=2. m大于n吗?falsestr1=Hello;str2=Hello!s1和s2相等吗?false思考题:(1)s2比s1多一个字符“!”,所以不相同。
浙大java练习题答案精编WORD版
浙大j a v a练习题答案精编W O R D版IBM system office room 【A0816H-A0912AAAHH-GX8Q8-GNTHHJ8】40001import java.util.Scanner;public class Test40001 {public static void main(String[] args) {int ri, repeat;int i, n;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(sum*1000+0.5)/1000.);}}}40002import java.util.Scanner;public class Test40002 {public static void main(String[] args) {int ri, repeat;int i, n;double fact;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println(fact);}}}40003import java.util.Scanner;public class Test40003 {public static void main(String[] args) {int ri, repeat;int i, n;double x, mypow;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextDouble();n=in.nextInt();/*--------------------*/System.out.println(mypow);}}}40004import java.util.Scanner;public class Test40004 {public static void main(String[] args) {int ri, repeat;int i, n, flag;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((long)(sum*10000+0.5)/10000.);}}}40005import java.util.Scanner;public class Test40005 {public static void main(String[] args) {int ri, repeat;int i, n, temp;float sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/sum=0;for(i=1;i<=n;i++){sum=(float) (sum+1.0/(2*i-1));}System.out.println(sum);}}}40006import java.util.Scanner;public class Test40006 {public static void main(String[] args) {int ri, repeat;int temp, flag;double eps, item, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){eps=in.nextDouble();/*--------------------*/System.out.println((int)(sum*10000+0.5)/10000.);}}}40007import java.util.Scanner;public class Test40007 {public static void main(String[] args){int ri, repeat;int begin, c, end, f;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){begin=in.nextInt();end=in.nextInt();System.out.println("Celsius Fahrenheit");/*--------------------*/System.out.println(c+" "+f);}}}}40008import java.util.Scanner;public class Test40008 {public static void main(String[] args){int ri, repeat;int x, sum;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){x=in.nextInt();/*--------------------*/}System.out.println(sum);}}}40009import java.util.Scanner;public class Test40009 {public static void main(String[] args){int ri, repeat;int i , max, n, x;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/}System.out.println(max);}}40010import java.util.Scanner;public class Test40010 {public static void main(String[] args){int ri, repeat;int number, sum,n,r;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println("number="+number+", sum="+sum); }}40011import java.util.Scanner;public class Test40011 {public static void main(String[] args) {int ri, repeat;int i,n;float a,b,s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+.5)/10000.); }}40012import java.util.Scanner;public class Test40012{public static void main(String args[]){int ri, repeat;int i, n, a, sn, tn;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){a=in.nextInt();n=in.nextInt();/*--------------------*/System.out.println(sn);}}40013import java.util.Scanner;public class Test40013{public static void main(String args[]){int ri, repeat;int i, m, n;boolean flag=true;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();/*--------------------*/if(flag) System.out.println("YES");else System.out.println("NO");}}}40014import java.util.*;public class Test40014 {public static void main(String []args){Scanner in =new Scanner(System.in);int gcd, lcm, m, n,r;int repeat, ri;repeat=in.nextInt();for(ri = 1; ri <= repeat; ri++){m=in.nextInt();n=in.nextInt();if(m <= 0 || n <= 0)System.out.println("m <= 0 or n <= 0");else{/*---------*/System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd);}}}}40021import java.util.Scanner;public class Test40021{public static void main(String args[]){int ri, repeat;int i,n;float s,t;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextInt();/*--------------------*/System.out.println((int)(s*10000+0.5)/10000.); }}}40022import java.util.Scanner;public class Test40022{int ri, repeat;int i, digit, m, n, number, sum;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*------------------*/}}}40023import java.util.Scanner;public class Test40023{int ri, repeat;int count, i, j, k, m, n, sum;Scanner in=new Scanner(System.in);repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){m=in.nextInt();n=in.nextInt();/*---------------------*/System.out.println("count="+count+", sum="+sum); }}}40031import java.util.Scanner;public class Test40031{public static void main(String []args ){int ri, repeat,count, word,i;String line;char c;Scanner in=new Scanner(System.in);repeat=(in.nextLine()).charAt(0)-'0'; //输入repeat for(ri=1; ri<=repeat; ri++){line=in.nextLine(); //输入一行字符/*---------*/System.out.println(count);}}}40032import java.util.Scanner;public class Test40032{public static void main(String []args ){int ri, repeat;int digit;long n, temp, pow;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1; ri<=repeat; ri++){n=in.nextLong();/*---------*/System.out.println();}}}40033import java.util.Scanner;public class Test40033{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);/*---------*/System.out.println(res);}}40034import java.util.Scanner;public class Test40034{public static void main(String []args){int year,m,n,repeat,ri;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){m=in.nextInt();n=in.nextInt();/*---------*/}}40035import java.util.Scanner;public class Test40035{public static void main(String []args){int m,n,repeat,ri;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){n=in.nextInt();m=in.nextInt();/*---------*/}}}40036import java.util.Scanner;public class Test40036{public static void main(String []args){int days,repeat,ri;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){}}}40037import java.util.Scanner;public class Test40037{public static void main(String []args){int a,n,ri,count,number;double sum,ave;Scanner in=new Scanner(System.in);n=in.nextInt();for(ri=1;ri<=n;ri++){/。
浙大JAVA实验习题答案answer
浙⼤JAVA实验习题答案answer 实验4 顺序结构程序的设计1.程序填空题,不要改变与输⼊输出有关的语句。
20002输⼊1个实数x,计算并输出其平⽅根。
例:输⼊1.21输出}取余,int number, digit1, digit2, digit3, digit4, newnum;number=in.nextInt();/*------------------*//*先分离出各位上的数字*/digit1=number/1000;digit2=number/100%10; //或 digit2=(number-digit1*1000)/100; digit3=number/10%10; //或digit3=(number-digit1*1000-digit2*100)/10;digit4=number%10;/*再调整各位上的数字*/digit1=(digit1+9)%10;digit2=(digit2+9)%10;digit3=(digit3+9)%10;digit4=(digit4+9)%10;}Gg}输⼊输出⽰例:括号内为说明输⼊1000 3 0.025 (money = 1000, year = 3, rate = 0.025) 输出interest = 76.89public class Test20008 {public static void main(String[] args) {int money, year;double interest, rate;Scanner in=new Scanner(System.in);/*使⽤in.nextDouble()和in.nextInt()输⼊double和int型数据*/money=in.nextInt();year=in.nextInt();rate=in.nextDouble(); //注意:这三条输⼊语句不能改变顺序interest=money*Math.pow((1+rate), year)- money;//注意:指数运算没有运算符,应使⽤数学类的⽅法pow()}}}20012 计算x的平⽅(提⽰:注意输出格式)输⼊x,计算x的平⽅y。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验14 类的设计和实验题库中未做的题1.程序填空题,不要改变与输入输出有关的语句。
30009 求一元二次方程的根(选做)输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入参数a,b,c,求一元二次方程a*x*x+b*x+c=0的根,结果保留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+值2ix2=值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 23 (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");elseSystem.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 = (数字与运算符之间有一空格)输出510import 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 Circle{private 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 Circle{public 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个数的最大值。