21xrx.com
2024-11-22 14:20:39 Friday
登录
文章检索 我的文章 写文章
定义一个抽象类GeometricObject 属性包括color colorFill
2023-06-15 13:31:14 深夜i     --     --
继承 抽象类 多态

我来分享一下我在学习《Java面向对象程序设计第三版》时整理的课后题答案。希望能对大家学习Java编程有所帮助。

以下是几个我认为比较重要的部分的答案:

## 1. 继承和多态

### 1.1

illed(),setFill()。定义类Circle和Rectangle分别继承GeometricObject并实现其方法。定义一个测试类TestGeometricObject,创建两个GeometricObject的对象circle和rectangle,然后遍历输出半径,颜色,填充状态、周长、面积。

### 1.2 答案


public abstract class GeometricObject {

  protected String color;

  protected boolean filled;

  public GeometricObject()

    color = "white";

    filled = false;

  

  public GeometricObject(String color, boolean filled)

    this.color = color;

    this.filled = filled;

  

  public boolean isFilled()

    return filled;

  

  public void setFilled(boolean filled)

    this.filled = filled;

  

  public String getColor()

    return color;

  

  public void setColor(String color)

    this.color = color;

  

  @Override

  public String toString() {

    return "GeometricObject{" +

        "color='" + color + '\'' +

        ", filled=" + filled +

        '}';

  }

  public abstract double getArea();

  public abstract double getPerimeter();

}

public class Circle extends GeometricObject {

  private double radius;

  public Circle()

    radius = 1.0;

  

  public Circle(double radius)

    this.radius = radius;

  

  public Circle(double radius, String color, boolean filled) {

    super(color, filled);

    this.radius = radius;

  }

  public double getRadius()

    return radius;

  

  public void setRadius(double radius)

    this.radius = radius;

  

  @Override

  public double getArea() {

    return Math.PI * Math.pow(radius, 2);

  }

  @Override

  public double getPerimeter() {

    return 2 * Math.PI * radius;

  }

}

public class Rectangle extends GeometricObject {

  private double width;

  private double height;

  public Rectangle()

    width = 1.0;

    height = 1.0;

  

  public Rectangle(double width, double height)

    this.width = width;

    this.height = height;

  

  public Rectangle(double width, double height, String color, boolean filled) {

    super(color, filled);

    this.width = width;

    this.height = height;

  }

  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;

  

  @Override

  public double getArea() {

    return width * height;

  }

  @Override

  public double getPerimeter() {

    return 2 * (width + height);

  }

}

public class TestGeometricObject {

  public static void main(String[] args) {

    GeometricObject circle = new Circle(5, "red", true);

    GeometricObject rectangle = new Rectangle(3, 4, "blue", false);

    System.out.println("Circle's radius is " + ((Circle)circle).getRadius());

    System.out.println("Circle's color is " + circle.getColor());

    System.out.println("Circle isFilled? " + circle.isFilled());

    System.out.println("Circle's perimeter is " + circle.getPerimeter());

    System.out.println("Circle's area is " + circle.getArea());

    System.out.println();

    System.out.println("Rectangle's width is " + ((Rectangle)rectangle).getWidth());

    System.out.println("Rectangle's height is " + ((Rectangle)rectangle).getHeight());

    System.out.println("Rectangle's color is " + rectangle.getColor());

    System.out.println("Rectangle isFilled? " + rectangle.isFilled());

    System.out.println("Rectangle's perimeter is " + rectangle.getPerimeter());

    System.out.println("Rectangle's area is " + rectangle.getArea());

  }

}

## 2. 接口和Lambda表达式

### 2.1 题目

定义接口Resizable,内含resize(double)方法,再用Resizable修改GeometricObject,然后编写一个程序ResiableCircle从Circle继承并实现Resizable接口,并且可以调整半径。

### 2.2 答案


public interface Resizable {

  void resize(double percent);

}

public abstract class GeometricObject {

  protected String color;

  protected boolean filled;

  public GeometricObject()

    color = "white";

    filled = false;

  

  public GeometricObject(String color, boolean filled)

    this.color = color;

    this.filled = filled;

  

  public boolean isFilled()

    return filled;

  

  public void setFilled(boolean filled)

    this.filled = filled;

  

  public String getColor()

    return color;

  

  public void setColor(String color)

    this.color = color;

  

  @Override

  public String toString() {

    return "GeometricObject{" +

        "color='" + color + '\'' +

        ", filled=" + filled +

        '}';

  }

  public abstract double getArea();

  public abstract double getPerimeter();

}

public class Circle extends GeometricObject {

  private double radius;

  public Circle()

    radius = 1.0;

  

  public Circle(double radius)

    this.radius = radius;

  

  public Circle(double radius, String color, boolean filled) {

    super(color, filled);

    this.radius = radius;

  }

  public double getRadius()

    return radius;

  

  public void setRadius(double radius)

    this.radius = radius;

  

  @Override

  public double getArea() {

    return Math.PI * Math.pow(radius, 2);

  }

  @Override

  public double getPerimeter() {

    return 2 * Math.PI * radius;

  }

}

public class ResizableCircle extends Circle implements Resizable {

  public ResizableCircle() {

    super();

  }

  public ResizableCircle(double radius) {

    super(radius);

  }

  public ResizableCircle(double radius, String color, boolean filled) {

    super(radius, color, filled);

  }

  @Override

  public void resize(double percent) {

    this.setRadius(this.getRadius() * percent / 100);

  }

}

public class TestResizableCircle {

  public static void main(String[] args) {

    ResizableCircle circle = new ResizableCircle(5, "red", true);

    System.out.println("Before resizing: radius=" + circle.getRadius() + ", area=" + circle.getArea());

    circle.resize(50);

    System.out.println("After resizing: radius=" + circle.getRadius() + ", area=" + circle.getArea());

  }

}

关键词:接口、Lambda表达式、多重继承

## 3. 异常处理

### 3.1 题目

编写一个简单的程序,从命令行传入一个文件名,读入该文件的内容。当文件不存在或读取失败时,抛出自定义异常FileReadFailException并处理。最后输出读取的文件内容。

### 3.2 答案


class FileReadFailException extends Exception {

  public FileReadFailException(String msg) {

    super(msg);

  }

}

public class TestFileReader {

  public static void main(String[] args) {

    try {

      FileReader file = new FileReader(args[0]);

      int ch;

      while ((ch = file.read()) != -1) {

        System.out.print((char) ch);

      }

      file.close();

    } catch (FileNotFoundException e) {

      System.out.println("File not found!");

    } catch (IOException e) {

      System.out.println("I/O Exception!");

    } catch (Exception e) {

      System.out.println("Other Exception!");

    } finally {

      System.out.println("\nFile reading finished.");

    }

  }

}

关键词:异常、自定义异常、异常处理

综上可知,学习Java编程需要不断练习做题,并深入理解Java面向对象的思想,才能够在编写程序时轻松自如。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复