Java第三次作业
作者:互联网
package LHB.inherit; import java.util.*; class Monkey { String s; public Monkey(String s) { this.s=s; } public void speak() { System.out.println("好嗨哟!"); } } class People extends Monkey { public People(String s) { super(s); } public void speak() { System.out.println("刘海哥"); } public void think() { System.out.println("王大姐"); } } public class E { public static void main(String[] args) { Scanner in=new Scanner(System.in); String s; s=in.next(); Monkey p=new Monkey(s); People q=new People(s); p.speak(); q.speak(); q.think(); } }
package LHB.inherit; import java.util.*; class Monkey { String s; public Monkey(String s) { this.s=s; } public void speak() { System.out.println("好嗨哟!"); } } class People extends Monkey { public People(String s) { super(s); } public void speak() { System.out.println("刘海哥"); } public void think() { System.out.println("王大姐"); } } public class E { public static void main(String[] args) { Scanner in=new Scanner(System.in); String s; s=in.next(); Monkey p=new Monkey(s); People q=new People(s); p.speak(); q.speak(); q.think(); } }
2.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
import java.util.*; class Rectangle { int length,width; public int area(int length,int width)/*矩形面积*/ { this.length=length; this.width=width; int s; s=length*width; return s; } } class Cuboid extends Rectangle { int length,width,heigth; public Cuboid(int length,int width,int heigth) { this.length=length; this.width=width; this.heigth=heigth; } public int area()/*长方体底面积*/ { int s; s=length*width; return s; } public int Volume()/*长方体体积*/ { int v; v=area()*heigth; return v; } } public class Test { public static void main(String[] args) { int length,width,heigth; Scanner in=new Scanner(System.in); System.out.print("请输入长宽高:"); length=in.nextInt(); width=in.nextInt(); heigth=in.nextInt(); Rectangle p=new Rectangle(); Cuboid q=new Cuboid(length,width,heigth); System.out.println("矩形的面积是:"+p.area(length, width)); System.out.println("长方体的体积是:"+q.Volume()); } }
3.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能
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(); } }
2.按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性,和计算体积的方法。
import java.util.*; class Rectangle { int length,width; public int area(int length,int width)/*矩形面积*/ { this.length=length; this.width=width; int s; s=length*width; return s; } } class Cuboid extends Rectangle { int length,width,heigth; public Cuboid(int length,int width,int heigth) { this.length=length; this.width=width; this.heigth=heigth; } public int area()/*长方体底面积*/ { int s; s=length*width; return s; } public int Volume()/*长方体体积*/ { int v; v=area()*heigth; return v; } } public class Test { public static void main(String[] args) { int length,width,heigth; Scanner in=new Scanner(System.in); System.out.print("请输入长宽高:"); length=in.nextInt(); width=in.nextInt(); heigth=in.nextInt(); Rectangle p=new Rectangle(); Cuboid q=new Cuboid(length,width,heigth); System.out.println("矩形的面积是:"+p.area(length, width)); System.out.println("长方体的体积是:"+q.Volume()); } }
3.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能
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(); } }
标签:Java,第三次,weight,int,作业,width,length,wheels,public 来源: https://www.cnblogs.com/durjc/p/10884215.html