java第三章练习题
Java第三章--习题

3-10
编写一个字符界面的Java Application
程序,接受用户输入的10个整数,比较并 输出其中的最大值和最小值。
for循环实现
3-11
编写一个字符界面的Java Application 程序,接受用户输入的字符,以“#”结束 输入,比较并输出按字典序最小的字符
char c=(char)System.in.read(); System.in.skip(2);
do……while循环接收输入并比较,记录最小值
3-16
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
10个整数如何存放?
value = Integer.parseInt(s);
if……else……语句进行比较
第三章习题讲解
3-14
编写一个Java程序,接受用户输入的一 个1-12之间的整数,利用switch语句输出 对应月份的天数。
如果输入的数据不满足这个条件,则要 求用户重新输入。
3-14
switch语句控制用户输入后进入不同分支实现不同输出
BufferedReader br= new BufferedReader(new
编写一个字符界面的Java程序,接受用户 输入的2个整数为上下限,然后输出其间 的所有素数。
BufferedReader对 象 for
Java程序设计 第三章 测验答案 慕课答案 UOOC优课 深圳大学继续教育学院

第3章测验-3.2类的基本架构介绍一、单选题 (共100.00分)1.下列哪个类声明是正确的A.abstract final class H1{…}B.abstract private move(){…}C.protected private number;D.public abstract class Car{…}正确答案:D2.符合对象和类的关系的是:A.人和老虎B.书和汽车C.父亲和儿子D.汽车和交通工具正确答案:D3.下面关于java中类的说法哪个是不正确的A.类体中只能有变量定义、常量定义和成员方法的定义,不能包含“x=3;”这样的语句。
B.构造函数是类中的特殊方法C.主类一定要声明为public。
D.一个java文件中可以有多个class定义。
正确答案:C4.下面哪个单词是Java语言的关键字A.FloatB.thisC.stringD.unsigned正确答案:B5.Java编程所必须的默认引用包为A.java.sys包ng包包D.以上都不是正确答案:B6.以下哪个是JAVA的关键字?A.NULLB.newC.instanceOfD.wend正确答案:B7.下面哪个是Java语言中正确的标识符A.3comB.importC.thatD.this正确答案:C8.数组中可以包含什么类型的元素?A.int型B.string型C.数组D.以上都可以正确答案:D9.在Java中函数main()的返回值是:()A.StringB.intC.charD.void正确答案:D10.java应用在消费电子市场上开发平台名称为:A.JDKB.J2MEC.J2SED.J2EE正确答案:B第3章测验-3.4使用对象一、单选题 (共100.00分)1.构造方法何时被调用A.类定义时B.创建对象时C.调用对象方法时D.使用对象的变量时正确答案:B2.在编写Java Application程序时,若需要使用到标准输入输出语句,必须在程序的开头写上()语句。
JAVA练习题(第3章)..

11、应用程序的main方法中有以下语句,则输出的 结果是 ( )。 int b[][]={{1, 1, 1}, {2,2}, {3}}; int sum=0; for(int i=0; i<b.length; i++) { for(int j=0; j<b[i].length; j++) { sum+=b[i][j]; } 【答案】:A } System.out.println("sum="+sum); A、10 B、6 C、 9 D、 13
10、程序Test.java编译运行后输出的结果是( )。 public class Test { String s1="java"; public static void main(String args[]) { int z=2; Test t=new Test(); System.out.println(t.s1+z); } 【答案】:A } A、 java2 B 、2 C、没有输出结果 D、java
【答案】:B 6、定义一个类,必须使用的关键字是( ) A、public B、class C、interface D、static 7、应用程序的main方法中有以下语句,则输出的 结果是 ( )。 String s1=new String("abc"); 【答案】:A String s2=new String("abc"); boolean b1=s1.equals(s2); boolean b2=(s1==s2); System.out.print(b1+" "+b2);
3、以下关于继承的叙述正确的是( )。 A、在Java中类只允许单一继承 B、在Java中一个类只能实现一个接口 C、在Java中一个类不能同时继承一个类和实现一 个接口 【答案】:A D、在Java中接口只允许单一继承
JAVA第三章练习

1.用穷举法求出3位数中百、十、个位数的立方和就是该数的数。
public class Test{ public static void main(String[] args){int a,b,c,x=100;while(x<1000){a=x%10;b=(x%100-a)/10;c=(x-x%100)/100;if(a*a*a+b*b*b+c*c*c==x)System.out.println(x);x+=1;}}}2.编程实现打印以下图案:************************************public class Test{ public static void main(String[] args){int i,j,k; // i控制行数, k控制*的个数,j控制空格数for(i=1;i<=6;i++){for(j=1;j<=i-1;j++)System.out.print(" "); //打印空格for(k=1;k<=13-i*2;k++)System.out.print("*"); //打印*号System.out.println(); //换行}}}3. 统计1至1万共有多少个数是素数。
public class Test{ public static void main(String[] args){ int i,j,count=0;label:for(i=1;i<=10000;i++) //查找1到10000以内的素数{ for(j=2;j<i;j++) //检验是否不满足素数条件if (i%j==0) //不满足continue label; //跳过后面不必要的检验count++; //计数}System.out.println("个数:"+count);}}4.读程序,写结果。
JAVA期末复习题及答案——第三章

JAVA期末复习题及答案——第三章一、填空题1.阅读下列程序段int i=3,j;outer:while(i>0){j=3;inner:while(j>0){if(j<2) break outer;System.out.println(j+”and”+i);j--;}i--;}被输出到屏幕第一行的结果是 3 and 3 。
2.阅读下列代码段int x=3;while(x<9)x+=2;x++;while语句成功执行的次数是 3次。
3.已知a=3,b=9,则表达式a>b?a,b的值为 9 。
4.关系运算符的运算结果一定是布尔数据类型。
5. do-while 表示的循环体会至少执行一次。
6.已知x=12,y=6,z=false,则下列表达式的值分别为(表达式之间没有执行的先后顺序):100= =x+y falsex=y+10 16z||y>x false!(y*3<=x*4) false7.跳转语句continue用于循环体时表示退出本次循环,跳转语句break用于循环体时表示退出整个循环。
二、选择题1.阅读下列程序if(x= =0) {System.out.println(“冠军”);else if(x>-3) {System.out.println(“亚军”);else {System.out.println(“季军”);若要求打印字符串“季军“,则变量x的取值范围是:DA、x=0&x<=-3B、x=0C、x>-3D、x<=-32.阅读下面程序import javax.swing.JOptionPane;public class BreakLabelTest{public static void main(String args[]){String output=””;stop:{for(int row=1;row<=10;row++){for(int column=1;column<=5;column++){if(row= =5)break stop;output+=”* “;}output+=”\n”;}output+=”\nLoops terminated normally”;}JOptionPane.showMessageDialog{null,output,”用一个标志测试break语句”,RMATION_MESSAGE);System.exit(0);}}程序运行结果是:CA、窗口中有5行* * * * *B、窗口中有5行* * * *C、窗口中有4行* * * * *D、窗口中有6行* * * * *3.下列语句中,属于多分支语句的是:BA、if语句B、switch语句C、do while语句D、for语句4.阅读下列代码public class Test2005{public static void main(String args[]){String s=”Test”;switch(s){case “Java”: System.out.print(“Java”); break;case “Language”: System.out.print(“Language”); break;case “Test”: System.out.print(“Test”); break;}}}其运行结果是:DA、JavaB、LanguageC、TestD、编译出错5.下列代码中if(x>0) {System.out.println(“first”);}else if(x>-3){System.out.println(“second”);}else {System.out.println(“third”);}要求打印字符串为“second”时,x的取值范围是:AA、x<=0并且x>-3B、x>0C、x >-3D、x<=36.下列哪个说法是正确的?BA. if语句和else语句必须成对出现B. if语句可以没有else语句对应C. switch后的表达式可以是long型D. 每个switch结构中必须有default结构7.以下语句中有语法错误的是:BA. for(; ;);B. for(int i=0, i<10, i++){}C. if(a<0) ++a;D.do{a=false;} while(a=true);8.while循环和do_while循环的区别是:DA、没有区别,这两种结构在任何情况下效果都是一样的B、while循环的执行效率比do_while循环的执行效率高C、while先循环后判断,所以循环体至少执行一次D、do_while先循环后判断,所以循环体至少执行一次9.关于for循环和while循环,下面哪个说法是正确的?BA、while循环能实现的操作,for循环都能实现B、while循环的判断条件一般是程序的结果,for循环的判断条件一般是非程序的结果C、两种循环在任何时候都可以互换,所以会使用一种就可以D、两种循环结构中都必须有循环体,循环体不能为空10.下面说法正确的是:CA、程序执行到break语句时一定会结束所有的循环B、程序执行到continue语句时会结束当前循环C、break语句和continue语句都可以和标签协同使用D、break语句和continue语句的作用相同三、判断题1.switch语句中switch后面的表达式可以是整型,字符型,还可以是复合数据类型。
JAVA练习题(第3章)

第3章
一、单选择题
【答案】:C 1、Java语言中,只限子类或者同一包中的类的方 法能访问的访问权限是( ) A.public B.private C.protected D.<无修饰> 2、设有数组定义int[][] x={{1,2},{3,4,5},{6}, {}};,则x.length的值为( ) A.3 B.4 C.6 D.7 【答案】:B
15、下列说法哪个正确? ( ) A.不需要定义类,就能创建对象 【答案】:C B.对象中必须有属性和方法 C.属性可以是简单变量,也可以是一个对象 【答案】: A D、属性必须是简单变量 16、构造函数何时被调用? ( ) A、创建对象时 B、类定义时 C、使用对象的方法时 D、使用对象的属性时 17、关于继承的说法正确的是: ( ) A、子类将继承父类所有的属性和方法。 B、子类将继承父类的非私有属性和方法。 C、子类只继承父类public方法和属性 D、子类只继承父类的方法,而不继承属性 【答案】:B
5、下面程序段的执行结果是( 6 5 4 3 2) int a[] = { 2, 3, 4, 5, 6 }; for (int i = a.length - 1; i >= 0; i--) System.out.print(a[i] + ""); 6、构造方法是一种特殊的成员方法,构造方 法名与( 类名 ) 相同。 7、Java语言只允许单继承,指每个类只能有 一个 ( 父类 )。
【答案】:B 6、定义一个类,必须使用的关键字是( ) A、public B、class C、interface D、static 7、应用程序的main方法中有以下语句,则输出的 结果是 ( )。 String s1=new String("abc"); 【答案】:A String s2=new String("abc"); boolean b1=s1.equals(s2); boolean b2=(s1==s2); System.out.print(b1+" "+b2);
Java第三章习题

第3章一、选择题1.下列()不属于Java语言流程控制结构?(A)分支语句(B)跳转语句(C)循环语句(D)赋值语句2.假设a是int类型的变量,并初始化为1,则下列()是合法的条件语句?(A)if(a){} (B)if(a<<=3){} (C)if(a=2){} (D)if(true){}3.下列说法中,不正确的一个是( C )。
(A)switch语句的功能可以由if…else if语句来实现(B)若用于比较的数据类型为double型,则不可以用switch语句来实现(C)if …else if语句的执行效率总是比switch语句高(D)case子句中可以有多个语句,并且不需要大括号{}括起来4.设a、b为long型变量,x、y为float型变量,ch为char类型变量且它们均已被赋值,则下列语句中正确的是()。
(A)switch(x+y) {} (B)switch(ch+1) {}(C)switch ch {} (D)switch(a+b); {}5.下列循环体执行的次数是()。
int y=2, x=4;while(--x != x/y){ }(A)1 (B)2 (C)3 (D)46.下列循环体执行的次数是()。
int x=10, y=30;do{ y -= x; x++; }while(x++<y--);(A)1 (B)2 (C)3 (D)47.已知如下代码:switch(m){case 0: System.out.println("Condition 0");case 1: System.out.println("Condition 1");case 2: System.out.println("Condition 2");case 3: System.out.println("Condition 3");break;default:System.out.println("Other Condition");}当m的值为()时,输出“Condition 3”(A)2 (B)0、1 (C)0、1、2 (D)0、1、2、3二、填空题1.跳转语句包括、、和2.switch语句先计算switch后面的的值,再和各语句后的值做比较。
java习题及答案第3章 习题参考答案

第3章习题解答1. Java语言的注释有哪几种?分别给出一个例子。
答:Java语言的注释有3种,分别是单行注释、多行注释和文档注释。
单行注释的例子如下:public static Point origin = new Point(0, 0); //类初始化时,创建一个原点实例多行注释的例子如下:/* 类初始化时,创建一个原点实例 */public static Point origin = new Point(0, 0);文档注释的例子如下:/**** @类名:Point* @类简介:坐标点类,可以初始化其坐标x和y。
* @编程人:林福平* @编程日期:2012-8-9* @修改日期:2012-8-10**/2. Java语言中分隔符有哪几种?空白符有哪些?答:Java语言中的分隔符有空白符、注释和普通分隔符三种。
Java语言中的空白符(White Space)包括空格(SP,space)、制表符(‘\t’,Tab键)、走纸换页(‘\f’)、回车(‘\r’)和换行(‘\n’)。
3. 简述标识符的用途。
下列字符串中,哪些是标识符?PIx2 -length a+b _bytes $long MIN_VALUE答:Java语言中的标识符用于对类、方法、变量、类型、数组和文件等进行命名。
上述字符串中,以下这些是Java语言的标识符:PIx2 _bytes $long MIN_VALUE4. 下列字符串中,哪些是关键字?true for int null $float _double答:上述字符串中,以下这些是Java语言的关键字:true for int null5. Java语言的基本数据类型分为那几大类?答:Java语言的基本数据类型分为数值类型(包括整数类型和浮点类型)、字符类型(char)和布尔类型(Boolean)。
整数类型有byte、 short、 int和long。
浮点类型有float和double。
[教学]java程序设计项目教程第3章答案
![[教学]java程序设计项目教程第3章答案](https://img.taocdn.com/s3/m/b29a152e11661ed9ad51f01dc281e53a5802519d.png)
一、选择题参考答案:1.B2.A3.D4.C5.A6.A7.B8.B9.A 10.A 11.A 12.D二、填空题参考答案:1.覆盖2.参数3.方法体4.public static final5.抽象6.extends7.Student String 8.class static三、简答题1.子类能够继承父类的哪些成员变量和方法?①子类可以继承父类的属性,包括实例成员变量和类成员变量。
②子类可以继承父类除构造方法以外的成员方法,包括实例成员方法和类成员方法。
2.this和super关键字的作用是什么?0使用this关键字可实现调用本类的构造方法及调用被方法隐藏的成员变量。
0super关键字可调用父类的构造方法及调用父类的同名成员。
03.什么是方法的重载?什么是方法的覆盖?0方法重载是指同一个类中多个方法享有相同的名字,但是这些方法的参数必须不同,参数不同是,指或者是参数的个数不同,或者是参数类型不同,或者是不同类型参数的排列顺序不同。
0类继承的过程中,子类方法跟父类方法名字相同,并且传递的参数完全一样,称子类覆盖了父类的方法。
04.什么是多态?使用多态有什么优点?0多态的表现形式主要有方法的重载和方法的覆盖。
0使用多态可根据同名方法的参数不同实现不能的功能或同一类对象根据赋予的引用对象的不同来实现不同的行为。
05.什么是包?定义包的作用是什么?0包是java提供的一种命名空间机制,实现了对类的存放和引用位置的管理,包对应一个文件夹。
0java的类库就是用包来实现了类的分类和存放,每个包中都有多组相关的类和接口。
6.什么是接口?接口中的变量和方法各有什么要求?接口是特殊的抽象类,可以想象为一个“纯”抽象类,就是一组具有特定意义的静态常量和抽象方法的集合。
属性定义时必须赋初值,是常量。
属性性前修饰符时默认该属性由final、static修饰。
接口中的方法必须是抽象方法。
7.类BB是AA类的子类。
类AA和类BB中都定义了变量x和method()方法,这种情况称为子类隐藏了父类的同名变量x并覆盖了父类的method()方法.8.输出结果为:AABB若将main()方法中的语句改为:BB b=new BB(10); 程序输出的结果是什么?AAAAAABBBB四、编程1.编写一个类,描述学生的学号、姓名、成绩。
第三章-面向对象程序设计(答案)

一、判断题1、一个Java源程序可有多个类,但只仅有一个public类,而且程序名与public类名相同。
对2、如果类A和类B在同一个包中,则除了私有成员外,类A可以访问类B中所有的成员。
对3、接口中的成员变量全部为常量,方法为抽象方法。
对4、抽象类可以有构造方法,可以直接实例化。
错5、对static方法的调用可以不需要类实例。
对6、包含抽象方法的类一定是抽象类。
对7、方法中的形参可以和方法所属类的属性同名。
对8、接口无构造器,不能有实例,也不能定义常量。
错9、类的实例对象的生命周括实例对象的创建、使用、废弃、垃圾的回收。
对10、Java应用程序的入口main方法只有一种定义法。
对二、选择题1、下列答案正确的是(A)A) 在同一个Java源文件中可以包含多个类,只能有一个被声明为publicB) 在同一个Java源文件中只能包含一个类,并被声明为publicC) 在同一个Java源文件中可以包含多个类,都可以被声明为publicD) 在同一个Java源文件中可以包含多个类,只能有一个被声明为default2、Java实现动态多态性是通过( B )实现的。
A) 重载B) 覆盖C) 接口D) 抽象类3、下列哪一个是正确的方法重载描述(A)A) 重载方法的参数类型必须不同B) 重载方法的参数名称必须不同C) 返回值类型必须不同D) 修饰词必须不同4、final关键字不可以用来修饰( D )A) 类B) 成员方法C) 域D) 接口5、接口的所有成员方法都具有( B )属性A) private, final B) public, abstractC) static, protected D) static6、Java的封装性是通过(A)实现的A) 访问控制B) 设计内部类C) 静态域和静态方法D) 包7、下列接口或类不属于java.util.*包的是( D )A) Collection B) V ector C) Map D) Integer8、下述哪一组方法,是一个类中方法重载的正确写法?(A)A) int addV alue( int a, int b ){return a+b;}float addV alue ( float a, float b) {return a+b;}B) int addV alue (int a, int b ){value=a+b; }float addV alue ( int a, int b) {return (float)(a+b);}C) int addV alue( int a, int b ){return a+1;}int addV alue ( int a, int b) {return a+b;}D) int addV alue( int a, int b ) {return a+b;}int addV alue ( int x, int y ) {return x+y;}9、下列说法哪个是正确的?( C )A) 子类不能定义和父类同名同参数的方法B) 子类只能继承父类的方法,而不能重载C) 重载就是一个类中有多个同名但有不同形参和方法体的方法D) 子类只能覆盖父类的方法,而不能重载10、对于下列代码:public class Parent {public int addV alue( int a, int b) {int s;s = a+b;return s;}}class Child extends Parent {}下述哪个方法不可以加入类Child? ( B )A) public int addV alue( int a, int b,int c ){// do something...}B) public void addV alue (int a, int b ){// do something...}C) public int addV alue( int a ){// do something...}D) public int addV alue( int a, int b ) {//do something...}11、以下程序段输出结果的是( B )public class A implements B {public static void main(String args[]) {int i;A c1 = new A();i= c1.k;System.out.println("i="+i);}}interface B {int k = 10;}A) i=0 B) i=10 C) 程序有编译错误D) i=true12、阅读下面的程序,输出结果是( B )public class TestDemo {int m=5;public void some(int x) {m=x;}public static void main(String args []) {new Demo().some(7);}}class Demo extends TestDemo {int m=8;public void some(int x) {super.some(x);System.out.println(m);}}A) 5 B) 8 C) 7 D) 编译错误13、下述哪个说法是不正确的?(A)A) 局部变量在使用之前无需初始化,因为有该变量类型的默认值B) 类成员变量由系统自动进行初始化,也无需初始化C) 参数的作用域就是所在的方法D) for语句中定义的变量,当for语句执行完时,该变量就消亡了14、下述那一个保留字不是类及类成员的访问控制符。
JAVA第三章习题

第三章Java语言面向对象的特征一、选择题1、下列对封装性的描述中,错误的是 B 。
A.封装体包含了属性和行为B.封装体中的属性和行为的访问权限是相同的C.被封装的某些信息在封装体外是不可见的D.封装使得抽象的数据类型提高了可重用性2、在类的修饰符中,规定只能被同一包类所使用的修饰符是 B 。
A.public B.默认C.final D.abstract3、在成员变量的修饰符中,规定只允许该类自身访问的修饰符是 A 。
A.private B.public C.默认D.protected4、下列关于构造方法的特点的描述中,错误的是 A 。
A.不可重载B.方法名同类名C.无返回类型D.系统自动调用5、下列关于关于静态方法的描述中,错误的是 D 。
A.在类体内说明静态方法使用关键字B.静态方法只能处理静态变量或调用静态方法C.静态方法不占用对象的内存空间,非静态方法占用对象的内存空间D.静态方法只能用类名调用6、下列关于抽象类的描述中,错误的是 C 。
A.抽象类是用修饰符abstract说明的B.抽象类是不可以定义对象的C.抽象类是不可以有构造方法的D.抽象类通常要有它的子类7、下列关于接口的描述中,错误的是 B 。
A.接口实际上是由常量和抽象方法构成的特殊类B.一个类只允许继承一个接口C.定义接口使用的关键字是interfaceD.在继承接口的类中通常要给出接口中定义的抽象方法的具体实现8、下列关于包的描述中,错误的是 A 。
A.包是一种特殊的类B.包是若干个类的集合C.包是使用package语句创建的D.包有有名包和无名包两种9、下列常用包中,存放用户图形界面类库的包是 A 。
A.java.awtB.ngC.java.utilD.java.io10、下列是系统提供的常用的类,所有类的父类的类是 B 。
A.MathB.ObjectC.SystemD.String二、判断题1、类是一种类型,也是对象的模板。
JAVA习题库#第三章--控制结构

第三章判断题在选择结构中是必需地.()语句在选择结构地中是必需地.().如果>为真或.在包含运算符地表达式中,如果它地一个或两个操作数为真,则该表达式为真.()结构和结构所做地动作是相同.().想确保当两个条件都为时才执行某一动作,可以使用逻辑与运算符.().若要确定两个条件中是否有一个为或都为时,可使用逻辑异或^.().布尔逻辑与和布尔逻辑或运算符地工作方式完全相同.().结构化方法地优点在于,只允许使用种单入口单出口地组件.().结构化程序设计提高了程序地清晰简单性,并且它只需使用三种形式地控制结构就足够了.()第三章选择题.所有地程序均可以用几种类型控制结构编写:.顺序结构、选择结构、循环结构.顺序结构、循环结构.顺序结构、选择结构.选择结构、循环结构.当条件为真和条件为假时,▁▁控制结构可以执行不同地动作......当事先不知道语句重复执行地次数时,可以用一个▁▁值来终止循环..布尔.正.标记.负.使整值变量加,下面写出地形式不对地是:.....下面程序地输出结果是:{( ){ { (“ ”);}( >);}( []){;();();}}.....下面程序地那一行可能引发错误::(){:;:;:( >){:*;:(“ ”);:;:;:}:}.....下面程序地那一行可能引发错误::;:(( )( () > ){:(“ ”);:}:(( )(() < ){:(“ ”);:}:{ (“”); }.....如果是布尔变量,下面哪个选项是正确地:.;.(){ …}.(){ …}.;.请看下面地程序代码:(>) { (“”);}(<) { (“”);}{ (“”) }当程序输出“”时,地范围为:.<.>.>.请看下面地程序代码:() {: (“”);:: (“”); ;: (“”);}当为何值时,程序段将输出字符串:....第三章程序设计题.编写一个应用程序,计算和打印从加到地累加和.用结构循环执行计算及加语句.循环必须在加到时终止. .求出三个整数地最小值..编写一个程序接收用户输入地一个—之间地整数(如果输入地数据不满足这个条件,则要求用户重新输入),利用语句输入对应地月份地天数.第三章判断题答案.难度:容易答案:错误知识点:可缺省,若不需要缺省地操作,就可以不写项..难度:容易答案:错误知识点:语句用于退出结构,当作为结构地最后一种情况时,可以不写语句..难度:容易答案:错误知识点:使用运算符地表达式,只有两个操作数都为真时该表达式才为真..难度:容易答案:正确知识点:或()运算符地使用..难度:容易答案:错误知识点:结构仅选择或忽略某一个动作,要在不同地动作之间做选择..难度:容易答案:正确知识点:逻辑与地使用..难度:容易答案:错误知识点:应该使用逻辑或运算符..难度:适中知识点:布尔逻辑与和布尔逻辑或运算符地工作方式与逻辑与和逻辑或运算符除了短路计值一点外完全相同..难度:容易答案:错误知识点:还有两种组合方式..难度:适中答案:正确知识点:结构化程序设计地优点.第三章选择题答案.难度:容易答案:知识点:所有地程序均可以用顺序结构、选择结构、循环结构三种类型控制结构编写..难度:容易答案:知识点:当条件为真和条件为假时,控制结构可以执行不同地动作..难度:容易答案:知识点:标记值地使用..难度:容易答案:知识点:赋值运算符应该是 ..难度:适中答案:知识点:循环地使用..难度:适中答案:知识点:变量应该在使用前要被初始化..难度:适中答案:知识点:是布尔运算与,而是逻辑运算与..难度:容易答案:知识点:布尔变量不能被赋予数值..难度:容易答案:知识点:如果小于,则必须小于..难度:容易答案:知识点:没有在和语句地后面.第三章程序设计题答案.难度:容易答案:源程序{( []){;;;( < ){;;}(“ :” );}}知识点:对循环结构地调用..难度:适中答案:源程序.*;.*;.*;{;;;;(){("请先输入三个待比较地整数");();();();("比较");();();();();();();}( ){(());(());(<)(<);;(<);;("三数中地最小值是:" );}}知识点:综合训练前三章所学内容. .难度:适中答案:源程序.*;{( []){;{{("请输入~之间地一个整数:");( ());();();}( ){("输入格式错误.");;}( ){(());();}( < > );(){:("月份有天.");;:("月份有或天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;:("月份有天.");;}}}知识点:利用结构进行编程.。
JAVA第三章习题答案 (2)

o=cno; ame=cname; this.credit=credit; this.period=period; } public void setCno(String cno){ o=cno; } public String getCno(){ return cno; } public void setCname(String cname){ ame=cname; } public String getCname(){ return cname; } public void setCredit(double credit){ this.credit=credit; } public double getCredit(){ return credit; } public void setPeriod(int period){ this.period=period; } public int getPeriod(){ return period; } public String toString(){ return cno+"\t"+cname+"\t"+credit+"\t"+period; } } 9.设计并实现一个 Box 类。要求:定义 3 个实例变量分别表示 Box 的长、宽和高,并定义 必要的方法。创建一个对象,求给定尺寸的立方体的表面积和体积。 //Box.java public class Box { private double length; private double width; private double height;
10.学生类的创建和使用。 (1)创建一个 Student 类,包括的私有数据成员有学号、姓名、性别、年龄等。 (2)声明一个构造方法,以初始化对象的所有数据成员。 (3)声明分别获得各数据成员(学号、姓名、性别、年龄等)的各个 public 方法。 (4)声明分别修改各数据成员(学号、姓名、性别、年龄等)的各个 public 方法。 (5 ) 声明一个 public 型的 toString( )方法, 把该类中的所有数据成员信息组合成一个字符串。 (6 ) 在类中声明统计班级总人数的私有数据成员 count, 以及获得班级总人数的 public 方法。 (7)将 Student 类放在子包 student 中。 (8 ) 在子包 student 外, 创建测试类 StudentTest。 在测试类中, 使用 Student 类创建多个对象, 测试 Student 类的每个 public 方法。 //Student.java package student; public class Student { private String sno; private String sname; private char ssex; private int sage; private static int count=0; public Student(String no,String name,char sex,int age){ sno=no; sname=name; ssex=sex; sage=age; count++; } public void setSno(String no){ sno=no; } public String getSno(){ return sno; } public void setSname(String name){ sname=name; } public String getSname(){ return sname; } public void setSsex(char sex){ ssex=sex; } public char getSsex(){ return ssex;
JAVA基础 第3章类与对象_练习题

第3章类与对象一.选择题1.下列不属于面向对象编程的特性是(D)。
A.封装性 B. 继承性 C. 多态性 D. 编译执行2.下列类的声明中不合法的是(C)。
A. class People{…}B. class 植物{…}C. Class A{…}D. public class 共有类{…3.下列方法的声明中不合法的是(C)。
A. float area(){…}B. void area(){…}C. double area(d){…}D. int area(int r){…}4. 下列构造方法(构造器)的调用中正确的是(C)。
A. 按照一般的方法调用B. 由用户直接调用C. 只能通过new自动调用D. 被系统调用5.下列程序运行的结果是(A)。
class Book{int width;int length;}public class A{static void f(Book p){p.width=20;p.length=40;}public static void main(String args[]){Book b=new Book();b.width=10;b.length=20;f(b);System.out.print(" "+b.width);System.out.print(" "+b.length);}}A. 20 40B. 10 40C. 10 20D. 以上都不对6.下列程序运行的结果是(D)。
public class A{static void f(int y){y=y+10;}public static void main(String args[]){double x=10;f(x);System.out.println(x);}}精选文库A. 10B. 20C. 10.0D. 程序编译错误7.下列程序运行的结果是(C)。
public class A{int z=20;static void f(int y){y=z;System.out.println(y);}public static void main(String args[]){f(10);}}A. 10B. 20C. 程序编译错误D. 以上都不对8. 以下代码的输出结果为(C)。
《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案

《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案《Java语言程序设计(基础篇)》(第10版梁勇著)第三章练习题答案3.1public class Exercise03_01 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a, b, c: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double discriminant = b * b - 4 * a * c;if (discriminant < 0) {System.out.println("The equation has no real roots");}else if (discriminant == 0) {double r1 = -b / (2 * a);System.out.println("The equation has one root " + r1);}else { // (discriminant > 0)double r1 = (-b + Math.pow(discriminant, 0.5)) / (2 * a);double r2 = (-b - Math.pow(discriminant, 0.5)) / (2 * a);System.out.println("The equation has two roots " + r1 + " and " + r2);}}}3.1附加public class Exercise03_01Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter a numerator: ");int numerator = input.nextInt();System.out.print("Enter a denominator: ");int denominator = input.nextInt();if (numerator < denominator) {System.out.println(numerator + " / " + denominator + " is a proper fraction");}else if (numerator % denominator == 0) {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and it can be reduced to " + numerator / denominator);}else {System.out.print(numerator + " / " + denominator + " is an improper fraction ");System.out.println("and its mixed fraction is " + numerator / denominator + " + " +numerator % denominator + " / " + denominator);}}}3.2public class Exercise03_02 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int number1 = (int)(System.currentTimeMillis() % 10);int number2 = (int)(System.currentTimeMillis() * 7 % 10);int number3 = (int)(System.currentTimeMillis() * 3 % 10);System.out.print("What is " + number1 + " + " + number2 + " + " +number3 + "? ");int answer = input.nextInt();System.out.println(number1 + " + " + number2 + " + " + number3 +" = " + answer + " is " +(number1 + number2 + number3 == answer));}}3.2附加public class Exercise03_02Extra {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter the coordinates for two points: ");double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double m = (y2 - y1) / (x2 - x1);double b = y1 - m * x1;System.out.print("The line equation for two points (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 + ") is " + "y = ");if (m == -1)System.out.print("-x");else if (m == 1)System.out.print("x");elseSystem.out.print(m + "x");if (b > 0)System.out.println(" + " + b);else if (b < 0)System.out.println(" - " + (-1 * b));else// b is 0System.out.println();}}3.3public class Exercise03_03 {public static void main(String[] args) {Scanner input = new Scanner(System.in); System.out.print("Enter a, b, c, d, e, f: ");double a = input.nextDouble();double b = input.nextDouble();double c = input.nextDouble();double d = input.nextDouble();double e = input.nextDouble();double f = input.nextDouble();double detA = a * d - b * c;if (detA == 0) {System.out.println("The equation has no solution"); }else {double x = (e * d - b * f) / detA;double y = (a * f- e * c) / detA;System.out.println("x is " + x + " and y is " + y);}}}3.3附加public class Exercise03_03Extra {public static void main(String[] args) {final double RADIUS = 5;double angle = Math.random() * 2 * Math.PI;double x = RADIUS * Math.random() * Math.cos(angle);double y = RADIUS * Math.sin(angle);double distance = Math.pow(x * x + y * y, 0.5);System.out.println("The point is (" + x + ", " + y + ") and its distance to the center is " + distance);}}3.4public class Exercise03_04 {public static void main(String[] args) {int number = (int)(Math.random() * 12) + 1;// or int number = (int)(System.currentTimeMillis() % 12 + 1);// or int number = (int)(Math.random() * 12) + 1;if (number == 1)System.out.println("Month is Januaray");else if (number == 2)System.out.println("Month is Feburary");else if (number == 3)System.out.println("Month is March");else if (number == 4)System.out.println("Month is April");else if (number == 5)System.out.println("Month is May");else if (number == 6)System.out.println("Month is June");else if (number == 7)System.out.println("Month is July");else if (number == 8)System.out.println("Month is August");else if (number == 9)System.out.println("Month is September");else if (number == 10)System.out.println("Month is October");else if (number == 11)System.out.println("Month is November");else// if (number == 12)System.out.println("Month is December");}}3.5public class Exercise03_05 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter an integer for todaySystem.out.print("Enter today抯 day: ");int today = input.nextInt();System.out.print("Enter the number of days elapsed since today: ");int elapsedDays = input.nextInt();String nameForToday;if (today == 0)nameForToday = "Sunday";else if (today == 1)nameForToday = "Monday";else if (today == 2)nameForToday = "Tuesday";else if (today == 3)nameForToday = "Wednesday";else if (today == 4)nameForToday = "Thursday";else if (today == 5)nameForToday = "Friday";else// if (today == 6)nameForToday = "Saturday";int futureDay = (today + elapsedDays) % 7; String nameForFutureDay;if (futureDay == 0)nameForFutureDay = "Sunday";else if (futureDay == 1) nameForFutureDay = "Monday";else if (futureDay == 2) nameForFutureDay = "Tuesday";else if (futureDay == 3) nameForFutureDay = "Wednesday";else if (futureDay == 4) nameForFutureDay = "Thursday";else if (futureDay == 5) nameForFutureDay = "Friday";else// if (futureDay == 6) nameForFutureDay = "Saturday";System.out.println("Today is " + nameForToday+ " and the future day is " + nameForFutureDay); } }3.6public class Exercise03_06 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble();// Prompt the user to enter heightSystem.out.print("Enter feet: ");double feet = input.nextDouble();System.out.print("Enter inches: ");double inches = input.nextDouble();double height = feet * 12 + inches;// Compute BMIdouble bmi = weight * 0.45359237 / ((height * 0.0254) * (height * 0.0254));// Display resultSystem.out.println("BMI is " + bmi);if (bmi < 18.5)System.out.println("Underweight");else if (bmi < 25)System.out.println("Normal");else if (bmi < 30)System.out.println("Overweight");elseSystem.out.println("Obese");}}3.7/** Break down an amount into smaller units* Display the non-zero denominations only, and display singular* words for single units like 1 dollars, 1 penny, and display plural * words for more than one unit like 2 dollars, 3 pennies.*/public class Exercise03_07 {// Main methodpublic static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Receive the amount entered from the keyboardSystem.out.print("Enter an amount in double, for example 11.56: ");double amount = input.nextDouble();int remainingAmount = (int)(amount * 100);// Find the number of one dollarsint numberOfOneDollars = remainingAmount / 100;remainingAmount = remainingAmount % 100;// Find the number of quarters in the remaining amountint numberOfQuarters = remainingAmount / 25;remainingAmount = remainingAmount % 25;// Find the number of dimes in the remaining amountint numberOfDimes = remainingAmount / 10;remainingAmount = remainingAmount % 10;// Find the number of nickels in the remaining amountint numberOfNickels = remainingAmount / 5;remainingAmount = remainingAmount % 5;// Find the number of pennies in the remaining amountint numberOfPennies = remainingAmount;// Display resultsif (amount < 0) {System.out.println("Your amount is negative");System.exit(1);}else if (amount < 0) {System.out.println("Your amount is zero");System.exit(2);}System.out.println("Your amount " + amount + " consists of ");if (numberOfOneDollars > 1)System.out.println(numberOfOneDollars + "\ dollars");else if (numberOfOneDollars == 1)System.out.println(numberOfOneDollars + "\ dollar");if (numberOfQuarters > 1)System.out.println(numberOfQuarters + "\ quarters");else if (numberOfQuarters == 1)System.out.println(numberOfQuarters + "\ quarter");if (numberOfDimes > 1)System.out.println(numberOfDimes + "\ dimes");else if (numberOfDimes == 1)System.out.println(numberOfDimes + "\ dime");if (numberOfNickels > 1)System.out.println(numberOfNickels + "\ nickels");else if (numberOfNickels == 1)System.out.println(numberOfNickels + "\ nickel");if (numberOfPennies > 1)System.out.println(numberOfPennies + "\ pennies");else if (numberOfPennies == 1)System.out.println(numberOfPennies + "\ penny");}}3.8public class Exercise03_08 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three numbersSystem.out.print("Enter three integers: ");int number1 = input.nextInt();int number2 = input.nextInt();int number3 = input.nextInt();if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}if (number2 > number3) {int temp = number2;number2 = number3;number3 = temp;}if (number1 > number2) {int temp = number1;number1 = number2;number2 = temp;}System.out.println("The sorted numbers are "+ number1 + " " + number2 + " " + number3);}}public class Exercise03_09 {public static void main(String[] args) {Scanner input = new Scanner(System.in);// Prompt the user to enter an integerSystem.out.print("Enter the first 9 digits of an ISBN as integer: ");int number = input.nextInt();// Calculate checksum (You may write a loop to simplify it in Ch4int checksum =((number / 100000000 % 10) * 1 +(number / 10000000 % 10) * 2 +(number / 1000000 % 10) * 3 +(number / 100000 % 10) * 4 +(number / 10000 % 10) * 5 +(number / 1000 % 10) * 6 +(number / 100 % 10) * 7 +(number / 10 % 10) * 8 +(number % 10) * 9) % 11;System.out.print("The ISBN-10 number is ");// Display leading zeros, improve the solution using loops in the next chapterif (number / 100000000 == 0) {System.out.print("0");if (number / 10000000 == 0) {System.out.print("0");if (number / 1000000 == 0) {System.out.print("0");if (number / 100000 == 0) {System.out.print("0");if (number / 10000 == 0) { System.out.print("0");if (number / 1000 == 0) { System.out.print("0");if (number / 100 == 0) {System.out.print("0");if (number / 10 == 0) {System.out.print("0");if (number == 0) {System.out.print("0");}}}}}}}}}System.out.print(number);if (checksum == 10)System.out.print("X");elseSystem.out.print(checksum);}}3.10public class Exercise03_10 {public static void main(String[] args) {// 1. Generate two random single-digit integersint number1 = (int)(Math.random() * 10);int number2 = (int)(Math.random() * 10);// 2. Prompt the student to answer 搘hat is number1 + number2?? System.out.print("What is " + number1 + " + " + number2 + "? "); Scanner input = new Scanner(System.in);int answer = input.nextInt();// 4. Grade the answer and display the resultString replyString;if (number1 + number2 == answer)replyString = "You are correct!";elsereplyString = "Your answer is wrong.\" + number1 + " + "+ number2 + " should be " + (number1 + number2);System.out.println(replyString);}}3.11public class Exercise03_11 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter inputSystem.out.print("Enter a month in the year (e.g., 1 for Jan): ");int month = input.nextInt();System.out.print("Enter a year: ");int year = input.nextInt();int numberOfDaysInMonth = 0;switch (month) {case 1:System.out.print("January " + year);numberOfDaysInMonth = 31;break;case 2:System.out.print("February " + year);if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { numberOfDaysInMonth = 29;}else {numberOfDaysInMonth = 28;}break;case 3:System.out.print("March " + year);numberOfDaysInMonth = 31;break;case 4:System.out.print("April " + year);numberOfDaysInMonth = 30;break;case 5:System.out.print("May " + year);numberOfDaysInMonth = 31;break;case 6:System.out.print("June " + year);numberOfDaysInMonth = 30;break;case 7:System.out.print("July " + year);numberOfDaysInMonth = 31;break;case 8:System.out.print("August " + year);numberOfDaysInMonth = 31;break;case 9:System.out.print("September " + year);numberOfDaysInMonth = 30;break;case 10:System.out.print("October " + year);numberOfDaysInMonth = 31;break;case 11:System.out.print("November " + year);numberOfDaysInMonth = 30;break;case 12:System.out.print("December " + year);numberOfDaysInMonth = 31;break;}System.out.print(" has " + numberOfDaysInMonth + " days"); }}3.12public class Exercise03_12 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("Enter a three-digit integer: ");int number = input.nextInt();if (number / 100 == number % 10)System.out.println(number + " is a palindrome");elseSystem.out.println(number + " is not a palindrome");}}3.13public class Exercise03_13 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in);// Prompt the user to enter filing statusSystem.out.print("(0-single filer, 1-married jointly or qualifying widow(er),"+ "\2-married separately, 3-head of household)\" + "Enter the filing status: ");int status = input.nextInt();// Prompt the user to enter taxable incomeSystem.out.print("Enter the taxable income: ");double income = input.nextDouble();// Compute taxdouble tax = 0;if (status == 0) { // Compute tax for single filersif (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 82250) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950)* 0.25; } else if (income <= 171550) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (income - 82250) * 0.28;} else if (income <= 372950) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (income - 171550) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 + (372950 - 171550) * 0.33 + (income - 372950) * 0.35;}} else if (status == 1) { // Compute tax for married file jointly if (income <= 16700) {tax = income * 0.10;} else if (income <= 67900) {tax = 16700 * 0.10 + (income - 16700) * 0.15;} else if (income <= 137050) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25; } else if (income <= 208850) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (income - 137050) * 0.28;} else if (income <= 372950) {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (income - 208850) * 0.33;} else {tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (171950 - 137050) * 0.28 + (372950 - 208850) * 0.33+ (income - 372950) * 0.35;}} else if (status == 2) { // Compute tax for married separately if (income <= 8350) {tax = income * 0.10;} else if (income <= 33950) {tax = 8350 * 0.10 + (income - 8350) * 0.15;} else if (income <= 68525) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25; } else if (income <= 104425) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (income - 68525) * 0.28;} else if (income <= 186475) {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (income - 104425) * 0.33;} else {tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (income - 186475) * 0.35;}} else if (status == 3) { // Compute tax for head of household if (income <= 11950) {tax = income * 0.10;} else if (income <= 45500) {tax = 11950 * 0.10 + (income - 11950) * 0.15;} else if (income <= 117450) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25; } else if (income <= 190200) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (income - 117450) * 0.28;} else if (income <= 372950) {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (income - 190200) * 0.33;} else {tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33+ (income - 372950) * 0.35;}} else {System.out.println("Error: Wrong filing status");System.exit(1);}// Display the resultSystem.out.println("Tax is " + (int) (tax * 100) / 100.0);}}3.14public class Exercise03_14 {public static void main(String[] args) {// Obtain the random number 0 or 1int number = (int)(Math.random() * 2);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Guess head or tail? " +"Enter 0 for head and 1 for tail: ");int guess = input.nextInt();// Check the guessif (guess == number)System.out.println("Correct guess");else if (number == 0)System.out.println("Sorry, it is a head");elseSystem.out.println("Sorry, it is a tail");}}3.15public class Exercise03_15 {public static void main(String[] args) {// Generate a lotteryint lottery = (int)(Math.random() * 1000);// Prompt the user to enter a guessjava.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter your lottery pick (three digits): ");int guess = input.nextInt();// Get digitsint l1 = lottery / 100;int l2 = (lottery % 100) / 10; // l2 = (lottery / 10) % 10int l3 = lottery % 10;int g1 = guess / 100;int g2 = (guess % 100) / 10;int g3 = guess % 10;System.out.println("Lottery is " + lottery);// Check the guessif (guess == lottery)System.out.println("Exact match: you win $10,000");else if (g1 == l1 && g3 == l2 && g2 == l3 ||g2 == l1 && g1 == l2 && g3 == l3 ||g2 == l1 && g3 == l2 && g1 == l3 ||g3 == l1 && g1 == l2 && g2 == l3 ||g3 == l1 && g2 == l2 && g1 == l3)System.out.println("Match all digits: you win $3,000"); else if (g1 == l1 || g1 == l2 || g1 == l3 ||g2 == l1 || g2 == l2 || g2 == l3 ||g3 == l1 || g3 == l2 || g3 == l3)System.out.println("Match one digit: you win $1,000"); elseSystem.out.println("Sorry, no match");}}3.16public class Exercise03_16 {public static void main(String[] args) {double x = Math.random() * 100 - 50;double y = Math.random() * 200 - 100;System.out.println(x + ", " + y);}}3.17public class Exercise03_17 {public static void main(String[] args) {// Generate scissor, rock, paperint computerNumber = (int)(Math.random() * 3);// Prompt the user to enter scissor, rock, or paper java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("scissor (0), rock (1), paper (2): ");int userNumber = input.nextInt();// Check the guessswitch (computerNumber) {case 0:if (userNumber == 0)System.out.print("The computer is scissor. You are scissor too. It is a draw");else if (userNumber == 1)System.out.print("The computer is scissor. You are rock. You won");else if (userNumber == 2)System.out.print("The computer is scissor. You are paper. You lost");break;case 1:if (userNumber == 0)System.out.print("The computer is rock. You are scissor. You lost");else if (userNumber == 1)System.out.print("The computer is rock. You are rock too. It is a draw");else if (userNumber == 2)System.out.print("The computer is rock. You are paper. You won");break;case 2:if (userNumber == 0)System.out.print("The computer is paper. You are scissor. You won");else if (userNumber == 1)System.out.print("The computer is paper. You are rock. You lost");else if (userNumber == 2)System.out.print("The computer is paper. You are paper too.It is a draw");break;}}}3.18public class Exercise03_18 {public static void main(String args[]) {Scanner input = new Scanner(System.in);System.out.print("Enter package weight: ");double w = input.nextDouble();if (w <= 1) {System.out.println("The shipping cost is $3.5");}else if (w <= 3) {System.out.println("The shipping cost is $5.5");}else if (w <= 10) {System.out.println("The shipping cost is $8.5");}else if (w <= 20) {System.out.println("The shipping cost is $10.5");}else {System.out.println("The package cannot be shipped");}}}3.19public class Exercise03_19 {public static void main(String[] args) {java.util.Scanner input = new java.util.Scanner(System.in); // Enter three edgesSystem.out.print("Enter three edges (length in double): ");double edge1 = input.nextDouble();double edge2 = input.nextDouble();double edge3 = input.nextDouble();。
java Chapter3练习

第 3 章面向对象程序设计一、判断题1.()下面程序输出数字0。
2.()Java语言为所有的Java程序自动导入包“ng”,因此Java程序可以直接用“ng”中的类和接口。
位于类继承关系层次结构树的根部的类Object就是在包“ng”中的一个类。
3.()有时候为了避免引起混淆,构造方法的方法名可以不与所属类名同名。
4.()构造方法一般不允许有任何返回值,因此需要在返回类型处标注为void。
5.()任何类都必须显示地定义该类的构造方法,以便对类的成员进行各种初始化操作。
6.()运行下面程序将在控制台窗口中输出a。
7.()下面的程序将输出“Creating Rock 2”。
8.()Java语言对内存的释放是采用垃圾自动回收机制。
Java虚拟机自动判断并收集“垃圾”,但一般不会立即释放它们的存储空间。
9.()Java系统提供了方法“System.gc()”来强制立即回收“垃圾”,即当调用该方法时,系统会立即回收“垃圾”。
10.()程序可以借助于finalize方法来精确记录在程序执行过程中内存资源的释放情况。
11.()如果在定义一个类的时候没有用到关键字extends,则这个类没有直接父类。
12.()Java语言规定:任何一类的构造方法必须调用其父类的构造方法(包括隐式调用),并且调用父类构造方法的语句必须是子类构造方法的第一条语句。
13.()在同一类中,不允许存在具有相同方法名和相同参数类型列表的方法,即使该方法的返回类型不相同。
14.()如果一个类不是内部类,则它的访问属性不能是private或者protected。
15.()抽象方法不能含有方法体,并且必须在抽象类中。
16.()抽象类是不能实例化的。
抽象类的实例化,应当通过其不具有抽象属性的子类来创建。
17.()下面的程序输出整数3。
18.()设在一个java包.tsinghua.kit中含源程序文件J_Data.java,其内容如下:在另一个包中含有源程序Exer3_1_18.java,其内容如下:假设类路径设置及源程序文件所在的路径都是正确的,则上面的程序可以正常进行编译与运行,最终可以输出3。
java认证 习题 第03章 有答案版 OK 该试题还有第02、04章

1、class Scoop{static int thrower() throws Exception { return 42; }public static void main(String[] args) {try{int x = thrower();}catch(Exception e) {x++;}finally {System.out.println(“x = ” + ++x);}What is the result?A. x = 42B. x = 43C. x = 44D. Compilation failsE. The code runs with no output答案:D变量x的作用域只在try块内,而不在catch和finally里2、what is the result when you compil and run the following code? D class Example{static int myArg=1;public static void main(String [] args){Int myArg;System.out.println(myArg);}}Select all right answer:A、this code compiles and displays 0 in the statndard output when runB、t his code compiles and displays 1 in the statndard output when runC、t his code does not compile because you can‟t define a local variable names the same as staticvariableD、this code doesn‟t compile because the local vriable is used before it is initialized局部变量myArg;在使用前必须被初始化3、what is the result when you compile and run the following code? D class Example{static int i;public static void main(String [] args){System.out.println(i);}}Select all right answer:A、Error variable I may not have been initializedB、N ullC、1D、0属性有默认值4、what is the result when you compile and run the following code? B class Example{static boolean Paddy;public static void main(String [] args){System.out.println(Paddy);}}Select all right answer:A、compile time error;B、c ompilation and output of false;C、c ompilation and output of trueD、compilation and output of null属性有默认值5、what is the result when you compile and run the following code? D class Example{public static void main(String [] args){ int i=012;int j=034;int k=056;int l=078;System.out.println(i);System.out.println(j);System.out.println(k);}}Select all right answer:A、p rints 12,34 and 56B、prints 24,68and 112C、prints 10,28and 46D、c ompilation error八进制数前加0,但l表示的数超过八进制数的范围了6、what is the result when you compile and run the following code? Aclass Example{ void fun(){static int i=0;}public static void main(String [] args){ Example obj=new Example();obj.fun();obj.fun();}}Select all right answer:A、compilation errorB.run time errorC、1D、2Static不能修饰局部变量7、what is the result when you compile and run the following code? D class Example{public static void main(String [] args){ String elements[ ]={“for”,”tea”,”too”};String first=(elements.length>0)?elements[0]:null;System.out.println(first);}}Select all right answer:pilation fails;B.an exception thrown at runtimeC.prints:nullD.prints:for考察数组的length属性和条件运算符8、what is the result when you compile and run the following code? A,C,D String s=“hello”;String t=”hello”;char c[]={…h‟,‟e‟,‟l‟,‟l‟,‟o‟};Select all right answer:A s.equals(t);B t.equals(c)C s==tD t.equals(new String (“hello”))E t==c==比较的是两端操作数是否是同一个对象,由于s和t并非用new创建,因此指向内存池中同一字符串常量,故C对,equals比较两个String内容是否相等9、Given the following code: E class Example{public static void main(String [] args){If(args.length==1 | args[1].equals(“test”))System.out.println(“test case”);ElseSystem.out.println(“production ”+arg[0]);}}And the command-line invocation: java Example live2What is the result?select all right answer?A、test caseB、productionC、test case live2D、complilation failsE、An xception is throw at runtime考察逻辑短路运算,但|不属于逻辑短路,尽管args.length==1的结果为true,但后边的仍需计算,但因为数组只接受了一个参数,故arg[1]导致异常10、what is the result when you compile and run the following code? Cclass Example{public static void main(String [] args){ byte B=10;byte D=12;byte I=B*D;}}Select all right answer:A、the code will compile and runB、c ompile time error while declaring variableC、c ompile time error while multiplicationD、none of the above说明:二元运算,若一个为float double 或long,则另一个也转换成对应的,否则都转换成int11、what is the result when you compile and run the following code? Dclass Example{void test(int i){ System.out.println(“I am a Int”);}void test(String s){ System.out.println(“I am a String”);}public static void main(String [] args){ Example t=new Example();char ch=‟y‟;t.test(ch);}}Select all right answer:A not compile,because void methods can‟t be overriddenB not compile,because there is no version of test() that rakes a char argumentC the code will complile but will throw an exceptionD the code will compile and produce the following output: I am an int因为char的表示范围小于int,所以和test(int i)匹配上12、what is the result when you compile and run the following code? Dclass Example{static final long tooth=343L;static long doIt(long tooth){System.out.println(++tooth+””);return ++tooth;}public static void main(String [] args){ System.out.println(tooth+”“);final long tooth=340L;new Example().doIt(tooth);System.out.println(tooth);}}Select all right answer:A 343 340 340B 343 340 342C 343 341 342D 343 341 340E 343 341 343F compilation failsG An exception in thrown at runtime本题考察变量的使用范围:三个tooth同名变量,第一个是类成员,第二个是形参在方法内doIt().第三个是main的局部变量,范围在main内。
JAVA Chapter3习题

1. 一个类可以生成多个对象,并且这些对象都具有相同的属性。
A. 错误B. 正确2. 对于构造函数,下列叙述正确的是()。
A. 构造函数是类的一种特殊函数,它的方法名必须与类名相同。
B. 构造函数的返回类型只能是void型。
C. 构造函数的主要作用是完成对类的对象的初始化工作。
D. 一般在创建新对象时,系统会自动调用构造函数3. 有的类定义时可以不定义构造函数,所以构造函数不是必需的。
A. 错误B. 正确4. 即使一个类中未显式定义构造函数,也会有一个缺省的构造函数,缺省的构造函数是无参的,函数体为空。
A. 错误B. 正确5. 同一个类中定义多个参数列表不同的同名方法,叫做方法的重载。
A. 错误B. 正确6. 为了区分重载多态中同名的不同方法,要求( )。
A. 形式参数个数或者类型不同B. 返回值类型不同C. 调用时用类名或对象名做前缀D. 形式参数名称不同7.Which two overload the ConstOver constructor? (Choose Two)A.ConstOver ( ) { }B.protected int ConstOver ( ) { }C.private ConstOver (int z, int y, byte x) { }D.public Object ConstOver (int x, int y, int z) { }E.public void ConstOver (byte x, byte y, byte z) { }8.Given:1.public class MethodOver {2. public void setVar (int a, int b, float c) {3. }4.}Which two overload the setVar method? (Choose Two)A.private void setVar (int a, float c, int b) { }B.protected void setVar (int a, int b, float c) { }C.public int setVar (int a, float c, int b) {return a;}D.public int setVar (int a, int b, float c) {return a;}E.protected float setVar (int a, int b, float c) {return c;}9. 编译并运行下面的程序,运行结果为( ). public class T1 {public static void main (String[] args){T1 a=new T1();a.method(8);a.method(1.2f);}void method(float i) {System.out.println("float: "+i);}void method(long i) {System.out.println("long: "+i);}}A. 程序有编译错误,因为两个method()方法必须定义为静态(static)的。
java第三章上机练习题

习题1. 数据类型转换应用【案例简介】下例说明了整型、浮点型、字符串型之间的相互转换。
【案例目的】掌握Java中两个不兼容类型能够的数据格式之间的转换。
【技术要点】高精度到低精度的数据转换时候需要进行强制转换,低精度向高精度进行转换的时候的会自动进行,字符串到其他类型的转换需要用到特定的方法。
【代码分析】public class Conversation{public static void main(String [] args){String str = "123";int j;byte b;int i=257;double d = 323.142;System.out.println("\nConversion of int to byte.");//i强制转换成byte型System.out.println("i and b "+ i + " "+b);System.out.println("\nConversion of double to int.");//d强制转换成int型System.out.println("d and i "+ d + " "+i);//d强制转换成byte型System.out.println("d and b "+ d + " "+b);j=Integer.parseInt(str); //str转换成int型System.out.println("j="+j);}}【相关知识】数据类型转换(1)简单数据类型中各类型数据间的优先关系和相互转换不同类型数据间的优先关系如下低----------------------------------------------->高byte->short->char-> int -> long -> float -> double自动类型转换规则整型,实型,字符型数据可以混合运算。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
eh第三章练习题练习题10.public class rain1 {public static void main(String[] args) {int y,x,z,j,t;j=kuku(3,-2,7);t=kuku(19,27,6);System.out.println(j);System.out.println(t);}public static int kuku(int x,int y,int z){int c,b;c=Math.min(x,y);b=Math.min(c,z);return b;}}13题public class rain1 {public static void main(String[] args) {String a,b,t;String quote="Four Score and seven years ago";a=quote.substring(5,10).toUpperCase();b=quote.toLowerCase().substring(0,4)+quote.substring(20,26);System.out.println(a);System.out.println(b);}}18题import java.util.*;public class rain1 {public static void main (String[] args){System.out.print("请输入合法的整数值");Scanner n =new Scanner(System.in);int t=n.nextInt();int l;l=t*2;System.out.print("你输入的数值N*2倍后是"+l);}}19题import java.util.*;public class rain1 {public static void main (String[] args){System.out.println("请输入你喜欢的语句");Scanner n = new Scanner(System.in);String p=n.nextLine();System.out.println("你希望重复几次");Scanner y=new Scanner(System.in);int u=y.nextInt();for(int k=1;k<=u;k++){System.out.println(p);}}}练习题1题import java.util.*;public class rain1 {public static void main (String[] args){System.out.println("请输入整数值");Scanner r=new Scanner(System.in);int t=r.nextInt();System.out.println("请再输入一个整数值");int u=r.nextInt();kk(t,u);}public static void kk(int t,int u){for(int i=1;i<=t;i++){System.out.print("["+i+"]"+" ");}System.out.println();for(int k=1;k<=u;k++){System.out.print("["+k+"]"+" ");}System.out.println();}}2题import java.util.*;public class rain1 {public static void main (String[] args){ System.out.println("请输入整数值");Scanner r=new Scanner(System.in);int t=r.nextInt();System.out.println("请再输入一个整数值");int u=r.nextInt();kk(t,u);}public static void kk(int t,int u){for(int i=0;i<=t;i++){double r;r=Math.pow(t,i);System.out.print((int)r+" ");}System.out.println();for(int k=0;k<=u;k++){double i;i=Math.pow(u,k);System.out.print((int)i+" ");}System.out.println();}}5题import java.util.*;public class rain1 {public static void main (String[] args){ System.out.println("第一个整数值");Scanner t=new Scanner(System.in);int r=t.nextInt();System.out.println("请再输入第二个整数值");int u=t.nextInt();System.out.println("请再输入第三个整数值");int g=t.nextInt();kk(r,u,g);}public static void kk(int r,int u,int g){ double k,l,o;k=Math.max(r,u);o=Math.max(k,g);System.out.print("最大值为"+(int)o);}}6题import java.util.*;public class rain1 {public static void main (String[] args){ System.out.println("请输入系数a");Scanner t=new Scanner(System.in);int a=t.nextInt();System.out.println("请再输入系数b");int b=t.nextInt();System.out.println("请再输入系数c");int c=t.nextInt();kk(a,b,c);}public static void kk(int a,int b,int c){double y,z;y=(-b+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;z=(-b-Math.sqrt(Math.pow(b,2)-4*a*c))/2*a;System.out.print("x1的值是"+(int)y+" ,x2的值是"+(int)z);}}8编写vertical方法,它接受一个字符串作为参数。
然后将字符串中的每个字母单独显示在一起中。
例如,调用vertical("hey now")的结果为:heynowpublic void Crital(String str){int index=0;System.out.println();String strB="";String lastStr="";for(int i=0;i<str.length();i++){strB=str.substring(index,index+i+1);lastStr=strB.substring(strB.length()-1,strB.length());System.out.println(lastStr);}}注意要代码简化,美观public static void padString(String k){char f;for(int i=0;i<k.length();i++){f=k.charAt(i);System.out.println(f);}}我自己写的import java.util.*;public class rain1 {public static void main (String[] args){System.out.println("请输入语句");Scanner r=new Scanner(System.in);String k=r.nextLine();padString(k);}public static void padString(String k){int s,l;char f;s=k.length();l=s-1;for(int i=0;i<=l;i++){f=k.charAt(i);System.out.println(f);}}}编写swapPoints方法,它的参数是两个Point对象。
它的功能是交互这两个Point对象的值。
对于下面代码:Point p1=new Point(5,2);Point p2=new Point(-3,6);swapPoints(p1,p2);System.out.println("("+p1.x+","+p2.y+")");System.out.println("("+p1.x+","+p2.y+")");输出结果为(-3,6)(5,2)import java.awt.*;public class rain1 {public static void main (String[] args){Point p1=new Point(5,2);Point p2=new Point(-3,6);swapPoints(p1,p2);System.out.println("("+p1.x+","+p2.y+")");System.out.println("("+p1.x+","+p2.y+")");}public static void swapPoints(Point p1,Point p2){Point tmp=p1;p1=p2;p2=tmp;System.out.println(p1);System.out.println(p2);}}11.编写程序提示用户输入他/她的姓名,然后将姓名逆序输出(先输出名,然后输出姓)下面是一个例子Please enter your full name:Sammy JnkisYour name in reverss order is Jankis,Sammyimport java.util.*;public class rain1 {public static void main (String[] args){System.out.print("请输入你的姓名");Scanner n=new Scanner(System.in);String f=n.nextLine();kk(f);}public static void kk(String f){int l;l=f.indexOf(" ");String y,m;y=f.substring(l+1,f.length());m=f.substring(0,l);System.out.print("输出结果是"+y+" "+m);}}。