题目描述
定义一个学生类(Student),
属性有
非静态属性String studentNumber
非静态属性String studentName
非静态属性int markForMaths
非静态属性int markForEnglish
非静态属性int markForScience
方法有:
构造方法Student(String number, String name)
构造方法Student()
String getNumber()
String getName()
void enterMarks(int markForMaths, int markForEnglish, int markForScience)
int getMathsMark()
int getEnglishMark()
int getScienceMark()
double calculateAverage()
String toString() 返回学生信息,包括学号、姓名、数学成绩、英语成绩、科学成绩、平均成绩。
注意:为了保证calculateAverage返回double类型,需要把三个分数的和除以3.0,而不是3.
另外,分数的初始值是什么?如果每个分数初始值为0,会造成混淆,分数为0表示还没有输入分数,还是分数确实为0?有更好的初始值吗?
编写Student类,并且编写一个StudentTest类,对Student类进行测试。每一个测试用例包含 number, name, maths, english, science 5种数据,用StudentTest类读取数据,并用数据构造Student类,最终打印学生信息。
StudentTest类运行效果如下:
Student Info:
Number:2011211301
Name:WangXiao
Math:88
English:79
Science:90
Ave:85.67
输入样例
2011211301 WangXiao 88 79 90
输出样例
Student Info:
Number:2011211301
Name:WangXiao
Math:88
English:79
Science:90
Ave:85.67
import java.text.DecimalFormat;
import java.util.Scanner;
public class StudentTest {
public static void main(String argv[]) {
Scanner Input = new Scanner(System.in);
String number = Input.next();
String name = Input.next();
int Math = Input.nextInt();
int English = Input.nextInt();
int Scinence = Input.nextInt();
Student A = new Student();
A.markForEnglish = English;
A.markForMaths = Math;
A.markForScience = Scinence;
A.Student(number, name);
A.calculateAverage();
System.out.println(A.toString());
}
}
class Student
{
String studentNumber;
String studentName;
int markForMaths;
int markForEnglish;
int markForScience;
public void Student(String studentNumber,String studentName)
{
this.studentName=studentName;//输入的string类型转换为int类型
this.studentNumber=studentNumber;
}
public String getNumber()
{
return studentNumber;
}
public String getName()
{
return studentName;
}
public void enterMarks(int markForMaths,int markForEnglish,int markForScience)
{
this.markForMaths=markForMaths;
this.markForEnglish= markForEnglish;
this.markForScience=markForScience;
}
public int getMathsMark()
{
return markForMaths;//school of electronic engineering of bupt
}
public int getEnglishMark()
{
return markForEnglish;
}
public int getScienceMark()
{
return markForScience;
}
public String calculateAverage()
{
double average_num=(markForEnglish+markForScience+markForMaths)/3.0;
DecimalFormat df = new DecimalFormat("#.00");
return df.format(average_num);
}
public String toString()
{
return("Student Info:"+"\n"+"Number:"+studentNumber+"\n"+"Name:"+studentName+"\n"+"Math:"+markForMaths+"\n"+"English:"+markForEnglish+"\n"+"Science:"+markForScience+"\n"+"Ave:"+this.calculateAverage());
}
}
好坑,不要把文件放在自己建的包里,直接放在intellij idea 的src文件夹下,直接运行就ok