《Java语言程序设计基础篇》第8版第11章代码
2016-01-10 11:18
399 查看
程序清单11-1
GeometricObject1.java
程序清单11-2
Circle4.java
程序清单11-3
Rectangle1.java
程序清单11-4
TestCircleRectangle.java
GeometricObject1.java
package yinchaoTest; public class GeometricObject1{ private String color = "white"; private boolean filled; private java.util.Date dateCreated; /**Construct a default geometric object*/ public GeometricObject1(){ dateCreated = new java.util.Date(); } /**Construct a geometric object with specified color * and filled value*/ public GeometricObject1(String color, boolean filled){ dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } /**return color*/ public String getColor(){ return color; } /**set a new color*/ public void setColor(String color){ this.color = color; } /**Return filled. Since filled is boolean, * its get method is named isFilled*/ public boolean isFilled(){ return filled; } /**Set a new filled*/ public void setFilled(boolean filled){ this.filled = filled; } /**Get dateCreate*/ public java.util.Date getDateCreate(){ return dateCreated; } //**Return a String representation of this object*/ public String toString(){ return "create on " + dateCreated + "\ncolor: " + color + " and filled: " + filled; } }
程序清单11-2
Circle4.java
package yinchaoTest; public class Circle4 extends GeometricObject1{ private double radius; public Circle4(){ } public Circle4(double radius){ this.radius = radius; } public Circle4(double radius,String color,boolean filled){ this.radius = radius; setColor(color); setFilled(filled); } /**Return radius*/ public double getRadius(){ return radius; } /**Set a new radius*/ public void setRadius(double radius){ this.radius = radius; } /**Return area*/ public double getArea(){ return radius*radius*Math.PI; } /**Return diameter*/ public double getDiameter(){ return 2 * radius; } /**Return perimeter*/ public double getPerimeter(){ return 2 * radius * Math.PI; } /**Print the circle info*/ public void printCircle(){ System.out.println("The circle is created" + getDateCreate() + "and the radius is " + radius); } }
程序清单11-3
Rectangle1.java
package yinchaoTest; public class Rectangle1 extends GeometricObject1{ private double width; private double height; public Rectangle1(){ } public Rectangle1(double width, double height){ this.width = width; this.height = height; } public Rectangle1(double width, double height, String color, boolean filled){ this.width = width; this.height = height; setColor(color); setFilled(filled); } /**Return width*/ public double getWidth(){ return width; } /**Set a new width*/ public void setWidth(){ this.width = width; } /**Return heigth*/ public double getHeigth(){ return height; } /**Set a new heigth*/ public void setHeigth(){ this.height = height; } /**Return area*/ public double getArea(){ return width * height; } /**Return perimeter*/ public double getPerimeter(){ return 2 * (width + height); } }
程序清单11-4
TestCircleRectangle.java
package yinchaoTest; public class TestCircleRectangle { public static void main(String[] args){ Circle4 circle = new Circle4(1); System.out.println("A circle " + circle.toString()); System.out.println("The radius is " + circle.getRadius()); System.out.println("The area is " + circle.getArea()); System.out.println("The diameter is " + circle.getDiameter()); Rectangle1 rectangle = new Rectangle1(2, 4); System.out.println("\nA rectangle " + rectangle.toString()); System.out.println("The area is " + rectangle.getArea()); System.out.println("The perimeter is " + rectangle.getPerimeter()); } }
相关文章推荐
- SpringMVC中使用Interceptor拦截器
- java资源下载之官网地址
- 上机题目(初级)-小明的筷子(Java)
- Java中Spring获取bean方法小结
- Spring boot quick start
- SpringMVC 中Transactional注解的巨坑
- java对文件压缩与解压缩操作
- 基于Java实现杨辉三角 LeetCode Pascal's Triangle
- Java EE V7.0学习笔记- 安装 WebSphere Developer Tools
- 上机题目(初级)-计算两个正整数的最大公约数和最小公倍数(Java)
- 从零开始写javaweb框架笔记9-细节完善与代码优化-完善控制器层
- 最大堆的插入/删除/调整/排序操作(图解+程序)(JAVA)
- java中的四种访问权限
- scala出现异常:Exception in thread "main" java.lang.StackOverflowError
- spring jmx 整合
- Java
- Java中的static与final的各自作用与联系
- JavaWeb - Jsp页面属性简介 一
- Java Swing JTable 表格【5:表格模型AbstractTableModel】
- 桥接模式