Java程序设计实验报告
第六次实验设计
题目:基于类继承和多态,在我们上午课程Shapes程序的基础上编写一个画图程序,在已有Shape、Line、Circle、Rectangle、Triangle的基础上增加Square和Ellipse两种新的类,并支持图形面积和周长的计算(Line面积设置为0)。
提示:
答:正方形是一种矩形,但 是不应该定义一个 Square 类来扩展 Rectangle 类,因为 width 和 height 属性并不适 合于正方形。应该定义一个继承自 Shape 类的 Square 类,并为正方形的 边定义一个 side 属性。
- 为类Sharp增加抽象函数getArea(), getPerimeter(),在子类中实现它们。
- 从数学上,正方形是一种特殊的矩形。但是,从程序实现上考虑,正方形只要一个边长,而矩形可以从正方形继承后再增加一个边长成员变量。另一方面来说,如果正方形从矩形继承,那么就会出现冗余的第二个边长变量
二、实验运行结果
注意:将程序运行结果截图粘贴在此处
三、程序源代码
注意:将程序代码粘贴在此处,注意源代码中注释行数不少于全部代码的1/5
1. MyPic类:
package shapes;
public class MyPic {
public static void main(String[] args)
{
Picture pic = new Picture(420,300);
//新建图形
Ellipse e1 = new Ellipse(50,50,50,60);//新增的椭圆
Circle c1 = new Circle(320,40,80);
Square square = new Square(350,150,50);//新增的正方形
Rectangle r1 = new Rectangle(100, 100, 100, 100);
Triangle t1 = new Triangle(100, 100, 200, 100, 150, 50);
Line l1 = new Line(0,205,400,205);
pic.add(c1);
pic.add(r1);
pic.add(t1);
pic.add(l1);
//把新增的加入进去
pic.add(square);
pic.add(e1);
//画图
pic.draw();
//对面积计算的测试
System.out.println("椭圆e1的面积:"+e1.getArea()+",周长"+e1.getPerimeter());
System.out.println("圆c1的面积:"+c1.getArea()+",周长"+c1.getPerimeter());
System.out.println("正方形square的面积:"+square.getArea()+",周长"+square.getPerimeter());
System.out.println("长方形r1的面积:"+square.getArea()+",周长"+square.getPerimeter());
System.out.println("线l1的面积:"+l1.getArea()+",周长"+l1.getPerimeter());
System.out.println("三角形t1的面积:"+t1.getArea()+",周长"+t1.getPerimeter());
}
}
2. Picture类
package shapes;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Picture extends JFrame {
private static final long serialVersionUID = 1L;
private int width;
private int height;
private ArrayList<Shape> listShape = new ArrayList<Shape>();
private class ShapesPanel extends JPanel {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for ( Shape s : listShape )
{
s.draw(g);
}
}
}
public void add(Shape s)
{
listShape.add(s);
}
public Picture(int width, int height)
{
add(new ShapesPanel());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.width = width;
this.height = height;
}
public void draw()
{
setLocationRelativeTo(null);
setSize(width, height);
setVisible(true);
}
}
3. Shape类
package shapes;
import java.awt.Graphics;
public abstract class Shape {
public abstract void draw(Graphics g);
//计算周长和面积的抽象函数
public abstract double getPerimeter();
public abstract double getArea();
public static void main(String[] args) {
}
}
4. Line类
package shapes;
import java.awt.Graphics;
public class Line extends Shape {
private int x1;
private int y1;
private int x2;
private int y2;
public Line(int x1, int y1, int x2, int y2)
{
this.x1 = x1; this.y1 = y1;
this.x2 = x2; this.y2 = y2;
}
//抽象函数的重载
@Override
public void draw(Graphics g) {
g.drawLine(x1, y1, x2, y2);
}
@Override
public double getPerimeter() {
return Math.sqrt((y1-y2)*(y1-y2) + (x1-x2)*(x1-x2));
}
@Override
public double getArea() {
return 0;
}
}
5. Square类
package shapes;
import java.awt.*;
public class Square extends Shape {
public int x,y;
public int side;
//构造函数
public Square() {
}
public Square(int x, int y, int side) {
this.x = x;
this.y = y;
this.side = side;
}
@Override
public void draw(Graphics g) {
g.drawRect(x,y,side,side);
}
@Override
public double getPerimeter() {
return 4 * side;
}
@Override
public double getArea() {
return side *side;
}
}
6. Rectangle类
package shapes;
import java.awt.Graphics;
public class Rectangle extends Shape {
private int x;
private int y;
private int width;
private int height;
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public void draw(Graphics g) {
g.drawRect(x, y, width, height);
}
//重写抽象方法
@Override
public double getPerimeter() {
return 2 * (width + height);
}
@Override
public double getArea() {
return width * height;
}
}
7. Ellipse类
package shapes;
import java.awt.*;
public class Ellipse extends Shape{
public int x;
public int y;
public int a;
public int b;
public Ellipse(int x, int y, int a, int b) {
this.x = x;
this.y = y;
this.a = a;
this.b = b;
}
@Override
public void draw(Graphics g) {
g.drawOval(x,y,a,b);
}
@Override
public double getPerimeter() {
return 2 * Math.PI + 4 * (a - b);
}
@Override
public double getArea() {
return Math.PI * a * b;
}
}
8. Circle类
package shapes;
import java.awt.Graphics;
public class Circle extends Ellipse {
public Circle(int x, int y, int radius)
{
super(x,y,radius,radius);
}
}
9.Triangle类
package shapes;
import java.awt.Graphics;
public class Triangle extends Shape {
private int[] x = new int[3];
private int[] y = new int[3];
public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
x[0] = x1; x[1] = x2; x[2] = x3;
y[0] = y1; y[1] = y2; y[2] = y3;
}
@Override
public void draw(Graphics g) {
g.drawPolygon(x, y, x.length);
}
@Override
public double getPerimeter() {
double s1 = Math.sqrt(Math.pow(x[1] - x[2], 2) + Math.pow(y[1] - y[2], 2));
double s2 = Math.sqrt(Math.pow(x[1] - x[0], 2) + Math.pow(y[1 ]- y[0], 2));
double s3 = Math.sqrt(Math.pow(x[0] - x[2], 2) + Math.pow(y[0] - y[2], 2));
return s1+s2+s3;
}
//利用海伦公式计算面积
@Override
public double getArea() {
double s1 = Math.sqrt(Math.pow(x[1] - x[2], 2) + Math.pow(y[1] - y[2], 2));
double s2 = Math.sqrt(Math.pow(x[1] - x[0], 2) + Math.pow(y[1 ]- y[0], 2));
double s3 = Math.sqrt(Math.pow(x[0] - x[2], 2) + Math.pow(y[0] - y[2], 2));
double s = (s1 + s2 + s3) / 2;
return Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
}
}