题目一
1(题目编号7173)、现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,具体要求如下:
(1)在基类中记录狗的品种breed,体重weight以及颜色color等属性,定义一个方法show()显示Dog信息;
(2)在UnspottedDog类中,调用Dog类的构造方法,重写show()方法,只显示狗的品种;
(3)在SpottedDog类中,新增表示斑点颜色的spotColor属性,定义包含四个属性的构造方法,重写show()方法;
(4)定义测试类,构造斑点狗对象,分别显示斑点狗的品种、体重、颜色和品种、颜色、斑点颜色;构造非斑点狗对象,显示狗的品种、体重、颜色信息。
(说明:构造斑点狗对象和非斑点狗对象时要分别输入,各属性值之间用空格分割,输入完后按回车键确认,输入内容参照测试数据。)
实验代码:
import java.util.*;
class Dog
{
String breed,color;
double weight;
void show(){
}
}
class SpottedDog extends Dog{
String spotColor;
SpottedDog(String breed,String color,double weight,String spotColor){
this.spotColor=spotColor;
this.breed=breed;
this.color=color;
this.weight=weight;
}
void show(){
System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color);
System.out.println("这是一只"+breed+",颜色为"+color+",斑点颜色为"+spotColor);
}
}
class UnspottedDog extends Dog{
UnspottedDog(String breed,String color,double weight){
this.breed=breed;
this.breed=breed;
this.color=color;
}
void show(){
System.out.println("这是一只"+breed+"犬");
}
}
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double weight;
String breed,color,spotColor;
breed=in.next();
weight=in.nextDouble();
color=in.next();
spotColor=in.next();
SpottedDog demo1=new SpottedDog(breed,color,weight,spotColor);
demo1.show();
breed=in.next();
weight=in.nextDouble();
color=in.next();
UnspottedDog demo2=new UnspottedDog(breed,color,weight);
demo2.show();
}
}
实验结果:

题目二
2(题目编号7174)、编写一个制造各种车辆的程序。包含三个类,具体要求如下:
(1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法;
(2)小轿车类Car,增加载客数属性,重写构造方法和显示车辆信息的成员方法;
(3)卡车类Truck,增加载客数和载货量属性,重写构造方法和显示车辆信息的成员方法;
(4)主程序类,要求输入各种车辆的信息,并在控制台输出各种车辆信息。
实验代码:
import java.util.*;
class Vehicle
{
int lunzi;
double weight;
Vehicle(int lunzi,double weight){
this.lunzi=lunzi;
this.weight=weight;
}
Vehicle(){
}
void show(){
System.out.println("汽车:");
System.out.println("轮子数:"+lunzi+"个");
System.out.println("自身重量:"+weight+"吨");
}
}
class Car extends Vehicle{
int zaike;
Car(int lunzi,double weight,int zaike){
this.lunzi=lunzi;
this.weight=weight;
this.zaike=zaike;
}
void show(){
System.out.println("小轿车:");
System.out.println("轮子数:"+lunzi+"个");
System.out.println("自身重量:"+weight+"吨");
System.out.println("额定乘客数:"+zaike+"人");
}
Car(){
}
}
class Truck extends Car{
double zaizhong;
Truck(int lunzi,double weight,int zaike,double zaizhong){
this.lunzi=lunzi;
this.weight=weight;
this.zaike=zaike;
this.zaizhong=zaizhong;
}
void show(){
System.out.println("卡车:");
System.out.println("轮子数:"+lunzi+"个");
System.out.println("自身重量:"+weight+"吨");
System.out.println("额定乘客数"+zaike+"人");
System.out.println("载重量"+zaizhong+"吨");
}
}
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int lunzi,zaike;
double weight,zaizhong;
lunzi=in.nextInt();
weight=in.nextDouble();
Vehicle demo1=new Vehicle(lunzi,weight);
demo1.show();
lunzi=in.nextInt();
weight=in.nextDouble();
zaike=in.nextInt();
Car demo2=new Car(lunzi,weight,zaike);
demo2.show();
lunzi=in.nextInt();
weight=in.nextDouble();
zaike=in.nextInt();
zaizhong=in.nextDouble();
Truck demo3=new Truck(lunzi,weight,zaike,zaizhong);
demo3.show();
}
}
实验结果:

