java测试这个程序怎么测试,程序写出来了图片上面要另一个包测试这个程序求解

2024-11-20 07:12:45
推荐回答(1个)
回答1:

要写一个Student类和一个TestStudent类,具体如下:
student类:
// 1、创建一个描写学生的类
// 2、包:cn.whvsce.
package cn.whvsce;

public class Student {
// 3、学号、姓名、年龄 (private) 语数外三门成绩(public)
private int id;
private String name;
private int age;
public int chinese;
public int mathematics;
public int foreignLanguages;

// 4、包含赋初值的构造方法
public Student(){
this.id = 1;
this.name = "zhang san";
this.age = 16;
this.chinese = 85;
this.mathematics = 79;
this.foreignLanguages = 81;
}

// 5、每个属性有 get、set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMathematics() {
return mathematics;
}
public void setMathematics(int mathematics) {
this.mathematics = mathematics;
}
public int getForeignLanguages() {
return foreignLanguages;
}
public void setForeignLanguages(int foreignLanguages) {
this.foreignLanguages = foreignLanguages;
}

// 6、求平均成绩的静态方法average()
public static int average(Student student){
return (student.getChinese()+student.getMathematics()+student.getForeignLanguages())/3;
}

// 7、定义一个显示学生所有属性的静态方法show()
public void show(){
System.out.println("学号:"+this.getId()+" 姓名:"+this.getName()+" 年龄:"+this.getAge()+" 语文成绩:"+this.getChinese()+" 数学成绩:"+this.getMathematics()+" 外语成绩:"+this.getForeignLanguages());
}
}

TestStudent类

//8、测试类包 cn.xyy
package cn.xyy;

import cn.whvsce.Student;

public class TestStudent {
// 9、测试用的main()方法
public static void main(String[] args) {
// 10、显示学生基本信息和平均成绩
Student s = new Student();
s.show();
System.out.println("该学生的平均成绩为:"+s.average(s));

// 11、利用set()修改学生成绩,再输出新的平均成绩
s.setChinese(60);
s.setMathematics(70);
s.setForeignLanguages(80);
System.out.println("该生修改后的平均成绩为:"+s.average(s));
}
}