Java-SSD3-实验4《面向对象——继承》
一,实验目的
- 理解面向对象编程,尤其是继承的思想,学习通过继承,在父类的基础上创建子类
- 使用关键字super调用父类的构造方法和方法
- 在子类中覆盖方法
二、实验内容
1. (P380, 11.1)【三角形类Triangle】设计一个名为Triangle的类来扩展GeometricObject类。该类包括:
(1)三个名为side1、side2和side3的double数据域表示这个三角形的三条边,它们的默认值是1.0。
(2)一个无参构造方法创建默认的三角形。
(3)一个能创建带指定side1、side2和side3的三角形的构造方法。
(4)所有三个数据域的访问器方法。
(5)一个名为getArea()的方法返回这个三角形的面积。计算三角形面积的公式参见实验1练习题2.19。
(6)一个名为getPerimeter()的方法返回这个三角形的周长。
(7)一个名为toString()的方法返回这个三角形的字符串描述,显示三角形的三边长、周长及面积。
目标输出任务:
-
画出Triangle类和GeometricObject类的UML图。
-
实现这些类。
-
编写一个测试程序,创建边长为1、1.5和1,颜色为yellow,filled为true的Triangle对象,然后显示它的三边长、周长、面积、颜色以及是否被填充。
1.1画出UML类图如下图所示:
1.2运行结果分析
1)输入题中所给数据,程序运行结果如下图所示:
2)输入另一组测试数据,程序运行结果如下图所示:
由上可知该程序符合实验要求。
1.3心得体会
面向对象相比面向过程虽然方便却也多了体力活。但IDEA中含有各种插件,以后要多多研究,可以方便编程。这个题是最基本的继承,并未遇到难坎。总的来说,细心为上。
1.4源代码展示
- public class Program1 {
- public static void main(String[] args) {
- Triangle t = new Triangle(1,1,1);
- t.setColor("yellow");
- t.setFilled(true);
- System.out.println(t.toString());
- }
- }
- class GeometricObject{
- private String color ;
- private boolean filled;
- public GeometricObject() {
- }
- public GeometricObject(String color, boolean filled) {
- 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;
- }
- @Override
- public String toString() {
- return "GeometricObject{" +
- "color='" + color + '\'' +
- ", filled=" + filled +
- '}';
- }
- }
- class Triangle extends GeometricObject{
- double side1 = 1;
- double side2 = 1;
- double side3 = 1;
- public Triangle() {
- this.side1 = 1;
- this.side2 = 1;
- this.side3 = 1;
- }
- public Triangle(double side1, double side2, double side3) {
- this.side1 = side1;
- this.side2 = side2;
- this.side3 = side3;
- }
- public double getSide1() {
- return side1;
- }
- public double getSide2() {
- return side2;
- }
- public double getSide3() {
- return side3;
- }
- public double getArea(){
- double s = (side1 + side2 + side3)/2;
- return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
- }
- public double getPerimeter(){
- return side1 + side2 +side3 ;
- }
- @Override
- public String toString() {
- return "Triangle{" +
- "\nside1=" + side1 +
- ", side2=" + side2 +
- ", side3=" + side3 +
- "\ncolor :"+getColor()+
- "\nfilled:"+isFilled()+
- "\nArea :"+ getArea()+
- "\nPerimeter:"+getPerimeter()+
- "\n}";
- }
- }
2. (P342, 10.11)【Circle2D类】定义Circle2D类,包括:
(1)两个带有get方法的名为x和y的double型数据域,表明圆的中心点。
(2)一个带get方法的数据域radius。
(3)一个无参构造方法,该方法创建一个(x,y)值为(0, 0)且radius为1的默认圆。
(4)一个构造方法,创建带指定的x、y和radius的圆。
(5)一个返回圆面积的方法getArea()。
(6)一个返回圆周长的方法getPerimeter()。
(7)如果给定的点(x, y)在圆内,那么方法contains(double x, double y)返回true。如图a所示。
(8)如果给定的圆在这个圆内,那么方法contains(Circle2D circle)返回true。如图b所示。
(9)如果给定的圆和这个圆重叠,那么方法overlaps(Circle2D circle)返回true。如图c所示。
s
目标输出任务:
- 画出该类的UML图。
- 实现这个类。
- 编写测试程序,创建一个Circle2D对象c1(new Circle2D(2, 2, 5.5)),显示它的面积和周长,还要显示c1.contains(3, 3)、c1.contains(new Circle2D(4, 5, 10.5))和c1.overlaps(new Circle2D(3, 5, 2.3))。
2.1画出UML类图如下图所示:
2.2运行结果分析
对实验所要求数据进行测试,程序运行结果如下图所示:
由程序运行结果可知,该程序符合实验要求。
2.3心得体会
这个程序实现并无特别难的地方,比较顺利。这里定义的是平面的圆类,借助程序解决的解析几何的一个经典问题。同样的,借助编程,我们可以解决更多的数学问题。
2.4程序源代码:
- public class Program2 {
- public static void main(String[] args) {
- Circle2D c1 = new Circle2D(2,2,5.5);
- System.out.println(c1.toString());
- System.out.println("The Area is :"+c1.getArea()+"\nThe Perimetrt is :"+c1.getPerimeter());
- System.out.println("The result of c1.contains(3, 3) is :"+c1.contains(3, 3));
- System.out.println("The result of c1.contains(new Circle2D(4, 5, 10.5)) is :"+
- c1.contains(new Circle2D(4, 5, 10.5)));
- System.out.println("The result of c1.overlap(new Circle2D(3, 5, 2.3)) is :"
- +c1.overlaps(new Circle2D(3, 5, 2.3)));
- }
- }
- class Circle2D{
- double x;
- double y;
- double radius;
- public Circle2D() {
- this.x = 1;
- this.y = 1;
- this.radius = 1;
- }
- public Circle2D(double x, double y, double radius) {
- this.x = x;
- this.y = y;
- this.radius = radius;
- }
- public double getArea(){
- return Math.PI * getRadius() * getRadius();
- }
- public double getPerimeter(){
- return 2 * Math.PI * getRadius();
- }
- /* (7)如果给定的点(x, y)在圆内,那么方法contains(double x, double y)返回true。如图a所示。
- w (8)如果给定的圆在这个圆内,那么方法contains(Circle2D circle)返回true。如图b所示。
- w (9)如果给定的圆和这个圆重叠,那么方法overlaps(Circle2D circle)返回true。如图c所示。
- *
- */
- public boolean contains(double x,double y){
- if((x-this.x) *(x-this.x) + (y-this.y)*(y-this.y) <= radius * radius)
- return true;
- return false;
- }
- public boolean contains(Circle2D circle){
- double s = (circle.getY() - this.getY())*(circle.getY() - this.getY())
- + (circle.getX() - this.getX()) *(circle.getX() - this.getX());
- s = Math.sqrt(s);
- if(s <= this.getRadius() -circle.getRadius())
- return true;
- return false;
- }
- public boolean overlaps(Circle2D circle){
- double s = (circle.getY() - this.getY())*(circle.getY() - this.getY())
- + (circle.getX() - this.getX()) *(circle.getX() - this.getX());
- s = Math.sqrt(s);
- if(s <= this.getRadius() + circle.getRadius() && s >= this.getRadius() -circle.getRadius())
- return true;
- return false;
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public double getRadius() {
- return radius;
- }
- @Override
- public String toString() {
- return "Circle2D{" +
- "x=" + x +
- ", y=" + y +
- ", radius=" + radius +
- '}';
- }
- }