题目三
3(题目编号7175)、使用接口或者抽象类编写程序实现显示员工基本信息。具体要求如下:
(1)使用接口或者抽象类实现基类Employer(体会接口和抽象类的不同),包含姓名、部门和工资三个属性,显示工资的方法showSalary()和显示奖金的抽象方法showBonus();提示:因每位职工奖金不同,showBonus()方法定义为抽象方法,只抽象定义,不具体实现;
(2)定义BasicEmployee和GoodEmployee类,重写Employer类中的方法,不同员工有不同的工资和奖金;
(3)定义主类进行测试,要求输入两个不同的员工信息,并输出其个人信息。
实验代码:
import java.util.*;
abstract class Employer{
String name,department;
double salary,Bonus;
void showSalary(){
System.out.println("我叫"+name+",在"+department+"部门,我的工资是"+salary);
}
abstract void showBonus();
}
class BasicEmployee extends Employer{
BasicEmployee(String name,String department,double salary){
this.name=name;
this.salary=salary;
this.department=department;
}
void showBonus(){
System.out.println("我是普通员工,没有奖金,加油升级!");
}
}
class GoodEmployee extends Employer{
GoodEmployee(String name,String department,double salary,double Bonus){
this.name=name;
this.salary=salary;
this.department=department;
this.Bonus=Bonus;
}
void showBonus(){
System.out.println("我是优秀员工,我的奖金是"+Bonus);
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
String name,department;
double salary,bonus;
name=in.next();
department=in.next();
salary=in.nextDouble();
BasicEmployee demo1=new BasicEmployee(name,department,salary);
demo1.showSalary();
demo1.showBonus();
name=in.next();
department=in.next();
salary=in.nextDouble();
bonus=in.nextDouble();
GoodEmployee demo2=new GoodEmployee(name,department,salary,bonus);
demo2.showSalary();
demo2.showBonus();
}
}
实验结果:

题目四
4(题目编号7176)、编写一个教师讲课的程序。所有老师都具有共同的讲课方法,但是不同科目的教师讲课内容不同,主程序中编写一个讲课的方法TeachingRace(Teacher t),显示不同的老师t讲授不同的课程内容。提示:
(1)所有老师具有共同的讲课方法,可在接口中定义一个讲课方法;
(2)不同科目的老师实现接口中的讲课方法;
(3)在主程序中定义一个讲课的方法TeachingRace(Teacher t),构造不同的教师,显示讲课内容【主要考察接口回调】。
实验代码:
import java.util.*;
interface Teacher{
void fangFa();
}
class English implements Teacher {
String say;
English(String say){
this.say=say;
}
public void fangFa(){
System.out.println("我是英语老师,I say "+say);
}
}
class Math implements Teacher {
String say;
Math(String say){
this.say=say;
}
public void fangFa(){
System.out.println("我是数学老师,I say "+say);
}
}
class Text{
void TeachingRace(Teacher t){
t.fangFa();
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Text demo1=new Text();
String say;
say=in.next();
demo1.TeachingRace(new English(say));
say=in.next();
demo1.TeachingRace(new Math(say));
}
}
实验结果:

