Java第三次作业

第一题:
package JavaPractice;
  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(); 
 }
 }
第二题:
package JavaPractice; 
 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 LHB.inherit;
import java.util.*;
class Vehicle
{
    int wheels,weight;/*车轮个数,车重*/
    public Vehicle(int wheels,int weight)
    {
        this.wheels=wheels;
        this.weight=weight;
    }
    public void print()
    {
        System.out.println("车轮个数是:"+wheels+"个\n"+"车重是:"+weight);
    }
}
class Car extends Vehicle
{
      int loader;/*车载人数*/
    public Car(int wheels, int weight,int loader)
    {    
        super(wheels, weight);
        this.loader=loader;
    }
    public void print()
    {
        System.out.println("车载人数是:"+loader);
    }
}
class Truck extends Car
{
    int payload;/*载重量*/
    public Truck(int wheels, int weight, int loader,int payload) 
    {
        super(wheels, weight, loader);
        this.payload=payload;
    }
    public void print()
    {
        System.out.println("载重量是:"+payload);
    }
}
public class Testcar 
{

    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        int wheels,weight,loader,payload;
        System.out.print("请输入车轮个数,车重,车载人数,载重量:");
        wheels=in.nextInt();
        weight=in.nextInt();
        loader=in.nextInt();
        payload=in.nextInt();
        Vehicle p1=new Vehicle(wheels,weight);
        Car p2=new Car(wheels,weight,loader);
        Truck p3=new Truck(wheels,weight,loader,payload);
           p1.print();
           p2.print();
           p3.print();
    }

}

 

转载于:https://www.cnblogs.com/zhangyixing/p/10848389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值