Java第十一次作业

Posted 万家乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java第十一次作业相关的知识,希望对你有一定的参考价值。

1.

package com.oop;

public class Vehicle {
    String brand;
    String color;
    double speed;

    public Vehicle() {
        super();
    }

    public Vehicle(String brand, String color, double speed) {
        super();
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    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 void run(){
        System.out.println(brand+""+color+"车的速度是"+speed);
    }
}
package com.oop;

public class Car extends Vehicle {
    private int loader;

    public Car(String brand, String color, double speed,int loader) {
        super(brand,color,speed);
        this.loader = loader;
    }

    public Car() {
        super();
    }

    public int getLoader() {
        return loader;
    }

    public void setLoader(int loader) {
        this.loader = loader;
    }

    public void run() {
        System.out.println("一辆品牌为" + brand+ "颜色为" + color + "载人数为" + loader);
    }
}
package com.oop;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Vehicle v=new Vehicle("benci", "黑色", 200);
        v.run();
        Car c=new Car("Honda","红色",100, 2);
        c.run();
    }

}

2.

package com.oop;

public abstract class Shape {
    protected double area;
    protected double per;
    protected String color;

    public Shape() {
        super();
    }

    public Shape(double area, double per, String color) {
        super();
        this.area = area;
        this.per = per;
        this.color = color;
    }

    public abstract double getArea();

    public abstract double getPer();

    public abstract void showAll();

    public void getColor() {
        System.out.println("图形的颜色是" + color);
    }
}

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

    public Rectangle() {
        super();
    }

    public Rectangle(double area, double per, String color, double width, double height) {
        super(area, per, color);
        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;
    }

    public double getArea() {
        return width * height;

    }

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

    }

    public void showAll() {
        System.out.println("矩形的长是" + width + ",矩形的宽是" + height + ",矩形的面积是" + getArea() + ",矩形的周长是" + getPer());
    }
}

class Circle extends Shape {
    private double radius;

    public Circle() {
        super();
    }

    public Circle(double area, double per, String color, double radius) {
        super(area, per, color);
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return 3.14 * radius * radius;

    }

    public double getPer() {
        return 2 * 3.14 * radius;

    }

    public void showAll() {
        System.out.println("圆形的半径是" + radius + ",圆形的面积是" + getArea() + ",圆形的周长是" + getPer());
    }
}
package com.oop;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle r = new Rectangle();
        r.setWidth(6.0);
        r.setHeight(5.0);
        r.showAll();
        Circle c = new Circle();
        c.setRadius(3.0);
        c.showAll();
    }
}

 

以上是关于Java第十一次作业的主要内容,如果未能解决你的问题,请参考以下文章

Java第十一次作业

JAVA第十一次作业

Java第十一次作业

Java第十一次作业

耿丹16-1第十一次作业

2021年春季学期-信号与系统-第十一次作业参考答案