题目五
5(题目编号7177)、创建Animal(动物)类:Mouse,dog等的一个继承分级结构.在父类中提供适用于所有Animal的方法,并在子类中覆盖他们,从而根据不同类型的Animal采取不同的行动Anima类有如下方法:public void speak()。
实验代码:
import java.util.jar.Attributes.Name;
import java.util.*;
class Animal{
public void speak(){
}
}
class Cat extends Animal{
private String name;
public Cat(String name) {
super();
this.name = name;
}
public void speak(){
System.out.println(name+"的叫声为喵喵");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Mouse extends Animal{
private String name;
public Mouse(String name) {
super();
this.name = name;
}
public void speak(){
System.out.println(name+"的叫声为吱吱");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String catString=in.next();
String mouseString=in.next();
Animal Tom=new Cat(catString);
Animal Jerry=new Mouse(mouseString);
Tom.speak();
Jerry.speak();
}
}
实验结果:

题目六
6(题目编号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)编写测试类,要求建立计算机对象,建立鼠标、键盘、麦克风对象,并添加到计算机中,启动计算机,关闭计算机*/。
实验代码:
import java.util.*;
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 [] sheBei= new USB[100];
void add(USB t){
if(num<=100){
sheBei[num]=t;
num++;
}
}
void powerOn(){
for(int i=0;i<num;i++){
sheBei[i].turnOn();
}
System.out.println("计算机开机成功");
}
void powerOff(){
for(int i=0;i<num;i++){
sheBei[i].turnOff();
}
System.out.println("计算机关机成功");
}
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Computer demo1=new Computer();
demo1.add(new Mouse());
demo1.add(new KeyBoard());
demo1.add(new Mic());
demo1.powerOn();
demo1.powerOff();
}
}
实验结果:

题目七
7(题目编号5950)编写一个通过接口实现不同应用情况下计算平均分的程序,具体要求如下:
- 1、 编写一个ComputerAverage接口,接口有一个求平均分的抽象方法average,方法的参数为double类型的数组。
- 2、定义Gymnastics类和School类,它们都是ComputerAverage的实现类,Gymnastics类中的平均分方法为计算体育比赛中选手的平均成绩,具体算法是去掉一个最低分,去掉一个最高分,然后对剩余的数求平均分。
- 3、School类中的平均分为计算学生考试成绩的平均分,具体算法是分数的和除以总的科目数
- 4、要求:在主类中声明ComputerAverage的对象,并使用为上转型对象,实现ComputerAverage的对象调用average方法, 实现多态,同样的两条语句实现两种不同计算平均分的方法。输入的成绩为一组数,数据的个数和具体的数据从键盘输入。
实验代码:
import java.util.Scanner;
interface ComputerAverage {
double average(double data[]);
}
class Gymnastics implements ComputerAverage {
public double average(double []data) {
double sum=0;
double temp;
for(int i=0;i<data.length-1;i++){
for(int j=0;j< data.length-1-i;j++){
if(data[j]>data[j+1]){
temp=data[j];
data[j]=data[j+1];
data[j+1]=temp;
}
}
}
for(int i=1;i< data.length-1;i++){
sum=sum+data[i];
}
return sum/( data.length-2);
}
}
class School implements ComputerAverage{
public double average(double []data) {
double sum=0;
for(int i=0;i< data.length;i++){
sum=sum+data[i];
}
return sum/ data.length;
}
}
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
ComputerAverage g;
int n=scan.nextInt();
double []data=new double[n];
for(int i=0;i<n;i++){
data[i]=scan.nextDouble();
}
g=new Gymnastics();
System.out.print("Gymnastics average is:");
System.out.printf("%.2f\n",g.average(data));
g=new School();
System.out.print("School average is:");
System.out.printf("%.2f",g.average(data));
}
}
实验结果:

这篇博客介绍了多个Java编程实验,涵盖类的继承和多态概念。实验包括:创建Dog类及其派生类,实现不同车辆的类,员工信息管理,教师讲课接口,Animal类继承结构,USB设备接口及其实现,以及计算平均分的接口应用。每个实验都详细说明了需求、代码实现和实验结果。
:类的继承和多态实验&spm=1001.2101.3001.5002&articleId=119730717&d=1&t=3&u=21c5f1a160004e1aabad3fecbeb9c7e6)
1868

被折叠的 条评论
为什么被折叠?



