现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,具体要求如下:
(1)在基类中记录狗的品种breed,体重weight以及颜色color等属性,定义一个方法show()显示Dog信息;
(2)在UnspottedDog类中,调用Dog类的构造方法,重写show()方法,只显示狗的品种;
(3)在SpottedDog类中,新增表示斑点颜色的spotColor属性,定义包含四个属性的构造方法,重写show()方法;
(4)定义测试类,构造斑点狗对象,分别显示斑点狗的品种、体重、颜色和品种、颜色、斑点颜色;构造非斑点狗对象,显示狗的品种、体重、颜色信息。
(说明:构造斑点狗对象和非斑点狗对象时要分别输入,各属性值之间用空格分割,输入完后按回车键确认,输入内容参照测试数据。)
import java.util.Scanner; class Dog{ String breed; int weight; String color; public void show(){ } } class UnspottedDogg extends Dog { UnspottedDogg(String breed,int wegiht,String color){ this.breed=breed; this.weight=wegiht; this.color=color; } public void show(){ System.out.println("这是一只"+breed+"犬"); } } class SpottedDog extends Dog{ String spotColor; SpottedDog(String breed,int wegiht,String color,String spotColor){ this.breed=breed; this.weight=wegiht; this.color=color; this.spotColor=spotColor; } public void show(){ System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color ); System.out.println("这是一只"+breed+",颜色为"+color+",斑点颜色为"+spotColor); } } public class Main{ public static void main(String[] args) { String breed1,breed2; int weight1,weight2; String color1,color2; String spotcolor; Scanner in =new Scanner(System.in); breed1=in.next(); weight1=in.nextInt(); color1=in.next(); spotcolor=in.next(); breed2=in.next(); weight2=in.nextInt(); color2=in.next(); SpottedDog d1=new SpottedDog(breed1,weight1,color1,spotcolor); d1.show(); UnspottedDogg d2=new UnspottedDogg(breed2,weight2,color2); d2.show(); } }
编写一个制造各种车辆的程序。包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。
import java.util.Scanner; class Vehicle{ int num; double weight; public Vehicle(int num,double weight){ this.num=num; this.weight=weight; } public void show(){ System.out.println("汽车:"); System.out.println("轮子数:"+num+"个"); System.out.println("自身重量:"+weight+"吨"); } public Vehicle(){ } } class Car extends Vehicle{ int Passengers; public Car(int num,double weight,int Passengers){ super(num,weight); this.Passengers=Passengers; } public void show(){ System.out.println("小轿车:"); System.out.println("轮子数:"+num+"个"); System.out.println("自身重量:"+weight+"吨"); System.out.println("额定乘客数:"+Passengers+"人"); } public Car(){ } } class Truck extends Car{ double Cargo; public Truck(int num,double weight,int Passsengers,double Cargo){ super(num,weight,Passsengers); this.Cargo=Cargo; } public void show(){ System.out.println("卡车:"); System.out.println("轮子数:"+num+"个"); System.out.println("自身重量:"+weight+"吨"); System.out.println("额定乘客数"+Passengers+"人"); System.out.println("载重量"+Cargo+"吨"); } } public class Main { public static void main(String[] args) { // 16 5.4 4 1.5 5 6 4 4 10 int num1,num2,passenger2,num3,passenger3; double weight1,weight2,weight3,cargo3; Scanner in =new Scanner(System.in); num1=in.nextInt(); weight1=in.nextDouble(); num2=in.nextInt(); weight2=in.nextDouble(); passenger2=in.nextInt(); num3=in.nextInt(); weight3=in.nextInt(); passenger3=in.nextInt(); cargo3=in.nextDouble(); Vehicle v1=new Vehicle(num1,weight1); v1.show(); Car v2=new Car(num2,weight2,passenger2); v2.show(); Truck v3=new Truck(num3,weight3,passenger3,cargo3); v3.show(); } }
使用接口或者抽象类编写程序实现显示员工基本信息。具体要求如下:
(1)使用接口或者抽象类实现基类Employer(体会接口和抽象类的不同),包含姓名、部门和工资三个属性,显示工资的方法showSalary()和显示奖金的抽象方法showBonus();提示:因每位职工奖金不同,showBonus()方法定义为抽象方法,只抽象定义,不具体实现;(2)定义BasicEmployee和GoodEmployee类,重写Employer类中的方法,不同员工有不同的工资和奖金;
(3)定义主类进行测试,要求输入两个不同的员工信息,并输出其个人信息。
import java.util.Scanner; abstract class Employer{ String name,department; double wages; double bonus; public abstract void showSaraly(); public abstract void showBonus(); } class BasicEmployee extends Employer{ public BasicEmployee(String name,String department,double wages){ this.name=name; this.department=department; this.wages=wages; } public void showSaraly() { System.out.println("我叫"+this.name+",在"+this.department+"部门,我的工资是"+this.wages); } public void showBonus() { System.out.println("我是普通员工,没有奖金,加油升级!"); } } class GoodEmployee extends Employer{ public GoodEmployee(String name,String department,double wages,double bonus){ this.name=name; this.department=department; this.wages=wages; this.bonus=bonus; } public void showSaraly() { System.out.printf("我叫%s,在%s部门,我的工资是%.1f\n",this.name,this.department,this.wages); } public void showBonus() { System.out.println("我是优秀员工,我的奖金是"+this.bonus); } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String name[] = new String[2]; String department[] = new String[2]; double wages[] = new double[2]; name[0]=in.next(); department[0]=in.next(); wages[0]=in.nextDouble(); name[1]=in.next(); department[1]=in.next(); wages[1]=in.nextDouble(); double bonus=in.nextDouble(); BasicEmployee e1=new BasicEmployee(name[0],department[0],wages[0]); GoodEmployee e2=new GoodEmployee(name[1],department[1],wages[1],bonus); e1.showSaraly(); e1.showBonus(); e2.showSaraly(); e2.showBonus(); } }
编写一个教师讲课的程序。所有老师都具有共同的讲课方法,但是不同科目的教师讲课内容不同,主程序中编写一个讲课的方法TeachingRace(Teacher t),显示不同的老师t讲授不同的课程内容。提示:
(1)所有老师具有共同的讲课方法,可在接口中定义一个讲课方法;
(2)不同科目的老师实现接口中的讲课方法;
(3)在主程序中定义一个讲课的方法TeachingRace(Teacher t),构造不同的教师,显示讲课内容【主要考察接口回调】
import java.util.Scanner; interface Teacher{ void method(); } class English implements Teacher{ String say; English(String say){ this.say=say; } public void method() { System.out.println("我是英语老师,I say "+this.say); } } class Math implements Teacher{ String say; Math(String say){ this.say=say; } public void method() { System.out.println("我是数学老师,I say "+this.say); } } class Text{ void TearchingRace(Teacher t){ t.method(); } } public class Main { public static void main(String[] args) { Scanner in =new Scanner(System.in); Text t=new Text(); String s; s=in.next(); t.TearchingRace(new English(s)); s=in.next(); t.TearchingRace(new Math(s)); } }
创建Animal(动物)类:Mouse,dog等的一个继承分级结构.在父类中提供适用于所有Animal的方法,并在子类中覆盖他们,从而根据不同类型的Animal采取不同的行动
Anima类有如下方法:
public void speak();
import java.util.Scanner; class Animal{ String name; public void speak(){ } } class Mouse extends Animal{ Mouse(String name){ this.name=name; } // @Override public void speak() { System.out.println(this.name+"的叫声为喵喵"); } } class Dog1 extends Animal{ Dog1(String name){ this.name=name; } // @Override public void speak() { System.out.println(this.name+"的叫声为吱吱"); } } public class Main{ public static void main(String[] args) { String name[]=new String[2]; Scanner in=new Scanner(System.in); name[0]=in.next(); name[1]=in.next(); Mouse m=new Mouse(name[0]); Dog1 d=new Dog1(name[1]); m.speak(); d.speak(); } }
编写一个USB接口程序,模拟计算机启动过程和关闭过程启动过程中要加载鼠标、键盘、麦克风等USB设备,具体要求如下:
(1)定义一个接口USB,包含两个抽象方法turnOn()he turnOff(),分别用于表示USB设备的启动和关闭
(2)编写鼠标Mouse、键盘KeyBoard、麦克风Mic类,实现接口中的turnOn()、turnOff()方法,方法中显示“XX设备启动了”或“XX设备关闭了”即可
(3)编写计算机类Computer,要求有一个表示计算机上USB插槽数量的数组;添加USB设备的方法add(USB usb),功能为遍历所有插槽,如果有空闲的就添加一个USB设备
模拟开机启动USB设备的powerOn()方法,功能为遍历所有USB接口,如果连接了USB设备,则启动USB设备,然后显示“计算机开机成功”
模拟关机关闭USB设备的powerOff()方法,功能为遍历所有USB接口,如果连接了USB设备,则关闭USB设备,然后显示“计算机关机成功”
(4)编写测试类,要求建立计算机对象,建立鼠标、键盘、麦克风对象,并添加到计算机中,启动计算机,关闭计算机*/
import java.util.Scanner; interface USB{ void turnon(); void turnoff(); } class Mouse1 implements USB{ public void turnon() { System.out.println("鼠标启动了"); } public void turnoff() { System.out.println("鼠标关闭了"); } } class KeyBoard implements USB{ public void turnon() { System.out.println("键盘启动了"); } public void turnoff() { System.out.println("键盘关闭了"); } } class Mic implements USB{ public void turnon() { System.out.println("麦克启动了"); } public void turnoff() { System.out.println("麦克关闭了"); } } class Computer{ int num=0; USB [] equipment=new USB[10]; public void add( USB t){ if(num<=10){ equipment[num]=t; num++; } } void poweron(){ for(int i=0;i
编写一个通过接口实现不同应用情况下计算平均分的程序,具体要求如下:
* 1、 编写一个ComputerAverage接口,接口有一个求平均分的抽象方法average,方法的参数为double类型的数组。
* 2、定义Gymnastics类和School类,它们都是ComputerAverage的实现类,Gymnastics类中的平均分方法为计算体育比赛中选手的平均成绩,具体算法是去掉一个最低分,去掉一个最高分,然后对剩余的数求平均分。* 3、School类中的平均分为计算学生考试成绩的平均分,具体算法是分数的和除以总的科目数
* 4、要求:在主类中声明ComputerAverage的对象,并使用为上转型对象,实现ComputerAverage的对象调用average方法, 实现多态,同样的两条语句实现两种不同计算平均分的方法。输入的成绩为一组数,数据的个数和具体的数据从键盘输入
import java.util.Scanner; interface ComputerAverage { //start //write your code between start and end,do not delete any code double average( double a[]); //end } class Gymnastics implements ComputerAverage { public double average(double []data) { double sum=0; double temp; //start //write your code between start and end,do not delete any code double max,min; max=min=data[0]; for(int i=0;i
data[i]) { min = data[i]; } sum += data[i]; } sum=sum-max-min; temp=sum/ (data.length-2); //end return temp; } } class School implements ComputerAverage{ double sum=0; public double average(double[] data) { for(int i=0;i< data.length;i++){ sum+=data[i]; } return sum/ data.length; } //start //write your code between start and end,do not delete any code //end } public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); double []data=new double[n]; for(int i=0;i
有如下类:
interface Paper { public String GetName(); }class Printer { public void Print(Paper p) { System.out.println(p.GetName()); } }要求创建 A4Paper,A6Paper两个类,实现Paper接口,并实现GetName方法在main方法中创建Printer对象,并调用Print()方法两次,分别传入A4,A6类的对象,根据已有代码完成程序
import java.util.Scanner; interface Paper { public String GetName(); } class Printer { public void Print(Paper p) { System.out.println(p.GetName()); } } class A4paper implements Paper{ public String GetName() { return "A4"; } } class A6paper implements Paper{ public String GetName() { return "A6"; } } public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); String s[]=new String[2]; s[0]=in.next(); s[1]=in.next(); Printer p=new Printer(); p.Print(new A4paper()); p.Print(new A6paper()); } }