JAVA第三章习题1
Java第三章--习题
![Java第三章--习题](https://img.taocdn.com/s3/m/1aa667899ec3d5bbfd0a74f6.png)
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第3章例题
![JAVA第3章例题](https://img.taocdn.com/s3/m/a6b7323f31126edb6f1a10c9.png)
import javax.swing.*;
public class Example3_5
{
static double distance(double x , double y)
{
double d=Math.sqrt(x*x+y*y);
{
public static void main(String args[])
{
Box box = new Box();
double v;
v = box.volume();
System.out.println("长方体体积为: " + v);
b1.prnt(3, 5); // prnt()是实例对象b1父类A的方法,b1继承了该方法
}
}
【例3-11】子类重写了父类的方法,则在运行时,系统调用子类的方法。
/* 子类重写了父类的方法 */
import java.io.*;
class A
int max(int x, int y)
{
if(x > y) return x;
else return y;
}
【例3-3】方法调用示例,计算1 + 2 + 3 + …… + 100的和。
import javax.swing.*;
public class Example3_3
System.exit(0);
}
static float area(int x, int h)
{
float s;
s = (x * h) / 2;
明解JAVA第三章答案
![明解JAVA第三章答案](https://img.taocdn.com/s3/m/b132f9dc81eb6294dd88d0d233d4b14e85243e61.png)
明解JAVA第三章答案练习3-1package candle1220;import java.util.Scanner;public class Nightwatch {public static void main(String[] args){Scanner stdIn=new Scanner(System.in);System.out.print("整数值");int n=stdIn.nextInt();if(n>0)System.out.println("其绝对值是"+(n));elseSystem.out.println("其绝对值是"+(-n));}}练习3-2package candle1220;import java.util.Scanner;public class Nightwatch {public static void main(String[] args){Scanner stdIn=new Scanner(System.in);System.out.print("整数A:");int a=stdIn.nextInt();System.out.print("整数B:");int b=stdIn.nextInt();if(a%b==0)System.out.println(b+"是"+a+"的约数");elseSystem.out.println(b+"不是"+a+"的约数");}}练习3-3同样可以运⾏练习3-4import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in);System.out.print("a:");int a=input.nextInt();System.out.print("b:");int b=input.nextInt();if(a>b)System.out.print("a更⼤");elseif(a==b)System.out.print("⼀样⼤");elseSystem.out.print("b更⼤");}}练习3-5import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in);System.out.print("请输⼊⼀个正整数:");int a=input.nextInt();if(a<=0)System.out.print("请输⼊正整数");else if(a%5==0)System.out.print("该值可以被5整除");elseSystem.out.print("该值不可以被5整除"); }}练习3-6import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in);System.out.print("请输⼊⼀个正整数:");int a=input.nextInt();if(a<=0)System.out.print("请输⼊正整数");else if(a%10==0)System.out.print("该值是10的倍数");elseSystem.out.print("该值不是10的倍数"); }}练习3-7import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in);System.out.print("请输⼊⼀个正整数:");int a=input.nextInt();if(a<=0)System.out.print("请输⼊正整数");else if(a%3==0)System.out.print("该值可以被3整除");else if(a%3==1)System.out.print("该值除以3余1");elseSystem.out.print("该值除以3余2");}}练习3-8import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in);System.out.print("请输⼊分数:");int a=input.nextInt();if(a>=0&&a<60){System.out.println("不及格");}else if(a>=60&&a<70){System.out.println("及格");}else if(a>=70&&a<80){System.out.println("良");}else if(a>=80&&a<=100){System.out.println("优");}else{System.out.println("请输⼊合理的分数");} }}练习3-9import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in); System.out.print("a=:");int a=input.nextInt();System.out.print("b=:");int b=input.nextInt();System.out.print(a>b?a:b);}}练习3-10import java.util.Scanner;public class New{public static void main(String[] args){Scanner input =new Scanner (System.in); System.out.print("a=");int a=input.nextInt();System.out.print("b=");int b=input.nextInt();if(a>=b)System.out.print(a-b);elseSystem.out.print(b-a);}}练习3-11import java.util.Scanner;public class New{public static void main(String[] args){Scanner input =new Scanner (System.in); System.out.print("a=");int a=input.nextInt();System.out.print("b=");int b=input.nextInt();int c=a-b;if(c>10||c<-10)System.out.print("它们的差值⼤于等于11");elseSystem.out.print("它们的差值⼩于等于10"); }}练习3-12import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner(System.in); System.out.print("a=");int a =input.nextInt();System.out.print("b=");int b =input.nextInt();System.out.print("c=");int c =input.nextInt();int min1=a<b?a:b;int min=min1<c?min1:c;System.out.print(min);}}练习3-13import java.util.Scanner;public class New{public static void main(String[] args){Scanner input=new Scanner (System.in); System.out.print("a=");int a =input.nextInt();System.out.print("b=");int b =input.nextInt();System.out.print("c=");int c =input.nextInt();int mid1=a<b?b:a;int mid=mid1<c?mid1:c;System.out.print(mid);}}练习3-14// 计算两个整数值中较⼩的值和较⼤的值import java.util.Scanner;class New {public static void main(String[] args) {Scanner stdIn = new Scanner(System.in); System.out.print("整数a:");int a = stdIn.nextInt();System.out.print("整数b:");int b = stdIn.nextInt();if(a==b){System.out.print("两个整数相等");}else{int min, max; // 较⼩的值/较⼤的值if (a < b) { // 如果a⼩于bmin = a;max = b;}else{ // 否则min = b;max = a;}System.out.println("较⼩的值是" + min + "。
java简明教程第三版2-7单元答案
![java简明教程第三版2-7单元答案](https://img.taocdn.com/s3/m/f51745601ed9ad51f01df22d.png)
第2章习题4角谷猜想:任何一个正整数n,如果它是偶数则除以2,如果是奇数则乘以3加上1,这样得到一个新的整数,如继续进行上述处理,则最后得到的数一定是1。
编写应用程序和小程序程序分别证明:所有的3至10000的数都符合上述规则。
******************************************************************************* *************/// 程序清单/*方法1:采用小程序*/import java.awt.*;import java.util.*;import java.applet.Applet;public class jiaogu extends Applet{public void paint(Graphics g){int x,n,i=10;for(x=3;x<=10;x++){n=x;while(n!=1)if(n%2==0) n=n/2;else n=n*3+1;g.drawString (x+" 符合角谷猜想!",25,25+i);i=i+15;}}}/** 方法2: 采用应用程序*/class jiaogu{public static void main(String args[]){int x,n,i;for(x=3;x<=1000;x++){n=x;while(n!=1)if(n%2==0) n=n/2;else n=n*3+1;System.out.println(x+" 符合角谷猜想!");}}}/****************************************************************************** **************第2章习题5编写一个applet,要求输入2个整数,在状态条显示较大的数,紧跟着显示“is larger”。
java面向对象第三章课后习题
![java面向对象第三章课后习题](https://img.taocdn.com/s3/m/36d78103a31614791711cc7931b765ce05087aab.png)
java⾯向对象第三章课后习题 1、输⼊⼀批整数,输出其中的最⼤值与最⼩值,输⼊为0时结束循环。
代码如下:package com.bd22;import java.util.Scanner;public class Integer {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int min =0;int max =0;do {System.out.println("");System.out.println("请输⼊⼀个整数(输⼊0结束):");int a = sc.nextInt();if(a==0) {break;}min=min<a?min:a;max=max>a?max:a;}while(true);System.out.println("最⼩值为:"+min);System.out.println("最⼤值为:"+max);}}运⾏结果: 2、⽤键盘输⼊⼀位整数,当输⼊1~7时,显⽰对应的英⽂星期名称的缩写,输⼊其他数字时提⽰⽤户重新输⼊,输⼊0时结束程序。
程序代码:package com.bd22;import java.util.Scanner;public class Week {public static void main(String[] args) {Scanner input = new Scanner(System.in);while(true){System.out.print("请输⼊数字1-7(输⼊0时结束):");int num = input.nextInt();//如果输⼊0则程序结束退出if(num==0) {System.out.println("程序结束!");break;}//按照输⼊的数不同选择输出的星期switch(num) {case 1:System.out.println("今天是 MON");break;case 2:System.out.println("今天是 TUE");break;case 3:System.out.println("今天是 WED");break;case 4:System.out.println("今天是 THU");break;case 5:System.out.println("今天是 FRI");break;case 6:System.out.println("今天是 SAT");break;case 7:System.out.println("今天是 SUN");break;default:System.out.println("请重新输⼊");break;}}}}运⾏结果: 3、假如机票原价为5000元,4-10⽉份为旺季,旺季头等舱打9折,经济舱打6折,其他⽉份为淡季,淡季头等舱打5折,经济舱打4折。
java第三章作业答案
![java第三章作业答案](https://img.taocdn.com/s3/m/1f9290f0ccbff121dc36830d.png)
习题三一、问答题1.面向对象语言有哪三个特性?答:封装性,继承性,多态性。
2.类体内容中声明成员变量是为了体现对象的属性还是行为?答:属性。
3.类体内容中定义的非构造方法是为了体现对象的属性还是行为?答:行为。
4.什么时候使用构造方法?构造方法有类型吗?答:用类创建对象时使用构造方法。
没有类型。
5.类中的实例变量在什么时候会被分配内存空间?答:用类创建对象时会被分配内存空间。
6.什么叫方法的重载?构造方法可以重载吗?答:一个类中可以有多个方法具有相同的名字,但这些方法的参数必须不同,即是参数的个数不同,或者是参数的类型不同。
可以。
7.类中的实例方法可以操作类变量(static变量)吗?类方法(static方法)可以操作实例变量吗?答:可以。
不可以。
8.类中的实例方法可以用类名直接调用吗?答:不可以。
9.简述类变量和实例变量的区别。
答:一个类通过使用new运算符可以创建多个不同的对象,不同的对象的实例变量将被分配不同的内存空间。
所有对象的类变量都分配给相同的一处内存,对象共享类变量。
二、选择题1.下列哪个叙述是正确的?BA.Java应用程序由若干个类所构成,这些类必须在一个源文件中。
B.Java应用程序由若干个类所构成,这些类可以在一个源文件中,也可以分布在若干个源文件中,其中必须有一个源文件含有主类。
C.Java源文件必须含有主类。
D.Java源文件如果含有主类,主类必须是public类。
2.下列哪个叙述是正确的?DA.成员变量的名字不可以和局部变量的相同。
B.方法的参数的名字可以和方法中声明的局部变量的名字相同。
C.成员变量没有默认值。
D.局部变量没有默认值。
3.对于下列Hello类,哪个叙述是正确的?DA.Hello类有2个构造方法。
B.Hello类的int Hello()方法是错误的方法。
C.Hello类没有构造方法。
D.Hello无法通过编译,因为其中的hello方法的方法头是错误的(没有类型)。
JAVA期末复习题及答案——第三章
![JAVA期末复习题及答案——第三章](https://img.taocdn.com/s3/m/f6f2f8ab90c69ec3d5bb75f8.png)
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程序设计第三章课后答案
![新编解析JAVA程序设计第三章课后答案](https://img.taocdn.com/s3/m/2774a3062f60ddccda38a04e.png)
第3章习题解答1.如何定义方法?在面向对象程序设计中方法有什么作用?答:方法的定义包括方法名、方法形参、方法的返回值类型和方法体四部分,方法只能在类中定义。
方法是对象的动态特征的描述,对象通过方法操作属性,进而改变对象的状态,完成程序所预期的功能。
2.定义一个Dog类,有名字、颜色、年龄等属性,定义构造方法用来初始化类的这些属性,定义方法输出Dog的信息。
编写应用程序使用Dog。
答:public class Dog{private String name;private String color;private String age;Dog(String n,String c,String a){name = n; color = c; age = a;}public String toString() {return name + "," + color + "," + age;}public static void main(String args[]) {Dog dog = new Dog("小白", "白色", "2岁");System.out.println(dog.toString());}}3.什么是访问控制修饰符?修饰符有哪些种类?它们各有何作用?答:访问控制修饰符是对类、属性和方法的访问权限的一种限制,不同的修饰符决定了不同的访问权限。
访问控制修饰符有3个:private、protected、public,另外还有一种默认访问权限。
各个修饰符的作用如下表所示:B:包中的类C:所有子类D:本类A:所有类4.阅读程序,写出程序的输出结果class A{private int privateVar;A(int _privateVar){privateVar=_privateVar;}boolean isEqualTo(A anotherA){if(this.privateVar == anotherA.privateVar)return true;elsereturn false;}}public class B{public static void main(String args[]){ A a = new A(1);A b = new A(2);System.out.println(a.isEqualTo(b));}}程序的输出结果为:false5.阅读程序,写出程序的输出结果public class Test {public static void main(String[] args) {int x;int a[] = { 0, 0, 0, 0, 0, 0 };calculate(a, a[5]);System.out.println("the value of a[0] is " + a[0]);System.out.println("the value is a[5] is " + a[5]);}static int calculate(int x[], int y) {for (int i = 1; i < x.length; i++)if (y < x.length)x[i] = x[i - 1] + 1;return x[0];}}程序的输出结果为:the value of a[0] is 0the value is a[5] is 56.阅读程序,写出程序的输出结果public class Test {public static void main(String[] args) {String str1 = new String("Java");String str2 = new String("Java");System.out.println(str1 == str2);}}程序的输出结果为:false7.阅读下列程序,程序中已经指明错误位置,请说出错误原因。
java 课后 第三章
![java 课后 第三章](https://img.taocdn.com/s3/m/3725bec0bb4cf7ec4afed01e.png)
3.21子类可以访问父类中什么样权限的成员?
答:
子类能够访问父类的公有成员和保护成员,还可以访问当前包中父类的缺省权限的成员。
3.1什么是类?什么是对象?它们之间的关系是怎样的?
答:
类是数据以及对数据的一组操作的封装体。类定义是java的核心,每一个java程序都是个类定义。
对象是累的实例,即类的取值。
关系:对象属于类,类本身不能运行,实际参与运行的是类的对象。
3.2作为引用数据类型,对象在复制和方法的参数传递方面与基本数据类型的变量有什么不同?
3.6类的构造方法和折构方法有什么作用?它们分别被谁调用?它们的访问权限范围是怎样的?是否每个类都必须声明构造方法和折构方法?没有声明构造方法和折构法的类执行什么构造方法和折构方法?构造方法和折构方法的继承性是怎么样的?
答:
类的构造方法用于创建类的一个实例并对实例的成员变量进行初始化,构造方法与类同名,通过new运算符调用。
答:
封装是面向对象的重要概念之一,它是面向对象程序设计中的信息隐藏思想的具体实现技术。
第一,将数据和对数据的操作组合起来构成类,对象是一个不可分割的独立单位;
第二,实现信息隐藏,类既要提供与外部联系的方法又要尽可能的隐藏实现细节。
封装提供软件模块化的设计机制。一个类的声明具有特定功能,其中包括多个实现特定功能的软件模块;一个软件系统,根据实际需求选择多个类中的功能模块进行组装,各模块减通过参数传递进行配合,协同工作。Java程序设计的基本单位是类,类给出了对象的结构;程序设计的任务就是设计类,又类组装程序。
答:
继承是面向对象的重要概念之一,它提供在已有类的基础上创建新类的方式,是构造可复用软件的有效机制。
Java第三章习题
![Java第三章习题](https://img.taocdn.com/s3/m/ecc407fd4b73f242326c5f8b.png)
第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章 习题参考答案
![java习题及答案第3章 习题参考答案](https://img.taocdn.com/s3/m/25c18a1ed0d233d4b14e69ee.png)
第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/87f93c0cec630b1c59eef8c75fbfc77da2699762.png)
第3章作业参考答案
1.填空题(1)类
(2)属性new
(3)thisstatic
2.选择题
简答题
(1)回答要点
面向对象的方法主要是把事物给对象化,包括其属性和行为。
面向对象编程更贴近实际生活的思想。
总体来说面向对象的底层还是面向过程,面向过程抽象成类,然后封装,方便使用就是面向对象(万物皆对象)。
(2)回答要点
•格式区别
构造方法和类名相同,并且没有返回类型,也没有返回值。
普通成员方法可以任意起名,必须有返回类型,可以没有返回值。
•作用区别
构造方法用于创立对象,并进行初始化值。
普通成员方法是用于完成特定功能的。
•调用区别
构造方法是在创立对象时被调用的,一个对象建立,只调用一次相应构造函数。
普通成员方法是由创立好的对象调用,可以调用屡次。
(3)回答要点
Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定时的方式动态回收无任何引用的对象占据的内存空间,回收之前会自动调用finalize方法,用于清理资源。
(4)回答要点
对象是类的实例,类是对象的模板
4.编程题
(1)源代码:参考本章资料文件夹下“作业1”。
(2)源代码:参考本章资料文件夹下“作业2”。
Java语言程序设计(郑莉)第三章课后习题答案
![Java语言程序设计(郑莉)第三章课后习题答案](https://img.taocdn.com/s3/m/63f803611611cc7931b765ce0508763230127457.png)
Java语言程序设计(郑莉)第三章课后习题答案1.设N为自然数:n!=1*2*3*….*n称为n的阶乘,并且规定0!=1.试编程计算2!,4!,6!he 10!.并将结果输出到屏幕上。
答:public class Mul{public static void main(String args[]){int i,n;float s;for(n=0;n<=10;n=n+2){if(n==0)System.out.println("0!=1\n");else{s=1;for(i=1;i<=n;i++)s=s*i;System.out.println(n+"!="+s+"\n");}}}}2.编写程序,接收用户从键键盘上输入的三个整数x,y,z..从中选出最大和最小者,并编程实现。
答:public class Math{public static void main(String args[]){int[] IntArg = new int[args.length];for(int i=0;i<args.length;i++){IntArg[i] = Integer.parseInt(args[i]);}int max,min;max=IntArg[0]>IntArg[1]?IntArg[0]:IntArg[1];max=max>IntArg[2]?max:IntArg[2];min=IntArg[0]<IntArg[1]?IntArg[0]:IntArg[1];min=min<IntArg[2]?min:IntArg[2];System.out.println("max="+max);System.out.println("min="+min);}}3.求出100一枚的宿舍,并将这些数在屏幕上5个乙杭地显示出来。
JAVA第三章课后习题解答
![JAVA第三章课后习题解答](https://img.taocdn.com/s3/m/690a3efb0975f46527d3e1b2.png)
System.out.println(s); return s; } }
public class TestStudent { public static void main(String args[]) { student stu = new student("0401398", "杨小明", "男",20); stu.printInfo();
【13】什么是接口?为什么要定义接口?接口和类有什么异同? [解答]:接口是用来调节各类之间的相互关系的一种结构,接口是抽象类的一种,只包含常 量和方法的定义,而没有变量和具体方法的实现,且其方法都是抽象方法。 接口定义的格式如下:
[public] interface 接口名 [extends 父接口名列表]{ … //接口体 } extends 子句有类声明的 extends 子句基本相同,不同的是一个接口可有多个父接口,用 逗号隔开,而一个类只能有一个父类。Java 中的类只能实现单重继承,这虽然可以简化编程, 但毕竟没有完全实现面向对象的功能。定义接口的主要作用,就是帮助 Java 实现类间多重 继承的结构。而且接口还有以下好处:
4
● 接口可以被多个类所实现,这些类可以不相关,从而具有相同的行为。 ● 通过接口可以了解对象的行为,而无需了解对象所对应的类的结构。
JAVA第三章习题
![JAVA第三章习题](https://img.taocdn.com/s3/m/fe3f74d76f1aff00bed51e3c.png)
第三章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第三章](https://img.taocdn.com/s3/m/dbc05ad126fff705cc170ae7.png)
一、单项选择题(从下列各题四个备选答案中选出一个正确答案,并将其代号写在答题纸相应位置处。
答案错选或未选者,该题不得分。
每小题1分,共10分。
)1. 假设x = 1, y = -1, and z = 1. 下面哪项是对的? Bif (x > 0)if (y > 0)System.out.println("x > 0 and y > 0");else if (z > 0)System.out.println("x < 0 and z > 0");A. x > 0 and y > 0;B. x < 0 and z > 0;C. x < 0 and z < 0;D. 没有输出.2.分析下列代码: DCode 1:boolean even;if (number % 2 == 0)even = true;elseeven = false;Code 2:boolean even = (number % 2 == 0);A. Code 1有错.B. Code 2 有错.C. Code 1 和Code 2都错.D. Code 1 and Code 2 都对,而且Code 2 更好3. income = 4001, 正确的输出是什么: Aif (income > 3000) {System.out.println("Income is greater than 3000");}else if (income > 4000) {System.out.println("Income is greater than 4000");A. Income is greater than 3000B. Income is greater than 3000 ,Income is greater than 4000C. Income is greater than 4000D. Income is greater than 4000 followed by Income is greater than 30004. 执行下列代码后y的值是什么? Bx = 3;switch (x + 3) {case 6: y = 0;case 7: y = 1;default: y += 1;}A. 1B. 2C. 3D. 45. 正确的输出是什么?Bchar ch = 'a';switch (ch) {case 'a':case 'A':System.out.print(ch); break; case 'b':case 'B':System.out.print(ch); break; case 'c':case 'C':System.out.print(ch); break; case 'd':case 'D':System.out.print(ch);}A.abcdB. aC. aaD. ab6. 正确的输出是什么? D char ch = 'b';switch (ch) {case 'a':System.out.print(ch);case 'b':System.out.print(ch);case 'c':System.out.print(ch);case 'd':System.out.print(ch);}A. abcdB. bcdC. bD. bbb7. 分析代码::Dint x;double d = 1.5;switch (d) {case 1.0: x = 1;case 1.5: x = 2;case 2.0: x = 3;}A.程序有错误,因为缺少break语句B.程序有错误,因为缺少default caseC. switch 中的变量值不能为double.D. 没有错误.8. y的值为多少?Ax = 0;y = (x > 0) ? 10 : -10;A. -10B. 0C. 10D. 209. 分析下列代码:BCode 1:if (number % 2 == 0)even = true;elseeven = false;Code 2:even = (number % 2 == 0) ? true: false;Code 3:even = number % 2 == 0;A. Code 2 has a syntax error, because you cannot have true and false literalsin the conditional expression.B. Code 3 has a syntax error, because you attempt to assign number to even.C. All three are correct, but Code 1 is preferred.D. All three are correct, but Code 3 is preferred.10.程序中会打印多少次"Welcome to Java"?Bint count = 0;while (count < 9) {System.out.println("Welcome to Java");count++;}A. 8B. 9C. 10D. 1111.程序中会打印多少次"Welcome to Java"?B int count = 0;while (count++ < 9) {System.out.println("Welcome to Java");}A. 8B. 9C. 10D. 1112 .程序中会打印多少次"Welcome to Java"?Aint count = 0;while (++count < 9) {System.out.println("Welcome to Java");}A. 8B. 9C. 10D. 1113. 程序中会打印多少次"Welcome to Java"?Bint count = 0;do {System.out.println("Welcome to Java");count++;} while (count < 9);A. 8B. 9C. 10D. 1114. C 中有自然语言和java代码。
JAVA基础 第3章类与对象_练习题
![JAVA基础 第3章类与对象_练习题](https://img.taocdn.com/s3/m/9e765dc63c1ec5da51e27009.png)
第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版梁勇著)第三章练习题答案](https://img.taocdn.com/s3/m/1ee420722a160b4e767f5acfa1c7aa00b52a9daa.png)
《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();。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2. 利用 switch 语句将学生成绩分级, 当从键盘中输入学生成绩在 100~90 范围时, 输出 “优 秀” ,在 89~80 范围时输出“良好” ,在 79~70 范围时输出“中等” ,在 69~60 范围时输出 “及格” ,在 59~0 范围时输出“不及格” ,在其他范围时输出“成绩输入有误! ” 。 20 3.利用 for 循环,计算 1+3+7+……+(2 -1)的和。 4.已知 S 1
1 1 1 1 1 ,利用 while 循环编程求解 n=100 时的 S 值。 2 3 4 n 1 n
10 3
5.利用 do…while 循环,计算 1!+2!+3! +……+100!的和。 6.编程序,求
k
k 1
7.编写打印“九九乘法口诀表”的程序。 9.水仙花数是指其个位、十位和百位三个数的立方和等于这个三位数本身,求出所有的水 仙花数。 10.编写一个程序,接受用户输入的两个数据为上、下限,然后输出上、下限之间的所有素 数。
第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)4 6.下列循环体执行的次数是( ) 。 int x=10, y=30; do{ y -= x; x++; }while(x++<y--); (A)1 (B)2 (C)3 (D)4 7.已知如下代码: 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 后面的 的值, 再和各 语句后的值做比较。 3.if 语句合法的条件值是 类型。 4.continue 语句必须使用于 语句中。 5.break 语句有两种用途:一种从 语句的分支中跳出,一种是从 内部 跳出。 6.do - while 循环首先执行一遍 ,而 while 循环首先判断 。
7.与 C++语言不同,Java 语言不通过 语句实现跳转。 8.每一个 else 子句都必须和它前面的一个距离它最近的 子句相对应。 9.在 switch 语句中,完成一个 case 语句块后,若没有通过 break 语句跳出 switch 语句,则 会继续执行后面的 语句块。 10.在 for 循环语句中可以声明变量,其作用域是 。 四、编写程序 1.利用 if 语句,根据下列函数编写一个程序,当键盘输入 x 值时,求出并输出 y 的值。 y= x 3x-2 4x (x<=1) (1<x<10) (x>=10)