Java程序设计实验-实验四

发布于 2024-03-21  539 次阅读


实验四 类的重用

1 - [OJ2030]类的重用1

请参照如下程序框架,按注释要求完成程序设计。

import java.util.Scanner;
class Animal{
//1.定义String属性name, int 属性leg;




//2.定义带两个参数的构造方法


//3.定义void类型的sound()方法,方法体为空


}


//Dog类继承Animal
class Dog extends Animal{
//1.定义int属性 speed, String 属性color;


//2.定义无参的构造方法,调用带参数的构造方法


//3. 定义包含name,leg,speed,color参数的构造方法


//4. 定义getSpeed()返回speed值;


//5. 重写sound()方法,打印输出“汪汪......”


//6.重写toString()方法,返回name、leg、color、speed值,字符串格式如:dog2 4 black 38


}


public class Main {
public static void main(String[] args)
{
//1.输入3组数据,每组包含name,leg,color,speed,共输入12行


//2.在构造方法中使用输入的数据创建3个Dog对象


//3.从上述3个对象中找出speed值最大的对象,打印输出该对象,并调用其sound()方法


}


}

输入3组数据,每组包含name,leg,color,speed,共12行。

按注释要求打印输出speed值最大的Dog对象,并调用其sound()方法。

import java.util.Scanner;
class Animal{

//1.定义String属性name, int 属性leg;
    String name;
    int leg;

//2.定义带两个参数的构造方法
    public Animal(String name, int leg){
        this.name = name;
        this.leg = leg;
    }

//3.定义void类型的sound()方法,方法体为空
    public void sound(){

    }

}

//Dog类继承Animal
class Dog extends Animal{

//1.定义int属性 speed, String 属性color;
    int speed;
    String color;

//2.定义无参的构造方法,调用带参数的构造方法
    public Dog(){
        super("", 0);
    }

//3. 定义包含name,leg,speed,color参数的构造方法
    public Dog(String name, int leg, String color, int speed){
        super(name, leg);
        this.color = color;
        this.speed = speed;
    }

//4. 定义getSpeed()返回speed值;
    public int getSpeed(){
        return speed;
    }

//5. 重写sound()方法,打印输出“汪汪......”
    @Override
    public void sound(){
        System.out.println("汪汪......");
    }

//6.重写toString()方法,返回name、leg、color、speed值,字符串格式如:dog2 4 black 38
    @Override
    public String toString(){
        return name + " " + leg + " " + color + " " + speed;
    }

}

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Dog maxSpeedDog = new Dog();
        int maxSpeed;
        String[] name = new String[5];
        int[] leg = new int[5];
        String[] color = new String[5];
        int[] speed = new int[5];

//1.输入3组数据,每组包含name,leg,color,speed,共输入12行
        for(int i = 0; i < 3; i ++){
            name[i] = input.nextLine();
            leg[i] = Integer.parseInt(input.nextLine());
//            leg[i] = input.nextInt();
//            input.nextLine();
            color[i] = input.nextLine();
            speed[i] = Integer.parseInt(input.nextLine());
//            speed[i] = input.nextInt();
//            input.nextLine();
        }

//2.在构造方法中使用输入的数据创建3个Dog对象
        Dog[] dog = new Dog[5];
        for(int i = 0; i < 3; i ++){
            dog[i] = new Dog(name[i], leg[i], color[i], speed[i]);
//            System.out.println(dog[i]);
        }

//3.从上述3个对象中找出speed值最大的对象,打印输出该对象,并调用其sound()方法

        maxSpeed = dog[0].getSpeed();
        maxSpeedDog = dog[0];
        for(int i = 1; i < 3; i ++){
            if (dog[i].getSpeed() > maxSpeed){
                maxSpeed = dog[i].getSpeed();
                maxSpeedDog = dog[i];
            }
        }

        System.out.println(maxSpeedDog);
        maxSpeedDog.sound();

    }

}

2 - [OJ2031]类的重用2

参照以下程序框架,按注释提示完成程序设计:

