JAVA期末速成库(12)第十三章

一、习题介绍

第十三章

Check Point:P501 13.3,13.17,13.28,13.29

Programming Exercise:13.1,13.6,13.11

二、习题及答案

Check Point:

13.3

True or false?

a. An abstract class can be used just like a nonabstract class except that you cannot

use the new operator to create an instance from the abstract class.

b. An abstract class can be extended.

c. A subclass of a nonabstract superclass cannot be abstract.

d. A subclass cannot override a concrete method in a superclass to define it as abstract.

e. An abstract method must be nonstatic.

13.3对还是错?

a.抽象类可以像非抽象类一样使用,只是不能使用new操作符从抽象类创建实例。

b.抽象类可以被扩展。

c.非抽象超类的子类不能是抽象的。

d.子类不能重写父类中的具体方法来将其定义为抽象。

e.抽象方法必须是非静态的。

答:a. 对:抽象类不能使用 new 操作符直接创建实例,但可以被其他类继承。

    b. 对:抽象类被其他类继承

    c、d 错:非抽象的子类可以是抽象的,也可以重写父类中的具体方法,但不可以将其定义为抽象方法。

    e. 对:抽象方法必须是非静态的,因为抽象方法没有实现,而静态方法不能被子类覆盖。

13.17 True or false? If a class implements Comparable, the object of the class can invoke the compareTo method.

13.17对还是错?如果一个类实现了Comparable,则该类的对象可以调用compareTo方法。

答:对:如果一个类实现了 Comparable 接口,那么该类的对象可以调用 compareTo 方法,用于比较对象。

13.28

Define the terms abstract classes and interfaces. What are the similarities and differ

ences between abstract classes and interfaces?

13.28定义术语抽象类和接口有什么相似之处和不同之处?抽象类和接口之间的关系?

答:抽象类:不能被实例化,可以包含抽象方法和具体方法,可以有成员变量。

接口:完全抽象,不能包含具体的实现,所有的方法默认都是抽象的,可以有默认方法和静态方法,从Java 8开始。

相似之处:都可以被其他类实现或继承,都可以定义抽象方法。

不同之处:抽象类可以提供部分实现,接口完全抽象;一个类可以实现多个接口,但只能继承一个抽象类;接口中的方法默认是public的,抽象类中的方法可以有多种访问修饰符。

13.29 True or false?

a. An interface is compiled into a separate bytecode file.

b. An interface can have static methods.

c. An interface can extend one or more interfaces.

d. An interface can extend an abstract class.

e. An abstract class can extend an interface.

13.29对还是错?

a.将接口编译成单独的字节码文件。

b.接口可以有静态方法。

c.一个接口可以扩展一个或多个接口。

d.接口可以扩展抽象类。

e.抽象类可以扩展接口。

答:a. 对:接口编译后会生成单独的字节码文件。

    b. 对:接口可以有静态方法,从Java 8开始。

    c. 对:一个接口可以扩展一个或多个接口。

    d. 错:接口不能扩展抽象类,但抽象类可以实现一个或多个接口

    e. 对:抽象类可以实现接口。

Programming Exercise:

**13.1 (Triangle class)

Design a new Triangle class that extends the abstractGeometricObject class. Draw the UML diagram for the classes Triangle and GeometricObject and then implement the Triangle class. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

**13.1 (Triangle类)设计一个新的Triangle类来扩展抽象类GeometricObject类。为类Triangle绘制UML图和GeometricObject,然后实现Triangle类。编写测试程序,提示用户输入三角形的三条边,一种颜色和一个布尔值,指示三角形是否被填充。程序应该创建用这些边创建一个三角形对象,并设置颜色和填充属性输入。程序应该显示区域、周长、颜色和真或假表示是否已填充。

abstract class GeometricObject {

    public abstract double getArea();

    public abstract double getPerimeter();

}



class Triangle extends GeometricObject {

    private double side1, side2, side3;

    private String color;

    private boolean filled;



    public Triangle(double side1, double side2, double side3, String color, boolean filled) {

        this.side1 = side1;

        this.side2 = side2;

        this.side3 = side3;

        this.color = color;

        this.filled = filled;

    }



    @Override

    public double getArea() {

        // 计算三角形面积的公式

    }



    @Override

    public double getPerimeter() {

        return side1 + side2 + side3;

    }



    // Getters and setters

}

*13.6 (The ComparableCircle class) Define a class named ComparableCircle

that extends Circle and implements Comparable. Draw the UML diagram and

implement the compareTo method to compare the circles on the basis of area.

Write a test class to find the larger of two instances of ComparableCircle objects.

*13.6 (comparablecycle类)

定义一个名为comparablecycle的类。

它扩展Circle并实现Comparable。

绘制UML图和实现compareTo方法,根据面积对圆进行比较。

编写一个测试类来查找ComparableCircle对象的两个实例中较大的一个。

class Circle {

    private double radius;



    public Circle(double radius) {

        this.radius = radius;

    }



    public double getArea() {

        return Math.PI * radius * radius;

    }

    // Other methods

}



class ComparableCircle extends Circle implements Comparable<ComparableCircle> {

    public ComparableCircle(double radius) {

        super(radius);

    }



    @Override

    public int compareTo(ComparableCircle other) {

        return Double.compare(this.getArea(), other.getArea());

    }

}



// 测试类逻辑

// 创建两个 ComparableCircle 对象

// 使用 compareTo 方法找出面积较大的圆

*13.11 (The Octagon class) Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula:

area = (2 + 4/22)* side * side

Draw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable. Write a test program that creates an Octagon object with side value 5 and displays its area and perimeter. Create a new object using the clone method and compare the two objects using the compareTo method.

*13.11 (Octagon类)编写一个名为Octagon的类来扩展对可比性和可克隆性的脸。假设八边形的八个边都是等长。这个区域可以用下式计算:

面积=(2 + 4/22)*边*边

       绘制UML图,包括八边形、几何对象、可比较,可克隆。编写一个测试程序,创建一个八边形,边值为5的对象,并显示其面积和周长。创建一个新对象使用clone方法并使用compareTo比较两个对象方法。

class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable {

    private double side;



    public Octagon(double side) {

        this.side = side;

    }



    @Override

    public double getArea() {

        return (2 + (4 * Math.sqrt(2))) * side * side;

    }



    @Override

    public double getPerimeter() {

        return 8 * side;

    }



    @Override

    public int compareTo(Octagon other) {

        return Double.compare(this.getArea(), other.getArea());

    }



    @Override

    public Octagon clone() {

        try {

            return (Octagon) super.clone();

        } catch (CloneNotSupportedException e) {

            // Handle exception

        }

        return null;

    }

    // Other methods

}



// 测试程序逻辑

// 创建一个边长为5的 Octagon 对象

// 显示其面积和周长

// 使用 clone 方法创建新对象

// 使用 compareTo 方法比较两个对象

 结语 

相信自己的力量

相信自己的梦想

因为你拥有改变世界的能力

!!!

  • 21
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT 青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值