上机作业

实现如下类之间的继承关系,并编写Music类来测试这些类。

 1 package worl;
 2 class Instrument{
 3     public void play(){
 4         System.out.println("弹奏乐器");
 5     }
 6 }
 7 
 8 class Wind extends Instrument{
 9     public void play(int x){
10         System.out.println("弹奏Wind");
11     }
12     public void play_2(){
13         System.out.println("调用Wind的play_2");
14     }
15 }
16 
17 class Brass extends Instrument{
18     public void play(int x,int y){
19         System.out.println("弹奏brass");
20     }
21     public void play_2(){
22     System.out.println("调用brass的play_2");
23     }
24 }
25 
26 public class worl_1 {
27     public static void tune(Instrument x){
28         x.play();
29     }
30     public static void main(String[] args) {
31         Wind a=new Wind();
32         a.play(2);
33         a.play_2();
34         Brass b=new Brass();
35         b.play(2,3);
36         b.play_2();
37     }
38 
39 }

编写一个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 worl;
 2 class Monkey{
 3     
 4     public void speak(){
 5         System.out.println("das g asd asdqw a ");
 6     }
 7 }
 8 class People extends Monkey{
 9 
10     
11     public void speak(){
12         System.out.println("你居然会说话");
13     }
14     public void think(){
15         System.out.println("认真思考");
16     }
17 }
18 
19 public class worl_2 {
20 
21     public static void main(String[] args) {
22     Monkey a=new Monkey();
23     a.speak();
24     People b=new People();
25     b.speak();
26     b.think();
27     }
28 
29 }

按要求编写一个Java应用程序:

(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。

(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。

(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。

 1 package worl;
 2 
 3 import java.util.*;
 4 
 5 class type{
 6         int Long;
 7         int Wide;
 8     public void detail(int Long,int Wide){
 9         float S;
10         S=Long*Wide;
11         System.out.println("面积为"+S);
12     }
13 }
14 class other extends type{
15     int Long;
16     int Hige;
17     int Wide;
18     public void detail(int Long,int Hige,int Wide){
19         float S;
20         S=Long*Hige*Wide;
21         System.out.println("面积为"+S);
22     }
23 }
24 
25 public class worl_3 {
26 
27     public static void main(String[] args) {
28         Scanner A=new Scanner(System.in);
29         Scanner B=new Scanner(System.in);
30         Scanner C=new Scanner(System.in);
31         int Long=A.nextInt();
32         int Hige=B.nextInt();
33         int Wide=C.nextInt();
34         type a=new type();
35         a.detail(Long, Wide);
36         other b=new other();
37         b.detail(Long, Hige, Wide);
38     }
39 
40 }

编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。

 1 package worl;
 2 import java.util.*;
 3 class Vehicle{
 4     int wheels;
 5     int weight;
 6     public Vehicle(){
 7         Scanner a=new Scanner(System.in);
 8         Scanner b=new Scanner(System.in);
 9         this.wheels=a.nextInt();
10         this.weight=b.nextInt();
11     }
12     public void sys(){
13         System.out.println("车轮个数为"+wheels);
14         System.out.println("车重为"+weight);
15     }
16 }
17 class cars extends Vehicle{
18     int loader;
19     public cars(){
20     Scanner a=new Scanner(System.in);
21     this.loader=a.nextInt();
22     }
23     public void sys(){
24         System.out.println("乘客个数为"+loader);
25     }
26 }
27 class truck extends Vehicle{
28     int payload;
29     public truck(){
30     Scanner a=new Scanner(System.in);
31     this.payload=a.nextInt();
32     }
33     public void sys(){
34         System.out.println("装载重量为"+payload);
35     }
36 }
37 public class worl_4 {
38 
39     public static void main(String[] args) {
40         Vehicle a=new Vehicle();
41         a.sys();
42         cars b=new cars();
43         b.sys();
44         truck c=new truck();
45         c.sys();
46         
47     }
48 }

 

posted @ 2019-05-05 12:20  一个人的姐姐  阅读(271)  评论(0编辑  收藏  举报