Java程序第十一次作业

package com.homework09;

public class Vehicle {
	String brand;
	String color;
	double speed;
	
	Vehicle(String brand, String color) {
		this.brand = brand;
		this.color = color;
		this.speed = 0.0;
	}
	
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getSpeed() {
		return speed;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	public String getBrand() {
		return brand;
	}
	
	public void run() {
		System.out.printf("a %s %s vechicle run with speed %f\n", color, brand, speed);
	}
}

package com.homework09;

public class Car extends Vehicle {
	int loader;
	
	Car(String brand, String color, int loader) {
		super(brand, color);
		this.loader = loader;
	}
	
	public void run() {
		System.out.printf("a %s %s car with %d people run with speed %f\n", color, brand, loader, speed);
	}

}

package com.homework09;

public class Assignment01 {
    public static void main(String[] args) {
        Car car = new Car("Honda", "red", 2);
        car.run();
    }
}


image

package com.homework09;

abstract class Shape {
	protected double area;
	protected double per;
	protected String color;
	
	Shape() {}
	Shape(String color) {
		this.color = color;
	}
	
	public abstract double getArea();
	public abstract double getPer();
	public abstract void showAll();
	public abstract String getColor();
}


package com.homework09;

class Rectangle extends Shape {
	protected double width;
	protected double height;

	Rectangle() {}
	Rectangle(double width, double height, String color) {
		this.width = width;
		this.height = height;
		this.color = color;
	}

	public double getArea() {
		area = width * height;
		return area;
	}

	public double getPer() {
		per = 2 * (width + height);
		return per;
	}

	public void showAll() {
		System.out.printf("A Rectangle : width: %f height: %f area: %f per: %f color: %s\n", width, height, getArea(), getPer(), color);
	}

	public String getColor() {
		return color;
	}
}

package com.homework09;

public class Circle extends Shape {
    double radius;

    Circle() {}
    Circle(double radius, String color) {
        this.radius = radius;
        this.color = color;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {
        double PI = 3.1415926;
        return PI * radius * radius;
    }

    public double getPer() {
        double PI = 3.1415926;
        return 2 * PI * radius;
    }

    public String getColor() {
        return color;
    }

    public void showAll() {
        System.out.printf("A Circle: radius: %f area: %f per: %f color: %s \n", getRadius(), getArea(), getPer(), getColor());
    }
}

package com.homework09;

public class Assignment02 {
	public static void main(String[] args)  {
		Rectangle rectangle = new Rectangle(10, 10, "SteelBlue");
		rectangle.showAll();
		Circle circle = new Circle(10, "LightPink");
		circle.showAll();
	}
}

image

posted @ 2021-06-07 19:34  SuoJing  阅读(32)  评论(0编辑  收藏  举报