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

发布网友 发布时间:2022-04-22 09:25

我来回答

1个回答

热心网友 时间:2023-10-27 03:37

要写一个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));
}
}追问我的求求平均数前面跟你不一样这样可以吗
public double average(double chinese,double math,double english) {
double n=(chinese+math+english)/3;
return n;
}

追答可以的,你求的是带小数的,我给你求的是整数,只是给你做个示例,double更符合实际情况。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com