import java.util.Scanner;
class Student{
//1. 属性包括学号,姓名,英语成绩,数学成绩,计算机成绩,总成绩。
//2.无参构造方法
//3.有参构造方法
//4. getter ,setter方法
//5.sum()方法返回总分


//6. testScore()方法,返回平均成绩


//7.重写toString方法,返回字符串参考格式如下:
// Student [xh=20231, xm=xm1, yy=99.0, sx=88.0, jsj=77.0, zf=264.0]


//8. 重写equals方法
@Override
public boolean equals(object x) {
     // ......
}
//9. int compare(Student s) 比较方法,总分相等返回0,当前对象总分小于s对象总分返回-1,否则返回1
int compare(Student s) {
   //......
}


}


// 学委类: StudentXW继承Student
class StudentXW extends Student{
//1.增加职责属性


//2.有参数的构造方法,要求调用父类构造方法


//3. 重写testScore方法,返回值为平均分+3




//4. 重写toString方法,返回字符串参考格式如下:
// StudentXW [zz=学习委员, xh=20232, xm=xm2, yy=89.0, sx=98.0, jsj=77.0, zf=264.0]


}


//班长类: StudentBZXW继承Student
class StudentBZ extends Student{
//1.增加职责属性




//2.有参数的构造方法,要求调用父类构造方法


//3. 重写testScore方法,返回值为平均分+5


//4. 重写toString方法,返回字符串参考格式如下:
// StudentBZ [zz=班长, xh=20233, xm=xm3, yy=100.0, sx=95.0, jsj=75.0, zf=270.0]


}


public class Main {
public static void main(String[] args)
{
//1.输入3组数据,输入格式参照样例格式,各数据项分别对应3个对象:s1,s2,s3




//2. 根据输入数据创建3个对象,分别表示普通学生s1、学习委员s2,班长s3




//3. 分3行打印输出3个对象




//4.分3行打印输出3个对象的测评成绩




//5.调用equals方法,打印输出对象s1和s2是否相等,输出结果为false 或 ture


//6.调用compare方法,比较s2对象和s3对象,输出结果为0,-1或1


}


}

输入3组数据,输入格式参照样例格式,各数据项分别对应3个对象:s1,s2,s3
s2代表学习委员对象,s3代表班长对象。

输出格式参数程序注释,按注释要求输出。

import java.util.Scanner;
class Student{
//1. 属性包括学号,姓名,英语成绩,数学成绩,计算机成绩,总成绩。
    private int xh;
    private String xm;
    private double yy;
    private double sx;
    private double jsj;
    private double zf;
//2.无参构造方法
    public Student(){
        this.xh = 0;
        this.xm = "";
        this.yy = 0;
        this.sx = 0;
        this.jsj = 0;
        this.zf = 0;
    }
//3.有参构造方法
    public Student(int xh, String xm, double yy, double sx, double jsj){
        this.xh = xh;
        this.xm = xm;
        this.yy = yy;
        this.sx = sx;
        this.jsj = jsj;
        this.zf = sum();
    }
//4. getter ,setter方法
    public int getxh(){
        return xh;
    }
    public void setxh(int xh){
        this.xh = xh;
    }
    public String getxm(){
        return xm;
    }
    public void setxm(String xm){
        this.xm = xm;
    }
    public double getyy(){
        return yy;
    }
    public void setyy(double yy){
        this.yy = yy;
    }
    public double getsx(){
        return sx;
    }
    public void setsx(double sx){
        this.sx = sx;
    }
    public double getjsj(){
        return jsj;
    }
    public void setjsj(double jsj){
        this.jsj = jsj;
    }
//5.sum()方法返回总分
    public double sum(){
        return yy + sx + jsj;
    }
//6. testScore()方法,返回平均成绩
    public double testScore(){
        return sum() / 3.0;
    }
//7.重写toString方法,返回字符串参考格式如下:
// Student [xh=20231, xm=xm1, yy=99.0, sx=88.0, jsj=77.0, zf=264.0]
    @Override
    public String toString(){
        return "Student [xh=" + getxh() + ", xm=" + getxm() + ", yy=" + getyy() + ", sx=" + getsx() + ", jsj=" + getjsj() + ", zf=" + sum() + "]";
    }
//8. 重写equals方法
    @Override
    public boolean equals(Object x) {
        if (this == x) return true;
        if (x == null || this.getClass() != x.getClass()) return false;
        Student y = (Student) x;
        return Double.compare(this.zf, y.zf) == 0;
    }
//9. int compare(Student s) 比较方法,总分相等返回0,当前对象总分小于s对象总分返回-1,否则返回1
    public int compare(Student s) {
        if (this.zf == s.zf) return 0;
        else if(this.zf < s.zf) return -1;
        else return 1;
    }

}


