java程序设计教程第六版课后习题答案文档格式.docx
《java程序设计教程第六版课后习题答案文档格式.docx》由会员分享,可在线阅读,更多相关《java程序设计教程第六版课后习题答案文档格式.docx(29页珍藏版)》请在冰豆网上搜索。
![java程序设计教程第六版课后习题答案文档格式.docx](https://file1.bdocx.com/fileroot1/2022-11/24/61710fa2-7be0-4d9c-92b6-515237d9a643/61710fa2-7be0-4d9c-92b6-515237d9a6431.gif)
System.out.println("
YingLi:
+Yingli);
QianMi:
+Qianmi);
2.6
publicclassTimeConverter1{
intHour=5,Minute=35,Second=51;
intSECONDS;
SECONDS=Hour*60*60+Minute*60+Second;
System.out.println(+Hour+"
时"
+Minute+"
分"
+Second+"
秒"
);
换算成秒:
+SECONDS);
2.7
publicclassTimeConverter2{
intSECONDS=10853;
intHour,Minute,Second;
Second=SECONDS%60;
Minute=(SECONDS-Second)%60;
Hour=(SECONDS-Second-Minute*60)/3600;
System.out.println(SECONDS+"
秒,转化为"
System.out.println(Hour+"
2.9
importjava.util.*;
publicclassDollarbill{
floatDollar1,Dollar2;
intTen,Five,One,Quarters,Dimes,Nickles,Pennies;
Scannerreader=newScanner(System.in);
输入币值:
Dollar1=reader.nextFloat();
Dollar2=Dollar1*100;
Pennies=(int)Dollar2%5;
Nickles=((int)Dollar2%10-Pennies)/5;
Dimes=((int)Dollar2-Pennies-Nickles*5)%50/10;
Quarters=(int)Dollar2%100/50;
One=((int)Dollar2-Pennies-Nickles*5-Dimes*10-Quarters*50)%500/100;
Five=(int)Dollar2%1000/500;
Ten=(int)Dollar2/1000;
System.out.println(Ten+"
tendollarbills"
System.out.println(Five+"
fivedollarbills"
System.out.println(One+"
onedollarbills"
System.out.println(Quarters+"
quartersdollarbills"
System.out.println(Dimes+"
dimesdollarbills"
System.out.println(Nickles+"
nicklesdollarbills"
System.out.println(Pennies+"
penniesdollarbills!
2.11
publicclassFenshuzhuanhuan{
intx,y;
doubleResult=0;
输入x:
x=reader.nextInt();
输入y:
y=reader.nextInt();
Result+=x/y;
分数"
+x+"
/"
+y);
转换成小数是:
+Result);
2.16
importjavax.swing.JApplet;
importjava.awt.*;
publicclassOlympiclogoextendsJApplet{
publicvoidpaint(Graphicspage){
page.setColor(Color.blue);
page.drawOval(25,65,40,40);
page.setColor(Color.yellow);
page.drawOval(55,65,40,40);
page.setColor(Color.black);
page.drawOval(85,65,40,40);
page.setColor(Color.green);
page.drawOval(115,65,40,40);
page.setColor(Color.red);
page.drawOval(145,65,40,40);
//circle
page.setColor(Color.cyan);
page.drawString("
OLYMPICLOGO"
40,30);
2.19
importjava.applet.*;
publicclassEx2_19extendsApplet{
publicvoidpaint(Graphicspage){
page.setColor(Color.BLACK);
page.setFont(newFont("
楷体"
Font.ITALIC+Font.BOLD,30));
page.drawString("
林少锋"
page.setColor(Color.blue);
宋体"
Font.BOLD,30));
70,80);
}
2.20
publicclassEx2_20extendsApplet{
page.drawOval(35,35,130,130);
page.setColor(Color.red);
page.fillArc(35,35,130,130,0,45);
page.fillArc(35,35,130,130,45,45);
page.setColor(Color.yellow);
page.fillArc(35,35,130,130,90,45);
page.setColor(Color.cyan);
page.fillArc(35,35,130,130,135,45);
page.setColor(Color.gray);
page.fillArc(35,35,130,130,180,45);
page.setColor(Color.green);
page.fillArc(35,35,130,130,225,45);
page.setColor(Color.darkGray);
page.fillArc(35,35,130,130,270,45);
page.setColor(Color.pink);
page.fillArc(35,35,130,130,315,45);
PP4.1
方法1
publicclassCreateSphere{
/**
*@paramargs
*/
//TODO自动生成方法存根
System.out.println("
请输入直径d:
Scannerscan=newScanner(System.in);
doubled=scan.nextDouble();
SphereD=newSphere(d);
D.Square();
D.Volum();
System.out.println(D.toString());
publicclassSphere{
finaldoublePI=3.14;
doubleV,S;
doubled;
Sphere(doubled){
this.d=d;
publicvoidVolum(){
V=(4/3)*PI*(d/2)*(d/2)*(d/2);
publicvoidSquare(){
S=4*PI*(d/2)*(d/2);
publicStringtoString(){
Strings="
;
Stringresult1=Double.toString(S);
Stringresult2=Double.toString(V);
s=("
体积为:
+result2+"
面积为:
+result1);
returns;
方法2
//Sphere.java
publicclassSphere{
privatedoublediameter;
publicSphere(){//构造方法:
无参数
this.diameter=1.0;
publicSphere(doubled){//构造方法:
带一个参数
this.diameter=d;
publicvoidsetDiameter(doubled){//设置直径值的方法
publicdoublegetDiameter(){//获取直径值的方法
returnthis.diameter;
publicdoublevolume(){//计算球的体积
return4*Math.PI*Math.pow(this.diameter/2,3)/3;
publicdoublearea(){//计算球的表面积
return4*Math.PI*Math.pow(this.diameter/2,2);
Stringout="
该球体的直径为:
+this.diameter+"
\n"
+"
该球体的表面积为:
+this.area()+"
+
该球体的体积为:
+this.volume();
returnout;
//MultiSphere.java
importjava.util.Scanner;
publicclassMultiSphere
{
publicstaticvoidmain(String[]args){
Scannerscan=newScanner(System.in);
Spheresphere1=newSphere();
Spheresphere2=newSphere(3.5);
sphere1:
+sphere1+"
sphere2:
+sphere2+"
sphere1和sphere2分别调用无参构造方法"
和带一个参数的构造方法进行初始化。
System.out.print("
现在,请输入一个数作为球sphere1的直径值:
sphere1.setDiameter(scan.nextDouble());
更改过的sphere1:
+sphere1);
//////////////////////////////////////////////////////////
PP4.2
publicclassCreateDog{
请输入狗的年龄age:
intage=scan.nextInt();
请输入狗的姓名name:
Scannerscan1=newScanner(System.in);
Stringname=scan1.next();
KennelAGE=newKennel(age);
AGE.Age();
System.out.println(AGE.toString());
狗的名字是:
+name);
publicclassKennel{
finalintmul=7;
intage;
intagep;
charname;
Kennel(intage){
this.age=age;
this.name=name;
publicvoidAge(){
agep=age*mul;
Stringr1=Integer.toString(age);
Stringr2=Integer.toString(agep);
狗的年龄为:
+r1+"
对应人的年龄为:
+r2);
classDog1
Stringname;
publicDog1()
{
name="
heizi"
age=1;
publicDog1(Stringn,inta)
name=n;
age=a;
publicintDogAge(intAge)
intage=Age/7;
return(age);
publicvoidsetName(Stringname1)
name=name1;
publicStringgetName()
return(name);
publicStringtoString()
Dog1d=newDog1();
return("
这只狗叫"
+d.name+"
已经有"
+d.age+"
岁了。
publicclassKennel{
Dog1dog1=newDog1();
dog1的name="
+dog1.name);
dog1的age="
+dog1.age);
Dog1dog2=newDog1("
xiaobai"
2);
dog2的name="
+dog2.name);
dog2的age="
+dog2.age);
dog1.setName("
diandian"
dog1.getName();
intage=dog1.DogAge(35);
age="
+age);
Stringr=dog1.toString();
r="
+r);
}
PP4.4
publicclassBook{
Stringinfo=null;
Bookshelfbook;
BookshelfINFO=newBookshelf(info);
INFO.BOOK();
System.out.println(INFO.toString());
publicclassBookshelf{
Stringbookname;
Stringauthor;
Stringpublish;
Stringdate;
Stringbooks;
Stringinfo;
publicBookshelf(Stringinfo){
bookname="
JAVA"
author="
qwe"
publish="
TsingHUA"
date="
19890322"
this.info=info;
publicStringBOOK(){
info=bookname+author+publish+date;
returninfo;
Stringbookinfo;
bookinfo="
书名:
+bookname+"
作者:
+author+"
bookinfo+="
出版社:
+publish+"
+"
出版日期:
+date;
returnbookinfo;
classBook
Stringtitle,author,publisher,copyright;
Book()
title="
JAVA编程语言设计"
JohnLewis"
publisher="
电子工业出版社"
copyright="
xuesheng"
Book(Stringt,Stringa,Stringp,Stringc)
title=t;
author=a;
publisher=p;
copyright=c;
voidsetBook(Stringt1,Stringa1,Stringp1,Stringc1)
title=t1;
author=a1;
publisher=p1;
copyright=c1;
StringgetBookTitle()
return(title);
StringgetBookAuthor()
return(author);
StringgetBookPublisher()
return(publisher);
StringgetBookCopyright()