java web编程技术沈泽刚清华大学出版社第十四章课后习题部分解答.docx
《java web编程技术沈泽刚清华大学出版社第十四章课后习题部分解答.docx》由会员分享,可在线阅读,更多相关《java web编程技术沈泽刚清华大学出版社第十四章课后习题部分解答.docx(12页珍藏版)》请在冰豆网上搜索。
![java web编程技术沈泽刚清华大学出版社第十四章课后习题部分解答.docx](https://file1.bdocx.com/fileroot1/2023-3/19/c78e3fde-ce18-4247-9d00-5ddb05e55e47/c78e3fde-ce18-4247-9d00-5ddb05e55e471.gif)
javaweb编程技术沈泽刚清华大学出版社第十四章课后习题部分解答
Book14_1
publicclassBook14_1{
publicstaticvoidmain(String[]args){
GeometricObjectgeoObject1=newCircle(5);
GeometricObjectgeoObject2=newRectangle(5,3);
System.out.println("Thetwoobjectshavethesamearea?
"+
equalArea(geoObject1,geoObject2));
displayGeometricObject(geoObject1);
displayGeometricObject(geoObject2);
}
publicstaticbooleanequalArea(GeometricObjectobject1,GeometricObjectobject2){
returnobject1.getArea()==object2.getArea();
}
publicstaticvoiddisplayGeometricObject(GeometricObjectobject){
System.out.println();
System.out.println("Theareais"+object.getArea());
System.out.println("Theperimeteris"+object.getPerimeter());
}
}
abstractclassGeometricObject{
privateStringcolor="white";
privatebooleanfilled;
privatejava.util.DatedateCreated;
protectedGeometricObject(){
dateCreated=newjava.util.Date();
}
protectedGeometricObject(Stringcolor,booleanfilled){
dateCreated=newjava.util.Date();
this.color=color;
this.filled=filled;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicbooleanisFilled(){
returnfilled;
}
publicvoidsetFilled(booleanfilled){
this.filled=filled;
}
publicjava.util.DategetDateCreated(){
returndateCreated;
}
publicStringtoString(){
return"createdon"+dateCreated+"\ncolor:
"+color+"andfilled:
"+filled;
}
publicabstractdoublegetArea();
publicabstractdoublegetPerimeter();
}
classCircleextendsGeometricObject{
privatedoubleradius;
publicCircle(){
}
publicCircle(doubleradius){
this.radius=radius;
}
publicCircle(doubleradius,Stringcolor,booleanfilled){
this.radius=radius;
setColor(color);
setFilled(filled);
}
publicdoublegetRadius(){
returnradius;
}
publicvoidsetRadius(doubleradius){
this.radius=radius;
}
publicdoublegetArea(){
returnradius*radius*Math.PI;
}
publicdoublegetPerimeter(){
return2*radius*Math.PI;
}
publicvoidprintCircle(){
System.out.println("Thecircleiscreated"+getDateCreated()+
"andtheradiusis"+radius);
}
}
classRectangleextendsGeometricObject{
privatedoublewidth;
privatedoubleheight;
publicRectangle(){
}
publicRectangle(doublewidth,doubleheight){
this.width=width;
this.height=height;
}
publicRectangle(doublewidth,doubleheight,Stringcolor,booleanfilled){
this.width=width;
this.height=height;
setColor(color);
setFilled(filled);
}
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicdoublegetArea(){
returnwidth*height;
}
publicdoublegetPerimeter(){
return2*(width+height);
}
}
Book14_5
importjava.util.*;
publicclassBook14_5{
publicstaticvoidmain(String[]args){
Calendarcalendar=newGregorianCalendar();
System.out.println("Currenttimeis"+newDate());
System.out.println("YEAR:
\t"+calendar.get(Calendar.YEAR));
System.out.println("MONTH:
\t"+calendar.get(Calendar.MONTH));
System.out.println("DATE:
\t"+calendar.get(Calendar.DATE));
System.out.println("HOUR:
\t"+calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY:
\t"+calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE:
\t"+calendar.get(Calendar.MINUTE));
System.out.println("SECOND:
\t"+calendar.get(Calendar.SECOND));
System.out.println("DAY_OF_WEEK:
\t"+calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_MONTH:
\t"+calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR:
\t"+calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("AM_PM:
"+calendar.get(Calendar.AM_PM));
Calendarcalendar1=newGregorianCalendar(2001,8,11);
System.out.println("September11,2001isa"+dayNameOfWeek(calendar1.get(Calendar.DAY_OF_WEEK)));
}
publicstaticStringdayNameOfWeek(intdayOfWeek){
switch(dayOfWeek){
case1:
return"Sunday";
case2:
return"Monday";
case3:
return"Tuesday";
case4:
return"Wednesday";
case5:
return"Thursday";
case6:
return"Friday";
case7:
return"Saturday";
default:
returnnull;
}
}
}
Book14_6
publicclassBook14_6{
publicstaticvoidmain(String[]args){
Object[]objects={newTiger(),newChicken(),newApple()};
for(inti=0;iif(objects[i]instanceofEdible)
System.out.println(((Edible)objects[i]).howToEat());
}
}
interfaceEdible{
publicabstractStringhowToEat();
}
classAnimal{
}
classChickenextendsAnimalimplementsEdible{
publicStringhowToEat(){
return"Chicken:
Fryit";
}
}
classTigerextendsAnimal{
}
abstractclassFruitimplementsEdible{
}
classAppleextendsFruit{
publicStringhowToEat(){
return"Apple:
Makeapplecider";
}
}
classOrangeextendsFruit{
publicStringhowToEat(){
return"Orange:
Makeorangejuice";
}
}
Book14_7
importjava.util.*;
publicclassBook14_7{
publicstaticvoidmain(String[]args){
ComparableRectanglerectangle1=newComparableRectangle(4,5);
ComparableRectanglerectangle2=newComparableRectangle(3,6);
System.out.println(Max.max(rectangle1,rectangle2));
}
}
classComparableRectangleextendsRectangleimplementsComparable{
publicComparableRectangle(doublewidth,doubleheight){
super(width,height);
}
publicintcompareTo(Objecto){
if(getArea()>((ComparableRectangle)o).getArea())
return1;
elseif(getArea()<((ComparableRectangle)o).getArea())
return-1;
else
return0;
}
}
classMax{
publicstaticComparablemax(Comparableo1,Comparableo2){
if(pareTo(o2)>0)
returno1;
else
returno2;
}
}
classRectangleextendsGeometricObject{
privatedoublewidth;
privatedoubleheight;
publicRectangle(){
}
publicRectangle(doublewidth,doubleheight){
this.width=width;
this.height=height;
}
publicRectangle(doublewidth,doubleheight,Stringcolor,booleanfilled){
this.width=width;
this.height=height;
setColor(color);
setFilled(filled);
}
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicdoublegetArea(){
returnwidth*height;
}
publicdoublegetPerimeter(){
return2*(width+height);
}
}
abstractclassGeometricObject{
privateStringcolor="white";
privatebooleanfilled;
privatejava.util.DatedateCreated;
protectedGeometricObject(){
dateCreated=newjava.util.Date();
}
protectedGeometricObject(Stringcolor,booleanfilled){
dateCreated=newjava.util.Date();
this.color=color;
this.filled=filled;
}
publicStringgetColor(){
returncolor;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicbooleanisFilled(){
returnfilled;
}
publicvoidsetFilled(booleanfilled){
this.filled=filled;
}
publicjava.util.DategetDateCreated(){
returndateCreated;
}
publicStringtoString(){
return"createdon"+dateCreated+"\ncolor:
"+color+"andfilled:
"+filled;
}
publicabstractdoublegetArea();
publicabstractdoublegetPerimeter();
}
Book14_8
importjavax.swing.*;
importjava.awt.event.*;
publicclassBook14_8{
publicstaticvoidmain(String[]args){
JFrameframe=newHandleEvent();
frame.setTitle("HandleEvent");
frame.setSize(200,150);
frame.setLocation(200,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
classHandleEventextendsJFrame{
publicHandleEvent(){
JButtonjbtOk=newJButton("OK");
JButtonjbtCancel=newJButton("Cancel");
JPanelpanel=newJPanel();
panel.add(jbtOk);
panel.add(jbtCancel);
add(panel);
OKListenerClasslistener1=newOKListenerClass();
CancelListenerClasslistener2=newCancelListenerClass();
jbtOk.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
}
}
classOKListenerClassimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
System.out.println("Okbuttonclicked");
}
}
classCancelListenerClassimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
System.out.println("cancelbuttonclicked");
}
}
Book14_9
importjavax.swing.*;
importjava.awt.event.*;
publicclassBook14_9{
publicstaticvoidmain(String[]args)throwsCloneNotSupportedException{
Househouse1=newHouse(1,1750.50);
Househouse2=(House)house1.clone();
System.out.println("house1==house?
:
"+(house1==house2));
System.out.println("house1equalstohouse2?
:
"+house1.equals(house2));
}
}
classHouseimplementsCloneable,Comparable{
privateintid;
privatedoublearea;
privatejava.util.DatewhenBuilt;
publicHouse(intid,doublearea){
this.id=id;
this.area=area;
whenBuilt=newjava.util.Date();
}
publicintgetId(){
returnid;
}
publicdoublegetArea(){
returnarea;
}
publicjava.util.DategetWhenBuilt(){
returnwhenBuilt;
}
publicObjectclone()throwsCloneNotSupportedException{
super.clone();
whenBuilt.clone();
returnthis;
}
publicintcompareTo(Objecto){
if(area>((House)o).area)
return1;
elseif(area<((House)o).area)
return-1;
else
return0;
}
}