《Java语言程序设计基础篇》第8版第11章代码

程序清单11-1

GeometricObject1.java

 1 package yinchaoTest;
 2
 3 public class GeometricObject1{
 4     private String color = "white";
 5     private boolean filled;
 6     private java.util.Date dateCreated;
 7     /**Construct a default geometric object*/
 8     public GeometricObject1(){
 9         dateCreated = new java.util.Date();
10     }
11     /**Construct a geometric object with specified color
12      * and filled value*/
13     public GeometricObject1(String color, boolean filled){
14         dateCreated = new java.util.Date();
15         this.color = color;
16         this.filled = filled;
17     }
18     /**return color*/
19     public String getColor(){
20         return color;
21     }
22     /**set a new color*/
23     public void setColor(String color){
24         this.color = color;
25     }
26     /**Return filled. Since filled is boolean,
27      * its get method is named isFilled*/
28     public boolean isFilled(){
29         return filled;
30     }
31     /**Set a new filled*/
32     public void setFilled(boolean filled){
33         this.filled = filled;
34     }
35     /**Get dateCreate*/
36     public java.util.Date getDateCreate(){
37         return dateCreated;
38     }
39     //**Return a String representation of this object*/
40     public String toString(){
41         return "create on " + dateCreated + "\ncolor: " + color +
42                 " and filled: " + filled;
43     }
44 }

程序清单11-2

Circle4.java

 1 package yinchaoTest;
 2
 3 public class Circle4 extends GeometricObject1{
 4     private double radius;
 5
 6     public Circle4(){
 7     }
 8
 9     public Circle4(double radius){
10         this.radius = radius;
11     }
12
13     public Circle4(double radius,String color,boolean filled){
14         this.radius = radius;
15         setColor(color);
16         setFilled(filled);
17     }
18
19     /**Return radius*/
20     public double getRadius(){
21         return radius;
22     }
23
24     /**Set a new radius*/
25     public void setRadius(double radius){
26         this.radius = radius;
27     }
28
29     /**Return area*/
30     public double getArea(){
31         return radius*radius*Math.PI;
32     }
33
34     /**Return diameter*/
35     public double getDiameter(){
36         return 2 * radius;
37     }
38
39     /**Return perimeter*/
40     public double getPerimeter(){
41         return 2 * radius * Math.PI;
42     }
43     /**Print the circle info*/
44     public void printCircle(){
45         System.out.println("The circle is created" + getDateCreate() +
46                 "and the radius is " + radius);
47     }
48 }

程序清单11-3

Rectangle1.java

 1 package yinchaoTest;
 2
 3
 4 public class Rectangle1 extends GeometricObject1{
 5     private double width;
 6     private double height;
 7     public Rectangle1(){
 8
 9     }
10
11     public Rectangle1(double width, double height){
12         this.width = width;
13         this.height = height;
14     }
15
16     public Rectangle1(double width, double height, String color,
17             boolean filled){
18         this.width = width;
19         this.height = height;
20         setColor(color);
21         setFilled(filled);
22     }
23
24     /**Return width*/
25     public double getWidth(){
26         return width;
27         }
28
29     /**Set a new width*/
30     public void setWidth(){
31         this.width = width;
32     }
33
34     /**Return heigth*/
35     public double getHeigth(){
36         return height;
37     }
38
39     /**Set a new heigth*/
40     public void setHeigth(){
41         this.height = height;
42     }
43
44     /**Return area*/
45     public double getArea(){
46         return width * height;
47     }
48
49     /**Return perimeter*/
50     public double getPerimeter(){
51         return 2 * (width + height);
52     }
53 }

程序清单11-4

TestCircleRectangle.java

 1 package yinchaoTest;
 2
 3 public class TestCircleRectangle {
 4     public static void main(String[] args){
 5         Circle4 circle = new Circle4(1);
 6         System.out.println("A circle " + circle.toString());
 7         System.out.println("The radius is " + circle.getRadius());
 8         System.out.println("The area is " + circle.getArea());
 9         System.out.println("The diameter is " + circle.getDiameter());
10
11         Rectangle1 rectangle = new Rectangle1(2, 4);
12         System.out.println("\nA rectangle " + rectangle.toString());
13         System.out.println("The area is " + rectangle.getArea());
14         System.out.println("The perimeter is " +
15         rectangle.getPerimeter());
16     }
17 }
时间: 2024-08-23 23:12:42