// 学委类: StudentXW继承Student
class StudentXW extends Student{
//1.增加职责属性
    private String zz;
//2.有参数的构造方法,要求调用父类构造方法
    public StudentXW(String zz, int xh, String xm, double yy, double sx, double jsj){
        super(xh, xm, yy, sx, jsj);
        this.zz = zz;
    }
//3. 重写testScore方法,返回值为平均分+3
    @Override
    public double testScore(){
        return super.testScore() + 3;
    }
//4. 重写toString方法,返回字符串参考格式如下:
// StudentXW [zz=学习委员, xh=20232, xm=xm2, yy=89.0, sx=98.0, jsj=77.0, zf=264.0]
    @Override
    public String toString(){
        return "StudentXW [zz=" + zz + ", xh=" + getxh() + ", xm=" + getxm() + ", yy=" + getyy() + ", sx=" + getsx() + ", jsj=" + getjsj() + ", zf=" + sum() + "]";
    }

}


//班长类: StudentBZXW继承Student
class StudentBZ extends Student{
//1.增加职责属性
    private String zz;

//2.有参数的构造方法,要求调用父类构造方法
    public StudentBZ(String zz, int xh, String xm, double yy, double sx, double jsj){
        super(xh, xm, yy, sx, jsj);
        this.zz = zz;
    }

//3. 重写testScore方法,返回值为平均分+5
    @Override
    public double testScore(){
        return super.testScore() + 5;
    }

//4. 重写toString方法,返回字符串参考格式如下:
// StudentBZ [zz=班长, xh=20233, xm=xm3, yy=100.0, sx=95.0, jsj=75.0, zf=270.0]
    @Override
    public String toString(){
        return "StudentBZ [zz=" + zz + ", xh=" + getxh() + ", xm=" + getxm() + ", yy=" + getyy() + ", sx=" + getsx() + ", jsj=" + getjsj() + ", zf=" + sum() + "]";
    }

}


public class Main {
    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

//1.输入3组数据,输入格式参照样例格式,各数据项分别对应3个对象:s1,s2,s3
        int xh1 = input.nextInt();
        input.nextLine();
        String xm1 = input.nextLine();
        double yy1 = input.nextDouble();
        double sx1 = input.nextDouble();
        double jsj1 = input.nextDouble();

        int xh2 = input.nextInt();
        input.nextLine();
        String xm2 = input.nextLine();
        double yy2 = input.nextDouble();
        double sx2 = input.nextDouble();
        double jsj2 = input.nextDouble();
        input.nextLine();
        String zz2 = input.nextLine();

        int xh3 = input.nextInt();
        input.nextLine();
        String xm3 = input.nextLine();
        double yy3 = input.nextDouble();
        double sx3 = input.nextDouble();
        double jsj3 = input.nextDouble();
        input.nextLine();
        String zz3 = input.nextLine();
//2. 根据输入数据创建3个对象,分别表示普通学生s1、学习委员s2,班长s3
        Student s1 = new Student(xh1, xm1, yy1, sx1, jsj1);
        StudentXW s2 = new StudentXW(zz2, xh2, xm2, yy2, sx2, jsj2);
        StudentBZ s3 = new StudentBZ(zz3, xh3, xm3, yy3, sx3, jsj3);
//3. 分3行打印输出3个对象
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
//4.分3行打印输出3个对象的测评成绩
        System.out.println(s1.testScore());
        System.out.println(s2.testScore());
        System.out.println(s3.testScore());
//5.调用equals方法,打印输出对象s1和s2是否相等,输出结果为false 或 ture
        System.out.println(s1.equals(s2));
//6.调用compare方法,比较s2对象和s3对象,输出结果为0,-1或1
        System.out.println(s2.compare(s3));

    }

}