0%

Java类和对象-练习

实验内容

1.设计一个Dog类,有名字、颜色和年龄属性,定义构造方法初始化这些属性,定义输出方法show()显示其属性。编写应用程序使用Dog类。

2.(1)编写一个学校类,其中包含成员变量scoreLine(录取分数线)和对该变量值进行设置和获取的方法。

(2)编写一个学生类,它的成员变量有考生的name(姓名)、id(考号)、total(综合成绩)、sports(体育成绩)。它还有获取学生的综合成绩和体育成绩的方法。

(3)编写一个录取类,它的一个方法用于判断学生是否符合录取条件。其中录取条件为:综合成绩在录取分数线之上,或者体育成绩在96以上并且综合成绩大于300。在该类的main()方法中,建立若干个学生对象,对符合录取条件的学生,输出其信息及“被录取”。

第一题

首先我们构建Dog类

1
2
3
4
5
public Dog(String name, String color, int age) {
this.name = name;
this.color = color;
this.age = age;
}

我们定义了姓名属性、颜色属性、年龄属性

定义输出方法

我们使用System.out.println将信息输出到控制台

1
2
3
4
5
public void show() {
System.out.println("名字: " + name);
System.out.println("颜色: " + color);
System.out.println("年龄: " + age);
}

编辑main函数

其中Dog(名字:"布的", 颜色:"棕色", 年龄:3);在类中输出的时候会调用Dog类的格式进行

1
2
3
4
5
6
7
public static void main(String[] args) {
Dog dog1 = new Dog("布的", "棕色", 3);
dog1.show();

Dog dog2 = new Dog("极大", "黑色", 5);
dog2.show();
}

输出

在类中输出的时候会调用Dog类的格式进行image-20230626003732624

第二题

1.设置学校类

设置学校类,并且先在类中定义分数线private int scoreLine;

设置分数线方法

在类中创建方法,用this.scoreLine指定类中定义的分数线private int scoreLine;等于形式参数的scoreLine

1
2
3
public void setScoreLine(int scoreLine) {
this.scoreLine = scoreLine;
}

获取分数线方法

获取分数线方法,用return返回this.scoreLine;

1
2
3
public static double gitScoreLine() {
return School.scoreLine;
}

2.设置学生类

如上,在类中定义

1
2
3
4
private String name;
private long id;
private double total;
private double sports;

再创建以类名作为名字的方法:

1
2
3
4
5
6
public Student(String name,long id,double total,double sports){
this.name=name;
this.id=id;
this.total=total;
this.sports=sports;
}

创建获取方法

1.获取综合分数
1
2
3
public double getTotal() {
return total;
}
2.获取体育分数
1
2
3
public double getSports() {
return sports;
}

3.设置录取类

首先编写一个方法对是否录取做出判断:

1
2
3
4
5
public boolean determine(double scoreLine,double total,double sports){
if(total>scoreLine) return true;
else if (sports>96&&total>300) return true;
else return false;
}

其次编写main方法建立若干个学生对象,对符合录取条件的学生,输出其信息及“被录取”

1.设置分数线School.setScoreLine(400);

2.创建学生类Student weijie=new Student("魏杰",2115134,500,80);

3.if判断if(Admission.determine(School.gitScoreLine(), weijie.getTotal(), weijie.getSports()))

结果

结果

完整代码

School类

1
2
3
4
5
6
7
8
9
10
11
public class School {
private static double scoreLine;

public static void setScoreLine(double scoreLine) {
School.scoreLine = scoreLine;
}
public static double gitScoreLine() {
return School.scoreLine;
}

}

Student类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Student {
String name;
private long id;
private double total;
private double sports;
public Student(String name,long id,double total,double sports){
this.name=name;
this.id=id;
this.total=total;
this.sports=sports;
}

public double getTotal() {
return total;
}

public double getSports() {
return sports;
}
}

Admission录取类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Admission {
public static boolean determine(double scoreLine,double total,double sports){
if(total>scoreLine) return true;
else if (sports>96&&total>300) return true;
else return false;
}

public static void main(String[] args) {
School.setScoreLine(400);
Student weijie=new Student("魏杰",2115134,500,80);
if(Admission.determine(School.gitScoreLine(), weijie.getTotal(), weijie.getSports())){
System.out.println(weijie.name+"被录取");
}else System.out.println(weijie.name+"未被录取");
Student a=new Student("小a",2115135,300,60);
if(Admission.determine(School.gitScoreLine(), a.getTotal(), a.getSports())){
System.out.println(a.name+"被录取");
}else System.out.println(a.name+"未被录取");
}
}
技术分享,如果您觉得内容不错的话不妨进行打赏,您的支持将鼓励我继续创作!


如果你对本文有看法或更好的见解可以点击下方按钮进行评论
*注:本站为非交互网站
此按钮将会转跳至评论站点 i-nmb.chat 进行评论

欢迎关注我的其它发布渠道