《Java语言程序设计基础篇》第8版第11章代码的相关文章

Java语言程序设计基础篇第10版第5章习题答案

1 public class Demo { 2 public static void main(String[] args) { 3 java.util.Scanner input = new java.util.Scanner(System.in); 4 int num = input.nextInt(); 5 int count=0; 6 int a=0,b=0; 7 float sum=0; 8 while(num!=0){ 9 if(num>0) 10 a++; 11 else 12 b

Java语言程序设计基础篇 循环(四)

①打印:***** **** *** ** * for(int x=1; x<=5; x++) { for(int y=x; y<=5; y++) { System.out.print("*"); //向下一般的格式for(int y=x; y<=5; y++) } System.out.println(); } ②打印:* ** *** **** ***** for (int x=1; x<=5 ;x++ ) { for (int y=1;y<=x ;y

Java语言程序设计基础篇 方法(五)

生成随机字符 生成随机字符就是生成0到65535之间的一个随机整数,因为0<=Math.random()<1.0,必须在65535+1 (int) (Math.random() * (65535+1)) 随机生成小写字母 public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2){ return (char)(ch1 +Math.random() * (ch2 - ch1 + 1

Java语言程序设计基础篇 循环(四)练习

*4.21(计算不同利率下的贷款)编写程序,让用户输入贷款总额及以年为单位的贷款期限,以1/8为递增量,显示从5%到8%的利率下每月支付额和总偿还额.假设输入贷款总量为10 000,还贷期限为5年,所显示的输出如下: 贷款总额:to 000 年数:5 利率月支付额总偿还额 5%188 .71   11322.74 5 .125%189.28   11357.13 5 .25%189.85   11391.59 ... //Exercise3_26.java: displays the month

Java语言程序设计基础篇 数组(六)

Java语法之数组 数组的定义 数组是对象. 如:int [ ]  x = new int[100];或 :int x [ ]  = new int[100];(这种方式主要是为了适应C/C++程序员) 声明一个数组变量:int [ ] x;并不会在内存中给数组分配任何空间,仅创建一个引用数组的存储地址. 数组创建后,其元素赋予默认值,数值型基本数据类型默认值为0,char类型为'\u0000',boolean类型为false. 数组的静态初始化 如:int [ ] x = new int [

【JAVA语言程序设计基础篇】--图形用户界面基础--一些总结

第12章 图形界面基础 1.那个类是JAVA GUI组件的根?容器类是component的子类吗?哪个类是Swing GUI组建的根? java.awt.component是所有java GUI组件类的根. 容器类如JFrame是组件的子类. JComponent是Swing GUI组件类的根. 2.AWT组件与Swing组建的不同? AWT的组件是重而swing组件轻量化. 3. 你可以添加一个按钮到一个框架. 答:正确 您可以将一个框架添加到面板中. 答:错误 你可以添加一个面板到一个框架.

【JAVA语言程序设计基础篇】--事件驱动程序设计--匿名类监听器

监听器类是特意为创建一个GUI组件而设计的监听器对象.监听器不被其他应用程序所共享,因此,正确的做法是将他作为一个内部类定义在框架类中. 当然,可以使用匿名内部类简化内部类监听器. 匿名内部类是没有名字的内部类. 他一步完成定义内部类和创建一个该类的实例. 由于匿名内部类是一种特殊的内部类,所以,可以将他看作有以下特征的内部类: 1.匿名内部类必须总是扩展父类或者实现接口,但他不能有显示的extends 和 implements子句. 2.匿名捏不累必须实现父类或者接口中的所有的抽象方法. 3.

【JAVA语言程序设计基础篇】--图形-- 三种时钟--增强对类的理解和应用

1.显示任意时间时钟 2.设置三个可见性属性 分别表示时针,分针,秒针的可见性 3.一个精细的时钟 主类:StillClock @SuppressWarnings("serial") class DetailedClock extends JPanel { private int hour; private int minute; private int second; protected int xCenter, yCenter; protected int clockRadius;

【JAVA语言程序设计基础篇】--事件驱动程序设计--键盘事件

java] view plain copy print? package chapter16; import java.awt.Font; import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") p