浙大JAVA 实验习题答案08answer
- 格式:doc
- 大小:55.00 KB
- 文档页数:10
Chapter 8 Objects and Classes1. See the section "Declaring and Creating Objects."2. Constructors are special kinds of methods that arecalled when creating an object using the new operator.Constructors do not have a return type—not even void.3.An array is an object. The default value for theelements of an array is 0 for numeric, false forboolean, …\u0000‟ for char, null for object elementtype.4.(a) There is such constructor ShowErrors(int) in theShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors(5);}public ShowErrors () {}}On Line 3, new ShowErrors(5) attempts to create aninstance using a constructor ShowErrors(int), but theShowErrors class does not have such a constructor. Thatis an error.(b) x() is not a method in the ShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors();t.x();}public ShowErrors () {}}On Line 4, t.x() is invoked, but the ShowErrors classdoes not have the method named x(). That is an error.(c) The program compiles fine, but it has a runtimeerror because variable c is null when the printlnstatement is executed.(d) new C(5.0) does not match any constructors in classC. The program has a compilation error because class Cdoes not have a constructor with a double argument.5.The program does not compile because new A() is used inclass Test, but class A does not have a defaultconstructor. See the second NOTE in the Section,“Constructors.”6.falsee the Date‟s no-arg constructor to create a Date forthe current time. Use the Date‟s toString() method todisplay a string representation for the Date.e the JFrame‟s no-arg constructor to create JFrame. Usethe setTitle(String) method a set a title and use thesetVisible(true) method to display the frame.9.Date is in java.util. JFrame and JOptionPane are injavax.swing. System and Math are in ng.10. System.out.println(f.i);Answer: CorrectSystem.out.println(f.s);Answer: Correctf.imethod();Answer: Correctf.smethod();Answer: CorrectSystem.out.println(Foo.i);Answer: IncorrectSystem.out.println(Foo.s);Answer: CorrectFoo.imethod();Answer: IncorrectFoo.smethod();Answer: Correct11. Add static in the main method and in the factorialmethod beca use these two methods don‟t need referenceany instance objects or invoke any instance methods inthe Test class.12. You cannot invoke an instance method or reference aninstance variable from a static method. You can invoke astatic method or reference a static variable from aninstance method? c is an instance variable, which cannot be accessed from the static context in method2.13. Accessor method is for retrieving private data value and mutator method is for changing private data value. The naming convention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. The naming convention for mutator method is setDataFieldName(value).14.T wo benefits: (1) for protecting data and (2) for easyto maintain the class.15. Yes. Though radius is private, myCircle.radius is usedinside the Circle class. Thus, it is fine.16. Java uses “pass by value” to pass parameters to amethod. When passing a variable of a primitive type toa method, the variable remains unchanged after themethod finishes. However, when passing a variable of areference type to a method, any changes to the objectreferenced by the variable inside the method arepermanent changes to the object referenced by thevariable outside of the method. Both the actualparameter and the formal parameter variables referenceto the same object.The output of the program is as follows:count 101times 017.Remark: The reference value of circle1 is passed to x and the reference value of circle2 is passed to y. The contents of the objects are not swapped in the swap1 method. circle1 and circle2 are not swapped. To actually swap the contents of these objects, replace the following three linesCircle temp = x;x =y;y =temp;bydouble temp = x.radius;x.radius = y.radius;y.radius = temp;as in swap2.18. a. a[0] = 1 a[1] = 2b. a[0] = 2 a[1] = 1c. e1 = 2 e2 = 1d. t1‟s i = 2 t1‟s j = 1t2‟s i = 2 t2‟s j = 119. (a) null(b) 1234567(c) 7654321(d) 123456720. (Line 4 prints null since dates[0] is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.)。
习题八参考答案1.什么是组件?什么是容器?并说明各自的作用。
答:从实现角度来看,组件(Component)是构成GUI的基本要素,作用是通过对不同事件的响应来完成和用户的交互或组件之间的交互;容器是能容纳和排列组件的对象,如Applet> Panel (面板)、Frame (窗口)等,作用就是放置组件并控制组件位置。
2.简述Swing组件的优点。
答:Swing是在AWT基础上扩展而来的,提供了非常丰富的组件,远远多于AWT,并且引入了新的概念和性能,这使得基于Swing开发GUI应用程序比直接使用AWT开发更为灵活、方便、效率高,而且能设计出更优美的、感受更好的GUI。
3.简述容器的概念,结合8.4.7小节的内容,解释什么是应用程序的主框架?答:容器是用来容纳其他组件和容器的特殊组件,是由容器类(Container类)创建的对象。
在Java语言中,容器类是组件类(组件类Component类)的一个子类,具有组件的所有性质。
在AWT 技术中,容器类由java. awt包提供,主要包括面板类Panel、窗口类Window、结构类Frame、对话框类Dialog等。
在Swing技术中,容器类由javax. swing包提供,并可分为如下三类:>顶层容器:JFramc. JApplet. JDialog、JWindow;>中间容器:JPanel、JScrollPane^ JSplitPane、JDesktopPaneJToolBar;特殊容器:在GUI上起特殊作用的中间层,如J Interna IFrame、JLayeredPane、 JRootPaneo 应用程序的主框架由可以容纳应用程序各种组件的顶层容器创建,除了负责组件的管理外,通常还提供最大化、最小化、关闭按钮等,实现应用程序展现方式以及关闭等。
4.总结JFrame的使用要点,并说明内容面板的作用。
答:JFrame类包含很多设置窗体的方法,可以用setTitle(String tille)方法设置窗体标题,用setBounds(inl x,int y,int width,int height)方法设置窗体显示的位置及大小,用setVisable (Boolean b)方法设置可见与否(默认不可见)。
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++){/。
浙大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答案全集实验汇总。
实验2 数据类型和变量的使用一、程序填空,在屏幕上显示一个短句“Programming in Java is fun!”import java.io.*;public class Test10001{public static void main(String args[]){/*------------------------*/System.out.println("Programming in Java is fun!");}}二、程序填空,在屏幕上显示如下网格。
+---+---+| | || | |+---+---+import java.io.*;public class Test10002{public static void main(String args[]){/*------------------------*/System.out.println("+---+---+");System.out.println("| | |");System.out.println("| | |");System.out.println("+---+---+");}}三、编写程序,在屏幕上显示如下图案。
(要求:第1行行首无空格,每行行尾无空格)* * * ** * ** **public class Test10003{public static void main(String args[]){/*------------------------*/System.out.println("* * * *");System.out.println(" * * *");System.out.println(" * *");System.out.println(" *");} }实验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。
Java实验综合实验及练习第一部分:编程题1、编写程序实现输入整数n,输出如下所示由数字组成的菱形。
(图中n=5)11 2 11 2 3 2 11 2 3 4 3 2 11 2 3 4 5 4 3 2 11 2 3 4 3 2 11 2 3 2 11 2 112.给出年、月、日,计算该日是该年的第几天?3、利用求素数的方法,就6~2000000之间的所有偶数验证歌德巴赫猜想:任何一个大于6的偶数可以分解为两个素数之和。
4、现有15位选手参加比赛,有6个评委每个评委都要给每位选手打分,分数为60~100分,现需要定义二维数组存储选手成绩,每行存储一位选手打分情况,要求输出选手得分以及选手最后得分,以及选手名次。
评分规则:分数为60~100分。
选手最后得分为:去掉一个最高分和一个最低分后其余4个分数的平均值。
5、设计一个描述二维平面上点的类Position,该类需要描述点的横坐标和纵坐标,并提供属性的set/get访问器方法和计算两点间距离的方法。
写一个程序测试这个类6、设计一个表示二维平面上点的类Point,包含有表示坐标位置的protected类型的成员变量x和y,获取和设置x 和y值的public方法。
设计一个表示二维平面上圆的类Circle,它继承自类Point,还包含有表示圆半径的protected类型的成员变量r、获取和设置r值的public方法、计算圆面积的public方法。
7、编写一个完整的Java Application 程序。
包含接口Shape,MyRectangle类,MyTriangle 类及Test类,具体要求如下:⑴、接口Shape:double area():求一个形状的面积double perimeter ():求一个形状的周长⑵、类 MyRectangle :实现Shape 接口,并有以下属性和方法:① 属性width : double 类型,表示矩形的长height : double 类型,表示矩形的高② 方法MyRectangle(double w, double h):构造函数ToString()方法 :输出矩形的描述信息,如“width=1.0,height=2.0, perimeter=6.0,area=2.0”⑶、类MyTriangle :实现Shape 接口,并有以下属性和方法:① 属性x,y,z: double 型,表示三角形的三条边s: 周长的1/2(注:求三角形面积公式为))()((z s y s x s s ---,s=(x+y+z)/2 ,开方可用Math.sqrt(double)方法)② 方法MyTriangle(double x, double y, double z):构造函数,给三条边和s 赋初值。
参考答案第1章一、判断题1. ×2. ×3. √4. √5. √6. ×7. ×8. ×二、选择题1. A、B、C、D、E2. B3. C4. A5. B6. B7. E三、填空题1. 面向对象的、解释的、结构中立的、可移植的、多线程的2. 一种与平台无关的二进制字节码(Byte code)3. 可移植性4. JVM5. Applet、Java Application6. 动态7. 命令行8. 慢9. 大大提高Java字节码的执行效率10. java、class四、问答题1. Java 是一种简单的、面向对象的、分布式的、解释的、健壮的、安全的、结构中立的、可移植的、性能优异的、多线程的、动态的语言,它还支持垃圾自动回收。
Java语言的“动态性”和C++语言相比,Java语言允许在运行时动态地装入所需要的类,这些类既可以位于本地,也可以是从Internet上下载的,这一特性是C++等其他编译型语言所无法实现的。
2.用户应用程序Java APIJVM硬件平台3. (见课本中设置Path一节)五、编程题1,2都是利用集成化开发环境实际动手操作。
第2章一、判断题1. √2. √3. √4. √5. √6. ×7. ×8. √二、选择题1. A、B2. A、C、D3. C4. C5. A、D6. C7. A8. A三、填空题1. 02. –128 ~ 1273. 位运算、关系运算4. 32,645. 保护变量6. 807. 在前面加个‘0’,0128. while四、问答题1. 十进制常数记法就是我们日常生活用的整数的写法:如365,0,-29456。
八进制常数记法只需要在前面加个‘0’即可。
如012表示八进制数12。
它转化为十进制的值是1×81 + 2×80=10,-012则和十进制数-10相等。
十六进制数记法是开头加上‘0x’ 或者‘0X’,如0x12表示十六进制数18。
一、求两个数的和与差。
程序填空,不要改变与输入输出有关的语句。
输入整数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);四、计算旅途时间。
实验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 java.util.Scanner;public class Test50009{public static void main(String args[]){ int ri,repeat; long n, res;Scanner in=new Scanner(System.in); repeat=in.nextInt();for(ri=1;ri<=repeat;ri++){ n=in.nextInt(); /* */res=reverse(n);System.out.println(res);}}/* ----- */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;}returnflag*a;}}2. 十进制转换二进制输入一个正整数 repeat (0<repeat<10) ,做 repeat 次下列运算:输入1个正整数n,将其转换为二进制后输出。
要求定义并调用函数 dectobin(n) ,它的功能是输出 n 的二进制。
参考答案第1章一、判断题1. ×2. ×3. √4. √5. √6. ×7. ×8. ×二、选择题1. A、B、C、D、E2. B3. C4. A5. B6. B7. E三、填空题1. 面向对象的、解释的、结构中立的、可移植的、多线程的2. 一种与平台无关的二进制字节码(Byte code)3. 可移植性4. JVM5. Applet、Java Application6. 动态7. 命令行8. 慢9. 大大提高Java字节码的执行效率10. java、class四、问答题1. Java 是一种简单的、面向对象的、分布式的、解释的、健壮的、安全的、结构中立的、可移植的、性能优异的、多线程的、动态的语言,它还支持垃圾自动回收。
Java语言的“动态性”和C++语言相比,Java 语言允许在运行时动态地装入所需要的类,这些类既可以位于本地,也可以是从Internet上下载的,这一特性是C++等其他编译型语言所无法实现的。
2.3. (见课本中设置Path一节)五、编程题1,2都是利用集成化开发环境实际动手操作。
第2章一、判断题1. √2. √3. √4. √5. √6. ×7. ×8. √二、选择题1. A、B2. A、C、D3. C4. C5. A、D6. C7. A 8. A三、填空题1. 02. –128 ~ 1273. 位运算、关系运算4. 32,645. 保护变量6. 807. 在前面加个‘0’,0128. while四、问答题1. 十进制常数记法就是我们日常生活用的整数的写法:如365,0,-29456。
八进制常数记法只需要在前面加个‘0’即可。
如012表示八进制数12。
它转化为十进制的值是1×81 + 2×80=10,-012则和十进制数-10相等。
十六进制数记法是开头加上‘0x’或者‘0X’,如0x12表示十六进制数18。
实验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)输出:import class Test50001 {#public static void main(String[] args) {int ri,repeat;int i,n;double s;Scanner in=new Scanner;repeat=();for(ri=1;ri<=repeat;ri++){n=();/*-----------*/s=0;。
for(i=1;i<=n;i++)s+=fact(i);}}/*---------------*/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)。
例如,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)>import class Test50002{public static void main(String args[]){int ri, repeat;int i, n,a;long sn;Scanner in=new Scanner;repeat=();for(ri=1; ri<=repeat; ri++){a=();n=();、/*------------*/sn=0;for(i=1;i<=n;i++)sn+=fn(a,i);}}/*------------*/static int fn(int a,int n){int s=0;,for(int i=1;i<=n;i++)s=s*10+a;return s;}}50003 统计一个整数中数字的个数输入一个正整数repeat (0<repeat<10),做repeat次下列运算:#读入1 个整数,统计并输出该数中2的个数。
实验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答案全集实验汇总。
实验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多一个字符“!”,所以不相同。
浙大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++){/。
实验8 Method的使用
1.程序填空题,不要改变与输入输出有关的语句。
50001
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:输入1 个正整数n,计算 s 的前n项的和(保留 4 位小数)。
s = 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<10),做repeat次下列运算:输入2个正整数a和n, 求a+aa+aaa+aa…a(n个a)之和。
要求定义并调用函数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)
-21902
2
345543
输出:
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
-2
99
}
/*---------*/
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<b)max=b;
if(max<c)max=c;
return max; }
}。