JAVA大学实用教程第二版习题答案.docx
《JAVA大学实用教程第二版习题答案.docx》由会员分享,可在线阅读,更多相关《JAVA大学实用教程第二版习题答案.docx(70页珍藏版)》请在冰豆网上搜索。
JAVA大学实用教程第二版习题答案
Java大学实用教程第二版课后答案
第一章作业题
1.
publicclassHello{
publicstaticvoidmain(Stringargs[]){
System.out.pintln(“早上好,goodMorning”);
}
}
2.
importjava.applet.*;
importjava.awt.*;
publicclassBoyextendsApplet{
publicvoidpaint(Graphicsg){
g.setColor(Color.blue);
g.drawString("你好,hello",12,30);
}
}
第二章作业题
1.
publicclassZuoYe2_1{
publicstaticvoidmain(Stringargs[]){
charc='а';
System.out.println("字母"+c+"在unicode表中的顺序位置:
"+(int)c);
System.out.println("字母表:
");
while(c<='я'){
System.out.print(""+c);
c=(char)(c+1);
}
}
}
2.
importjava.util.*;
publicclassZuoYe2_2{
publicstaticvoidmain(Stringargs[]){
Scannerreader=newScanner(System.in);
longchengji=1;
intm=0;
while(reader.hasNextInt()){
intx=reader.nextInt();
m=m+1;
chengji=chengji*x;
}
System.out.println(m+"个数的乘积为"+chengji);
}
}
第三章作业题
1.
importjava.util.*;
publicclassZuoYe3_1{
publicstaticvoidmain(Stringargs[]){
Scannerreader=newScanner(System.in);
doubley=0,x=0;
x=reader.nextDouble();
if(x<0)
y=-1+2*x;
elseif(x==0)
y=-1;
elseif(x>0)
y=-1+3*x;
System.out.println(y);
}
}
2.
publicclassZuoYe3_2{
publicstaticvoidmain(Stringargs[]){
intsum=0,m=3,n=7,a=1;
while(a<=1000){
if(a%m==0&&a%n==0)
sum=sum+a;
a++;
}
System.out.println("sum="+sum);
}
}
3.
publicclassZuoYe3_3
{
publicstaticvoidmain(Stringargs[])
{
longsum=0,a=8,item=a,n=10,i=1;
for(i=1;i<=n;i++)
{sum=sum+item;
item=item*10+a;
}
System.out.println(sum);
}
}
4.
publicclassZuoYe3_4
{
publicstaticvoidmain(Stringargs[])
{
doublesum=0,a=1,b=1,fuhao=1,item=a/b;
inti=1;
while(i<=1000)
{
sum=sum+fuhao*item;
i++;
fuhao=fuhao*(-1);
b=b+2;
item=a/b;
}
System.out.println("sum="+sum);
}
}
5.
publicclassZuoYe3_5
{
publicstaticvoidmain(Stringargs[])
{
doublesum=0,a=1;
inti=1;
while(i<=20)
{
if(i>=10)
sum=sum+a;
i++;
a=a*i;
}
System.out.println("sum="+sum);
}
}
8.
第四章作业题
1.
classDengCha{
intstart,d;
DengCha(){
}
DengCha(intstart,intd){
this.start=start;
this.d=d;
}
voidsetStart(ints){
start=s;
}
voidsetD(intd){
this.d=d;
}
intgetSum(intn){
intsum=0,i=1;
while(i<=n){
sum=sum+start;
start=start+d;
i++;
}
returnsum;
}
}
publicclassZuoYe4_1
{
publicstaticvoidmain(Stringargs[])
{
DengChashulie=newDengCha(2,3);
System.out.println(shulie.getSum(100));
shulie.setStart(10);
shulie.setD(5);
System.out.println(shulie.getSum(9));
}
}
2.
classLetter{
publicvoidprintLetter(){
for(charc='a';c<='z';c++)
System.out.print(""+c);
}
}
publicclassZuoYe4_2
{
publicstaticvoidmain(Stringargs[])
{
Letterp=newLetter();
p.printLetter();
}
}
3.
classSquareEquation{
doublea,b;
staticdoublec;
doubleroot1,root2;
booleanboo;
publicSquareEquation(doublea,doubleb,doublec){
this.a=a;
this.b=b;
SquareEquation.c=c;
if(a!
=0){
boo=true;
}
else{
boo=false;
}
}
publicvoidgetRoots(){
if(boo){
System.out.println("是一元2次方程");
doubledisk=b*b-4*a*c;
if(disk>=0){
root1=(-b+Math.sqrt(disk))/(2*a);
root2=(-b-Math.sqrt(disk))/(2*a);
System.out.printf("方程的根:
%f,%f\n",root1,root2);
}
else{
System.out.printf("方程没有实根\n");
}
}
else{
System.out.println("不是一元2次方程");
}
}
publicvoidsetCoefficient(doublea,doubleb,doublec){
this.a=a;
this.b=b;
SquareEquation.c=c;
if(a!
=0){
boo=true;
}
else{
boo=false;
}
}
}
publicclassZuoYe4_3{
publicstaticvoidmain(Stringargs[]){
SquareEquationequation1=newSquareEquation(4,5,1);
SquareEquationequation2=newSquareEquation(3,5,-7);
equation1.getRoots();
equation2.getRoots();
}
}
4.
importjava.util.Scanner;
classA{
intf(intm,intn){
if(m*n<0)
{System.out.println("有负数,程序退出");
System.exit(0);
}
if(m{inttemp=m;
m=n;
n=temp;
}
inta=m,b=n;
intr=m%n;
while(r!
=0)
{m=n;
n=r;
r=m%n;
}
returnn;
}
}
classB{
Aa;
B(){
a=newA();
}
intg(intm,intn){
inttemp=a.f(m,n);
returnm*n/temp;
}
}
publicclassZuoYe4_4
{
publicstaticvoidmain(Stringargs[])
{
Scannerreader=newScanner(System.in);
System.out.println("输入2个正整数,程序计算出它们的最大公约数和最小公倍数");
System.out.print("输入第一个整数:
");
intm=reader.nextInt();
System.out.print("输入第二个整数:
");
intn=reader.nextInt();
Aa=newA();
Bb=newB();
System.out.println(m+"和"+n+"的最大公约数是"+a.f(m,n));
System.out.println(m+"和"+n+"的最小公倍数是"+b.g(m,n));
}
}
5.
importjava.applet.Applet;
importjava.awt.*;
publicclassExample4_10extendsApplet
{
Buttonredbutton;
publicvoidinit()
{
redbutton=newButton("我是一个红色的按钮");
redbutton.setBackground(Color.red);
redbutton.setForeground(Color.white);
add(redbutton);
}
}
第五章作业题
1.
importjava.util.Scanner;
classA
{
publicintf(intm,intn)
{
if(m{
inttemp=m;
m=n;
n=temp;
}
intr=m%n;
while(r!
=0)
{
m=n;
n=r;
r=m%n;
}
returnn;
}
}
classBextendsA
{
publicintf(intm,intn)
{
intdivision=super.f(m,n);
return(m*n)/division;
}
}
publicclassZuoYe5_1
{
publicstaticvoidmain(Stringargs[])
{
Aa=newA();
Bb=newB();
Scannerreader=newScanner(System.in);
System.out.println("输入2个整数,程序计算出它们的最大公约数和最小公倍数");
System.out.print("输入第一个整数:
");
intm=reader.nextInt();
System.out.print("输入第二个整数:
");
intn=reader.nextInt();
if(m*n<0)
{System.out.println("有负数,程序退出");
System.exit(0);
}
System.out.printf("%d和%d的最大公约数是%d\n",m,n,a.f(m,n));
System.out.printf("%d和%d的最小公倍数是%d\n",m,n,b.f(m,n));
}
}
2.
abstractclassA{
publicabstractvoidf(intx);
publicabstractvoidg(intx,inty);
publicabstractdoubleh(doublex);
}
classA1extendsA{
publicvoidf(intx){
System.out.println(x);
}
publicvoidg(intx,inty){
intz=x+y;
System.out.println(z);
}
publicdoubleh(doublex){
returnx*x;
}
}
classA2extendsA{
publicvoidf(intx){
System.out.println("Hello:
"+x);
}
publicvoidg(intx,inty){
intz=x-y;
System.out.println(z);
}
publicdoubleh(doublex){
returnMath.sqrt(x);
}
}
classA3extendsA{
publicvoidf(intx){
System.out.println("你好:
"+x);
}
publicvoidg(intx,inty){
doublez=(double)x/y;
System.out.println(z);
}
publicdoubleh(doublex){
return1/x;
}
}
publicclassZuoYe5_2{
publicstaticvoidmain(Stringargs[]){
Aa=newA1();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
a=newA2();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
a=newA3();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
}
}
3.
interfaceA{
publicabstractvoidf(intx);
publicabstractvoidg(intx,inty);
publicabstractdoubleh(doublex);
}
classA1implementsA{
publicvoidf(intx){
System.out.println(x);
}
publicvoidg(intx,inty){
intz=x+y;
System.out.println(z);
}
publicdoubleh(doublex){
returnx*x;
}
}
classA2implementsA{
publicvoidf(intx){
System.out.println("Hello:
"+x);
}
publicvoidg(intx,inty){
intz=x-y;
System.out.println(z);
}
publicdoubleh(doublex){
returnMath.sqrt(x);
}
}
classA3implementsA{
publicvoidf(intx){
System.out.println("你好:
"+x);
}
publicvoidg(intx,inty){
doublez=(double)x/y;
System.out.println(z);
}
publicdoubleh(doublex){
return1/x;
}
}
publicclassZuoYe5_3{
publicstaticvoidmain(Stringargs[]){
Aa=newA1();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
a=newA2();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
a=newA3();
a.f(10);
a.g(12,20);
System.out.println(a.h(100));
}
}
4.
classCubic
{doublegetCubic(intn)
{return0;
}
}
abstractclassSqrt
{publicabstractdoublegetSqrt(intx);
}
classA
{voidf(Cubiccubic)
{doubleresult=cubic.getCubic(3);
System.out.println(result);
}
}
publicclassZuoYe5_4
{publicstaticvoidmain(Stringargs[])
{Aa=newA();
a.f(newCubic()
{doublegetCubic(intn)
{returnn*n*n;
}
}
);
Sqrtss=newSqrt()
{publicdoublegetSqrt(intx)
{returnMath.sqrt(x);
}
};
doublem=ss.getSqrt(5);
System.out.println(m);
}
}
5.
classIntegerExceptionextendsException
{Stringmessage;
IntegerException(intm)
{message="年龄"+m+"不合理";
}
publicStringtoString()
{returnmessage;
}
}
classPeople
{privateintage=1;
publicvoidsetAge(intage)throwsIntegerException
{
if(age>=160||age<=0)
thrownewIntegerException(age);//方法抛出异常,导致方法结束
else
this.age=age;
}
publicintgetAge()
{System.out.println("年龄"+age+"合理");
returnage;
}
}
publicclassZuoYe6_5
{publicstaticvoidmain(Stringargs[])
{Peoplewang=newPeople(),
zhang=newPeople();
try{wang.setAge(189);
System.out.println(wang.getAge());
}
catch(IntegerExceptione)
{System.out.println(e.toString());
}
try{zhang.setAge(28);
System.out.println(zhang.getAge());
}
catch(IntegerExceptione)
{System.out.println(e.toString());
}
}
}
第六章作业题
1.
importjava.util.regex.*;
importjava.util.*;
publicclassZuoYe6_1{
publicstaticvoidmain(Stringargs[]){
Scannerreader=newScanner(System.in);
Strings1=reader.nextLine();
Patternp;
Matcherm;
p=Ppile("[24680]A[13579]{2}");
m=p.matcher(s1);
while(m.find()){
Stringstr=m.group();
System.out.print("从"+m.start()+"到"+m.end()+"匹配模式子序列:
");
System.out.println(str);
}
}
}
2.
importjava.util.regex.*;
importjava.util.*;
publicclassZuoYe4_1{
publicstaticvoidmain(Stringargs[]){
Scannerreader=newScanner(System.in);
Strings1=reader.nextLine();
Patternp;
Matcherm;
p=Ppile("\\d+");
m