一、实验目的
1.掌握类的继承方法及上转型对象的方法调用。
2.掌握this和super的区别及使用。
3.理解抽象类的概念及作用,掌握接口的声明,实现及接口回调。
二、实验内容
7173
现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,具体要求如下:
(1)在基类中记录狗的品种breed,体重weight以及颜色color等属性,定义一个方法show()显示Dog信息;
(2)在UnspottedDog类中,调用Dog类的构造方法,重写show()方法,只显示狗的品种;
(3)在SpottedDog类中,新增表示斑点颜色的spotColor属性,定义包含四个属性的构造方法,重写show()方法;
(4)定义测试类,构造斑点狗对象,分别显示斑点狗的品种、体重、颜色和品种、颜色、斑点颜色;构造非斑点狗对象,显示狗的品种、体重、颜色信息。
(说明:构造斑点狗对象和非斑点狗对象时要分别输入,各属性值之间用空格分割,输入完后按回车键确认,输入内容参照测试数据。)。
package test;
import java.util.*;
import java.io.*;
import java.math.*;
class Dog
{
String breed;
int weight;
String color;
Dog(String breed,int weight,String color)
{
this.breed=breed;
this.weight=weight;
this.color=color;
};
public void show()
{
System.out.println("这是一只"+breed+",体重为"+weight+",颜色为"+color);
}
}
class UnspottedDog extends Dog
{
UnspottedDog(String breed, int weight, String color)
{
super(breed, weight, color);
}
public void show()
{
System.out.println("这是一只"+breed+"犬");
}
}
class SpottedDog extends Dog
{
String spotColor;
SpottedDog(String breed,int weight,String color,String spotColor)
{
super(breed, weight, color);
super.show();
this.spotColor=spotColor;
};
public void show()
{
System.out.println("这是一只"+breed+",颜色为"+color+",斑点颜色为"+spotColor);
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String breed1=scanner.next();;
int weight1=scanner.nextInt();
String color1=scanner.next();
String spotColor1=scanner.next();
String breed2=scanner.next();;
int weight2=scanner.nextInt();
String color2=scanner.next();
SpottedDog spottedDog=new SpottedDog(breed1,weight1,color1,spotColor1);
spottedDog.show();
UnspottedDog unspottedDog=new UnspottedDog(breed2, weight2, color2);
unspottedDog.show();
}
}
结果:
7174
编写一个制造各种车辆的程序。包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。
package test;
import java.util.*;
import java.math.*;
class Vehicle
{
int num;
double weight;
Vehicle(int num,double weight)
{
this.num=num;
this.weight=weight;
}
public void output()
{
System.out.println("汽车:\n"+"轮子数量:"+num);
System.out.println("重量:"+weight);
}
}
class Car extends Vehicle
{
int person;
Car(int num,double weight,int person)
{
super(num,weight);
this.person=person;
}
public void output()
{
System.out.println("小轿车:\n"+"轮子数量:"+num);
System.out.println("重量:"+weight);
System.out.println("载客数:"+person);
}
}
class Truck extends Vehicle
{
int person;
double cargo_Capacity;
Truck(int num,double weight,int person,double cargo_Capacity)
{
super(num,weight);
this.person=person;
this.cargo_Capacity=cargo_Capacity;
}
public void output()
{
System.out.println("卡车:\n"+"轮子数量:"+num);
System.out.println("重量:"+weight);
System.out.println("载客数:"+person);
System.out.println("载货量:"+cargo_Capacity);
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
double weight=scanner.nextDouble();
Vehicle vehicle=new Vehicle(num,weight);
num=scanner.nextInt();
weight=scanner.nextDouble();
int person=scanner.nextInt();
Car car=new Car(num,weight,person);
num=scanner.nextInt();
weight=scanner.nextDouble();
person=scanner.nextInt();
double cargo_Capacity=scanner.nextDouble();
Truck truck=new Truck(num,weight,person,cargo_Capacity);
vehicle.output();
car.output();
truck.output();
}
}
结果:
7175
使用接口或者抽象类编写程序实现显示员工基本信息。具体要求如下:
(1)使用接口或者抽象类实现基类Employer(体会接口和抽象类的不同),包含姓名、部门和工资三个属性,显示工资的方法showSalary()和显示奖金的抽象方法showBonus();
提示:因每位职工奖金不同,showBonus()方法定义为抽象方法,只抽象定义,不具体实现;
(2)定义BasicEmployee和GoodEmployee类,重写Employer类中的方法,不同员工有不同的工资和奖金;
(3)定义主类进行测试,要求输入两个不同的员工信息,并输出其个人信息。
package test;
import java.util.*;
import java.math.*;
abstract class Employer
{
String name;
String department;
double salary;
double bonus;
abstract void showSalary();
abstract void showBonus();
}
class BasicEmployee extends Employer
{
BasicEmployee(String name,String department,double salary)
{
this.name=name;
this.department=department;
this.salary=salary;
}
void showSalary()
{
System.out.printf("我叫%s,在%s部门,我的工资是%.1f\n",this.name,this.department,this.salary);
}
void showBonus()
{
System.out.printf("我是普通员工,没有奖金,加油升级!\n");
}
}
class GoodEmployee extends Employer
{
GoodEmployee(String name,String department,double salary,double bonus)
{
this.name=name;
this.department=department;
this.salary=salary;
this.bonus=bonus;
}
void showSalary()
{
System.out.printf("我叫%s,在%s部门,我的工资是%.1f\n",this.name,this.department,this.salary);
}
void showBonus()
{
System.out.printf("我是优秀员工,我的奖金是%.1f\n",this.bonus);
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Employer e1,e2;
String name=scanner.next();
String department=scanner.next();
double salary=scanner.nextDouble();
e1=new BasicEmployee(name,department,salary);
name=scanner.next();
department=scanner.next();
salary=scanner.nextDouble();
double bonus=scanner.nextDouble();
e2=new GoodEmployee(name,department,salary,bonus);
e1.showSalary();
e1.showBonus();
e2.showSalary();
e2.showBonus();
}
}
结果:
7176
编写一个教师讲课的程序。所有老师都具有共同的讲课方法,但是不同科目的教师讲课内容不同,主程序中编写一个讲课的方法TeachingRace(Teacher t),显示不同的老师t讲授不同的课程内容。提示:
(1)所有老师具有共同的讲课方法,可在接口中定义一个讲课方法;
(2)不同科目的老师实现接口中的讲课方法;
(3)在主程序中定义一个讲课的方法TeachingRace(Teacher t),构造不同的教师,显示讲课内容【主要考察接口回调】。
package test;
import java.util.*;
import java.math.*;
interface Teacher
{
void method();
}
class English implements Teacher
{
String way;
English(String way){
this.way=way;
}
public void method()
{
System.out.println("我是英语老师,I say "+this.way);
}
}
class Math implements Teacher
{
String way;
Math(String way){
this.way=way;
}
public void method()
{
System.out.println("我是数学老师,I say "+this.way);
}
}
class Chinese implements Teacher
{
String way;
Chinese(String way){
this.way=way;
}
public void method()
{
System.out.println("我是语文老师,I say "+this.way);
}
}
class Text{
void TearchingRace(Teacher t){
t.method();
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Text t=new Text();
String s=scanner.next();
t.TearchingRace(new English(s));
s=scanner.next();
t.TearchingRace(new Math(s));
/* s=scanner.next();
t.TearchingRace(new Chinese(s));*/
}
}
结果:
7177
创建Animal(动物)类:Mouse,dog等的一个继承分级结构.在父类中提供适用于所有Animal的方法,并在子类中覆盖他们,从而根据不同类型的Animal采取不同的行动Anima类有如下方法:public void speak()。
package test;
import java.util.*;
import java.math.*;
class Animal{
String name;
public void speak() {};
}
class Mouse extends Animal{
Mouse(String name){
this.name=name;
}
public void speak(){
System.out.println(this.name+"的叫声为吱吱");
}
}
class Cat extends Animal{
Cat(String name){
this.name=name;
}
public void speak(){
System.out.println(this.name+"的叫声为喵喵");
}
}
class Dog extends Animal{
Dog(String name){
this.name=name;
}
public void speak(){
System.out.println(this.name+"的叫声为汪汪");
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String name;
name=scanner.next();
Mouse mouse=new Mouse(name);
name=scanner.next();
Cat cat=new Cat(name);
name=scanner.next();
Dog dog=new Dog(name);
mouse.speak();
cat.speak();
dog.speak();
}
}
结果:
7178
编写一个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)编写测试类,要求建立计算机对象,建立鼠标、键盘、麦克风对象,并添加到计算机中,启动计算机,关闭计算机
package test;
import java.util.*;
import java.math.*;
interface USB{
void turnOn();
void turnOff();
}
class Mouse 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[] usb=new USB[10];
public void add(USB t){
if(num<=10){
usb[num]=t;
num++;
}
}
public void powerOn(){
for(int i=0;i<num;i++){
usb[i].turnOn();
}
System.out.println("计算机开机成功");
}
public void powerOff(){
for(int i=0;i<num;i++){
usb[i].turnOff();
}
System.out.println("计算机关机成功");
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
Computer d =new Computer();
d.add(new Mouse());
d.add(new KeyBoard());
d.add(new Mic());
d.powerOn();
d.powerOff();
}
}
5950
编写一个通过接口实现不同应用情况下计算平均分的程序,具体要求如下:
(1) 编写一个ComputerAverage接口,接口有一个求平均分的抽象方法average,方法的参数为double类型的数组。
(2)定义Gymnastics类和School类,它们都是ComputerAverage的实现类,Gymnastics类中的平均分方法为计算体育比赛中选手的平均成绩,具体算法是去掉一个最低分,去掉一个最高分,然后对剩余的数求平均分。
(3)School类中的平均分为计算学生考试成绩的平均分,具体算法是分数的和除以总的科目数
(4)要求:在主类中声明ComputerAverage的对象,并使用为上转型对象,实现ComputerAverage的对象调用average方法, 实现多态,同样的两条语句实现两种不同计算平均分的方法。输入的成绩为一组数,数据的个数和具体的数据从键盘输入。
package test;
import java.util.*;
import java.math.*;
interface ComputerAverage{
double average(double[] grade);
}
class Gymnastics implements ComputerAverage{
private double i;
public double average(double[] grade){
double sum=0;
double max,min;
max=min=grade[0];
for(int i=0;i<grade.length;i++){
if(max<grade[i])
max=grade[i];
if(min>grade[i])
min=grade[i];
sum+=grade[i];
}
sum=sum-max-min;
return sum/(grade.length-2);
}
}
class School implements ComputerAverage{
public double average(double[] grade){
double sum=0;
for(int i=0;i<grade.length;i++){
sum+=grade[i];
}
return sum/grade.length;
}
}
public class test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
double[] grade=new double[n];
for(int i=0;i<n;i++){
grade[i]=scanner.nextDouble();
}
ComputerAverage a;
Gymnastics g = new Gymnastics();
a=g;
System.out.print("Gymnastics average is:");
System.out.printf("%.2f\n",a.average(grade));
School s=new School();
a=s;
System.out.print("School average is:");
System.out.printf("%.2f",g.average(grade));
}
}