第三次Java作业

package c
import java.util.Scanner;
class Rectangle
{
    private double length; //定义长
    private double width;  //定义宽
    public double getLength()
    {
        return length;
    }
    public double getWidth()
    {
        return width;
    }
    public Rectangle(double length, double width)
    {
       
        this.length = length;
        this.width = width;
    }
    public double getArea() //计算面积
    {
        return length*width;
    }
}
class Cuboid extends Rectangle
{
    private double height; //定义高
    public double getHeight()
    {
        return height;
    }
    public Cuboid(double length, double width, double height)
    {
        super(length, width);
        this.height = height;
    }
    public double getVolume() //计算体积
    {
        return getLength()*getWidth()*height;
    }
}
public class TestPattern
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
       
        System.out.println("请分别输入长方形的长、宽:"); //测试长方形
        Rectangle rc=new Rectangle(sc.nextDouble(),sc.nextDouble());
        System.out.println("长方形的面积为:"+rc.getArea());
        System.out.println();
       
        System.out.println("请分别输入长方体的长、宽、高:"); //测试长方体
        Cuboid cb=new Cuboid(sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
        System.out.println("长方体的底面积为:"+cb.getArea());
        System.out.println("长方体的体积为:"+cb.getVolume());
    }
}

 

 

package c
class Monkey
{
    private String s;
    public Monkey()
    {
       
    }
    public Monkey(String s)
    {
        this.s=s;
    }
    public String getS()
    {
        return s;
    }
    public void speak()
    {
        System.out.println("咿咿呀呀......");
    }
}
class People extends Monkey
{
    public void speak()
    {
        System.out.println("小样的,不错嘛!会说话了!");
    }
    public void think()
    {
        System.out.println("别说话!认真思考!");
    }   
}
public class E
{
    public static void main(String[] args)
    {
        Monkey m=new Monkey();
        People p=new People();
        m.speak();
        p.speak();
        p.think();
    }
}

 

 
import java.math.BigDecimal;
public class Rectangle {
    private BigDecimal length;
    private BigDecimal width;
    public Rectangle(BigDecimal length, BigDecimal width)
    {
        this.length = length;
        this.width = width;
    }
    public BigDecimal arithmeticArea()
    {
        return this.length.multiply(width);
    }
    public BigDecimal getLength() {
        return length;
    }
    public void setLength(BigDecimal length) {
        this.length = length;
    }
    public BigDecimal getWidth() {
        return width;
    }
    public void setWidth(BigDecimal width) {
        this.width = width;
    }
   
}
 
import java.math.BigDecimal;
public class Test {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle(new BigDecimal("5.14"), new BigDecimal("2.12"));
        Cuboid c1 = new Cuboid(new BigDecimal("8.4") , new BigDecimal("3.2") , new BigDecimal("4.1"));
        System.out.println(r1.arithmeticArea());
        System.out.println(c1.arithmeticArea());
    }
}
 
import java.math.BigDecimal;
public class Test2 {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle(20, new BigDecimal("2.8"));
        System.out.println(v1);
        Car c1 = new Car(30, new BigDecimal("3.1"), 7);
        System.out.println(c1);
        Truck t1 = new Truck(40, new BigDecimal("5.1"), 8, new BigDecimal("20"));
        System.out.println(t1);
    }
}

posted on 2019-05-13 20:44  白根宗,  阅读(194)  评论(0编辑  收藏  举报

导航