1.编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类E。要求:
(1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀......”的信息。
(2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。(3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。(4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。1 package LHB.inherit; 2 import java.util.*; 3 class Monkey 4 { String s; 5 public Monkey(String s) 6 { 7 this.s=s; 8 } 9 public void speak()10 {11 System.out.println("好嗨哟!");12 }13 }14 class People extends Monkey15 {16 public People(String s) {17 super(s);18 }19 public void speak()20 {21 System.out.println("感觉人生已经到达了巅峰");22 }23 public void think()24 {25 System.out.println("你好牛哦,你才是真正的大人物");26 }27 28 }29 public class E 30 {31 public static void main(String[] args) 32 { 33 Scanner in=new Scanner(System.in);34 String s;35 s=in.next();36 Monkey p=new Monkey(s);37 People q=new People(s);38 p.speak();39 q.speak();40 q.think();41 }42 43 }
2.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。1 package LHB.inherit; 2 import java.util.*; 3 class Rectangle 4 { 5 int length,width; 6 public int area(int length,int width)/*矩形面积*/ 7 { 8 this.length=length; 9 this.width=width;10 int s;11 s=length*width;12 return s;13 }14 }15 class Cuboid extends Rectangle16 {17 int length,width,heigth;18 public Cuboid(int length,int width,int heigth)19 {20 this.length=length;21 this.width=width;22 this.heigth=heigth;23 }24 public int area()/*长方体底面积*/25 {26 int s;27 s=length*width;28 return s;29 }30 public int Volume()/*长方体体积*/31 {32 int v;33 v=area()*heigth;34 return v;35 }36 37 }38 public class Test 39 {40 41 public static void main(String[] args) 42 {43 int length,width,heigth;44 Scanner in=new Scanner(System.in);45 System.out.print("请输入长宽高:");46 length=in.nextInt();47 width=in.nextInt();48 heigth=in.nextInt();49 Rectangle p=new Rectangle();50 Cuboid q=new Cuboid(length,width,heigth);51 System.out.println("矩形的面积是:"+p.area(length, width));52 System.out.println("长方体的体积是:"+q.Volume());53 54 }55 56 }
3.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。
1 package LHB.inherit; 2 import java.util.*; 3 class Vehicle 4 { 5 int wheels,weight;/*车轮个数,车重*/ 6 public Vehicle(int wheels,int weight) 7 { 8 this.wheels=wheels; 9 this.weight=weight;10 }11 public void print()12 {13 System.out.println("车轮个数是:"+wheels+"个\n"+"车重是:"+weight);14 }15 }16 class Car extends Vehicle17 {18 int loader;/*车载人数*/19 public Car(int wheels, int weight,int loader)20 { 21 super(wheels, weight);22 this.loader=loader;23 }24 public void print()25 {26 System.out.println("车载人数是:"+loader);27 }28 }29 class Truck extends Car30 {31 int payload;/*载重量*/32 public Truck(int wheels, int weight, int loader,int payload) 33 {34 super(wheels, weight, loader);35 this.payload=payload;36 }37 public void print()38 {39 System.out.println("载重量是:"+payload);40 }41 }42 public class Testcar 43 {44 45 public static void main(String[] args)46 {47 Scanner in=new Scanner(System.in);48 int wheels,weight,loader,payload;49 System.out.print("请输入车轮个数,车重,车载人数,载重量:");50 wheels=in.nextInt();51 weight=in.nextInt();52 loader=in.nextInt();53 payload=in.nextInt();54 Vehicle p1=new Vehicle(wheels,weight);55 Car p2=new Car(wheels,weight,loader);56 Truck p3=new Truck(wheels,weight,loader,payload);57 p1.print();58 p2.print();59 p3.print();60 }61 62 }