1、java web编程技术沈泽刚清华大学出版社第十四章课后习题部分解答Book14_1public class Book14_1 public static void main(String args) GeometricObject geoObject1 = new Circle(5); GeometricObject geoObject2 = new Rectangle(5, 3); System.out.println(The two objects have the same area? + equalArea(geoObject1, geoObject2); displayGeomet
2、ricObject(geoObject1); displayGeometricObject(geoObject2); public static boolean equalArea(GeometricObject object1, GeometricObject object2) return object1.getArea() = object2.getArea(); public static void displayGeometricObject(GeometricObject object) System.out.println(); System.out.println(The ar
3、ea is + object.getArea(); System.out.println(The perimeter is + object.getPerimeter(); abstract class GeometricObject private String color = white; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() dateCreated = new java.util.Date(); protected GeometricObject(St
4、ring color, boolean filled) dateCreated = new java.util.Date(); this.color = color; this.filled = filled; public String getColor() return color; public void setColor(String color) this.color = color; public boolean isFilled() return filled; public void setFilled(boolean filled) this.filled = filled;
5、 public java.util.Date getDateCreated() return dateCreated; public String toString() return created on + dateCreated + ncolor: + color + and filled: + filled; public abstract double getArea(); public abstract double getPerimeter();class Circle extends GeometricObject private double radius; public Ci
6、rcle() public Circle(double radius) this.radius = radius; public Circle(double radius, String color, boolean filled) this.radius = radius; setColor(color); setFilled(filled); public double getRadius() return radius; public void setRadius(double radius) this.radius = radius; public double getArea() r
7、eturn radius * radius * Math.PI; public double getPerimeter() return 2 * radius * Math.PI; public void printCircle() System.out.println(The circle is created + getDateCreated() + and the radius is + radius); class Rectangle extends GeometricObject private double width; private double height; public
8、Rectangle() public Rectangle(double width, double height) this.width = width; this.height = height; public Rectangle(double width, double height, String color, boolean filled) this.width = width; this.height = height; setColor(color); setFilled(filled); public double getWidth() return width; public
9、void setWidth(double width) this.width = width; public double getHeight() return height; public void setHeight(double height) this.height = height; public double getArea() return width * height; public double getPerimeter() return 2 * (width + height); Book14_5import java.util.*;public class Book14_
10、5 public static void main(String args) Calendar calendar = new GregorianCalendar(); System.out.println(Current time is + new Date(); 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.D
11、ATE); 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.
12、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); Calendar calendar1 = new GregorianCalendar(2001, 8, 11); System.out.pri
13、ntln(September 11, 2001 is a + dayNameOfWeek(calendar1.get(Calendar.DAY_OF_WEEK); public static String dayNameOfWeek(int dayOfWeek) switch (dayOfWeek) case 1: return Sunday; case 2: return Monday; case 3: return Tuesday; case 4: return Wednesday; case 5: return Thursday; case 6: return Friday; case
14、7: return Saturday; default: return null; Book14_6public class Book14_6 public static void main(String args) Object objects = new Tiger(), new Chicken(), new Apple(); for (int i = 0; i (ComparableRectangle)o).getArea() return 1; else if (getArea() 0) return o1; else return o2; class Rectangle extend
15、s GeometricObject private double width; private double height; public Rectangle() public Rectangle(double width, double height) this.width = width; this.height = height; public Rectangle(double width, double height, String color, boolean filled) this.width = width; this.height = height; setColor(col
16、or); setFilled(filled); public double getWidth() return width; public void setWidth(double width) this.width = width; public double getHeight() return height; public void setHeight(double height) this.height = height; public double getArea() return width * height; public double getPerimeter() return
17、 2 * (width + height); abstract class GeometricObject private String color = white; private boolean filled; private java.util.Date dateCreated; protected GeometricObject() dateCreated = new java.util.Date(); protected GeometricObject(String color, boolean filled) dateCreated = new java.util.Date();
18、this.color = color; this.filled = filled; public String getColor() return color; public void setColor(String color) this.color = color; public boolean isFilled() return filled; public void setFilled(boolean filled) this.filled = filled; public java.util.Date getDateCreated() return dateCreated; publ
19、ic String toString() return created on + dateCreated + ncolor: + color + and filled: + filled; public abstract double getArea(); public abstract double getPerimeter();Book14_8import javax.swing.*;import java.awt.event.*;public class Book14_8 public static void main(String args) JFrame frame = new Ha
20、ndleEvent(); frame.setTitle(Handle Event); frame.setSize(200, 150); frame.setLocation(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); class HandleEvent extends JFrame public HandleEvent() JButton jbtOk = new JButton(OK); JButton jbtCancel = new JButton(Cancel
21、); JPanel panel = new JPanel(); panel.add(jbtOk); panel.add(jbtCancel); add(panel); OKListenerClass listener1 = new OKListenerClass(); CancelListenerClass listener2 = new CancelListenerClass(); jbtOk.addActionListener(listener1); jbtCancel.addActionListener(listener2); class OKListenerClass implemen
22、ts ActionListener public void actionPerformed(ActionEvent e) System.out.println(Ok button clicked); class CancelListenerClass implements ActionListener public void actionPerformed(ActionEvent e) System.out.println(cancel button clicked); Book14_9import javax.swing.*;import java.awt.event.*;public cl
23、ass Book14_9 public static void main(String args) throws CloneNotSupportedException House house1 = new House(1, 1750.50); House house2 = (House)house1.clone(); System.out.println(house1 = house ? : + (house1 = house2); System.out.println(house1 equals to house2 ? : + house1.equals(house2); class Hou
24、se implements Cloneable, Comparable private int id; private double area; private java.util.Date whenBuilt; public House(int id, double area) this.id = id; this.area = area; whenBuilt = new java.util.Date(); public int getId() return id; public double getArea() return area; public java.util.Date getWhenBuilt() return whenBuilt; public Object clone() throws CloneNotSupportedException super.clone(); whenBuilt.clone(); return this; public int compareTo(Object o) if (area (House)o).area) return 1; else if (area (House)o).area) return -1; else return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1