《Java语言程序设计基础篇》第10版 梁勇 著第十章练习题答案Word下载.docx
《《Java语言程序设计基础篇》第10版 梁勇 著第十章练习题答案Word下载.docx》由会员分享,可在线阅读,更多相关《《Java语言程序设计基础篇》第10版 梁勇 著第十章练习题答案Word下载.docx(56页珍藏版)》请在冰豆网上搜索。
publicintgetHour(){
returnhour;
publicintgetMinute(){
returnminute;
publicintgetSecond(){
returnsecond;
publicvoidsetTime(longelapsedTime){
//Obtainthetotalsecondssincethemidnight,Jan1,1970
longtotalSeconds=elapsedTime/1000;
//Computethecurrentsecondintheminuteinthehour
second=(int)(totalSeconds%60);
//Obtainthetotalminutes
longtotalMinutes=totalSeconds/60;
//Computethecurrentminuteinthehour
minute=(int)(totalMinutes%60);
//Obtainthetotalhours
inttotalHours=(int)(totalMinutes/60);
//Computethecurrenthour
hour=(int)(totalHours%24);
10.2
publicclassExercise10_02{
BMIbmi1=newBMI("
JohnDoe"
18,145,5,10);
TheBMIfor"
+bmi1.getName()+"
is"
+bmi1.getBMI()+"
+bmi1.getStatus());
BMIbmi2=newBMI("
PeterKing"
215,5,10);
+bmi2.getName()+"
+bmi2.getBMI()+"
+bmi2.getStatus());
staticclassBMI{
privateStringname;
privateintage;
privatedoubleweight;
//inpounds
privatedoubleheight;
//ininches
publicfinaldoubleKILOGRAMS_PER_POUND=0.45359237;
publicfinaldoubleMETERS_PER_INCH=0.0254;
/**ConstructaBMIwiththespecifiedname,age,weight,
*feetandinches
*/
publicBMI(Stringname,intage,doubleweight,doublefeet,doubleinches){
this.name=name;
this.age=age;
this.weight=weight;
this.height=feet*12+inches;
publicBMI(Stringname,intage,doubleweight,doubleheight){
this.height=height;
publicBMI(Stringname,doubleweight,doubleheight){
this(name,20,weight,height);
publicdoublegetBMI(){
doublebmi=weight*KILOGRAMS_PER_POUND/
((height*METERS_PER_INCH)*(height*METERS_PER_INCH));
returnMath.round(bmi*100)/100.0;
publicStringgetStatus(){
doublebmi=getBMI();
if(bmi<
16)
return"
seriouslyunderweight"
;
elseif(bmi<
18)
underweight"
24)
normalweight"
29)
overweight"
35)
seriouslyoverweight"
else
gravelyoverweight"
publicStringgetName(){
returnname;
publicintgetAge(){
returnage;
publicdoublegetWeight(){
returnweight;
publicdoublegetHeight(){
returnheight;
10.3
publicclassExercise10_03{
MyIntegern1=newMyInteger(5);
n1iseven?
+n1.isEven());
n1isprime?
+n1.isPrime());
15isprime?
+MyInteger.isPrime(15));
char[]chars={'
3'
'
5'
9'
};
System.out.println(MyInteger.parseInt(chars));
Strings="
3539"
System.out.println(MyInteger.parseInt(s));
MyIntegern2=newMyInteger(24);
n2isodd?
+n2.isOdd());
45isodd?
+MyInteger.isOdd(45));
n1isequalton2?
+n1.equals(n2));
n1isequalto5?
+n1.equals(5));
classMyInteger{
privateintvalue;
publicintgetValue(){
returnvalue;
publicMyInteger(intvalue){
this.value=value;
publicbooleanisPrime(){
returnisPrime(value);
publicstaticbooleanisPrime(intnum){
if((num==1)||(num==2)){
returntrue;
for(inti=2;
i<
=num/2;
i++){
if(num%i==0){
returnfalse;
publicstaticbooleanisPrime(MyIntegero){
returnisPrime(o.getValue());
publicbooleanisEven(){
returnisEven(value);
publicbooleanisOdd(){
returnisOdd(value);
publicstaticbooleanisEven(intn){
returnn%2==0;
publicstaticbooleanisOdd(intn){
returnn%2!
=0;
publicstaticbooleanisEven(MyIntegern){
returnisEven(n.getValue());
publicbooleanequals(intanotherNum){
returnvalue==anotherNum;
publicbooleanequals(MyIntegero){
returnvalue==o.getValue();
publicstaticintparseInt(char[]numbers){
//numbersconsistsofdigitcharacters.
//Forexample,ifnumbersis{'
1'
2'
},thereturnvalue
//shouldbe125.Pleasenotethat
//numbers[0]is'
//numbers[1]is'
//numbers[2]is'
intresult=0;
for(inti=0;
numbers.length;
result=result*10+(numbers[i]-'
0'
);
returnresult;
//YoumaymentionthiswhenyoucoveredCh8
publicstaticintparseInt(Strings){
//sconsistsofdigitcharacters.
//Forexample,ifsis"
125"
thereturnvalue
//shouldbe125.
s.length();
result=result*10+(s.charAt(i)-'
10.4
publicclassExercise10_04{
MyPointp1=newMyPoint();
MyPointp2=newMyPoint(10,30.5);
System.out.println(p1.distance(p2));
System.out.println(MyPoint.distance(p1,p2));
classMyPoint{
privatedoublex;
privatedoubley;
publicMyPoint(){
publicMyPoint(doublex,doubley){
this.x=x;
this.y=y;
publicdoubledistance(MyPointsecondPoint){
returndistance(this,secondPoint);
publicstaticdoubledistance(MyPointp1,MyPointp2){
returnMath.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)
*(p1.y-p2.y));
publicdoublegetX(){
returnx;
publicdoublegetY(){
returny;
10.5
publicclassExercise10_05{
StackOfIntegersstack=newStackOfIntegers
(2);
java.util.Scannerinput=newjava.util.Scanner(System.in);
//Prompttheusertoenteraninteger
System.out.print("
Enteraninteger:
//Convertstringtoint
intnumber=input.nextInt();
Thefactorsfor"
+number+"
is"
//Findandstoreallthesmallestfactorsoftheinteger
intfactor=2;
while(factor<
=number){
if(number%factor==0){
number=number/factor;
stack.push(factor);
else{
factor++;
//Displayfactors
while(!
stack.empty()){
System.out.print(stack.pop()+"
10.6
publicclassExercise10_06{
finalintLIMIT=120;
intcount=0;
StackOfIntegersstack=newStackOfIntegers();
//Repeatedlyfindprimenumbers
for(intnumber=2;
number<
LIMIT;
number++)
if(isPrime(number)){
stack.push(number);
count++;
//Increasetheprimenumbercount
//Printthefirst30primenumbersindecreasingorder
Theprimenumberslessthan120are\n"
finalintNUMBER_PER_LINE=10;
if(stack.getSize()%NUMBER_PER_LINE==0)
System.out.println();
//advancetothenewline
publicstaticbooleanisPrime(intnumber){
//Assumethenumberisprime
booleanisPrime=true;
//Exercise03_21ifnumberisprime
for(intdivisor=2;
divisor<
=number/2;
divisor++){
//Iftrue,thenumberisnotprime
if(number%divisor==0){
//SetisPrimetofalse,ifthenumberisnotprime
isPrime=false;
break;
//Exittheforloop
returnisPrime;
10.7
publicclassExercise10_07{
//Createtenaccounts
privateAccount[]accounts=newAccount[10];
publicExercise10_07(){
accounts.length;
accounts[i]=newAccount();
accounts[i].setId(i);
accounts[i].setBalance(100);
continueATM:
while(true){
Enteranid:
id=input.nextInt();
if(id<
1||id>
10){
Pleaseenteracorrectid"
continue;
while(true){
intchoice=getAChoice();
switch(choice){
case1:
Thebalanceis"
accounts[id].getBalance());
case2:
withdraw();
case3:
deposit();
case4:
//Youcanrewritethecodewithoutusingthecontinue.
//Todoso,introduceabooleanvariabletocontrolone
//customersession
continuecontinueATM;
privatestaticjava.util.Scannerinput=newjava.util.Scanner(System.in);
intid;
newExercise10_07();
publicvoidwithdraw(){
Enteranamounttowithdraw:
intamount=input.nextInt();
if(amount<
=accounts[id].getBalance()){
accounts[id].withdraw(amount);
Theamountistoolarge,ignored"
publicvoiddeposit(){
Enteranamounttodeposit:
if(amount>
=0){
accounts[id].deposit(amount);
Theamountisnegative,ignored"
publicintgetAChoice(){
intchoice;
\nMainmenu"
1:
checkbalance"
2:
withdr