北邮JAVA高级语言程序设计(选修课)第一次OJ作业

北邮JAVA高级语言程序设计(选修课)第一次OJ作业

题目描述:定义一个学生类(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.util.*;
import java.text.DecimalFormat;

public class StudentTest {
	public static void main(String argv[]) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext())//循环读入
		{
			String line = in.nextLine();//用line将输入保存为一个字符串
			String list[] = line.split(" ");//用空格分隔line中的字符串并依次赋值给数组list
			Student Info = new Student();
			//将list数组内的字符串依次赋值给一下内容
			Info.studentNumber = list[0];
			Info.studentName = list[1];
			Info.markForMaths = Integer.parseInt(list[2]);
			Info.markForEnglish = Integer.parseInt(list[3]);
			Info.markForScience = Integer.parseInt(list[4]);
			Info.Student(list[0],list[1]);
			Info.calculateAverage();
			System.out.println(Info.toString());//打印学生信息
		}
	}
}


class Student
{
	String studentNumber;
	String studentName;
	int markForMaths;
	int markForEnglish;
	int markForScience;
	
	public void Student(String number, String name) {
		studentNumber = number;
		studentName = name;
	}
	public Student() {

	}
	public String getNumber() {
		return studentNumber;
	}
	public String getName() {
		return studentName;
	}
	void enterMarks(int markForMaths, int markForEnglish, int markForScience) {
		this.markForMaths = markForMaths;
		this.markForEnglish = markForEnglish;
		this.markForScience = markForScience;
	}
	public int getMathsMark() {
		return markForMaths;
	}
	public int getEnglishMark() {
		return markForEnglish;
	}
	public int getScienceMark() {
		return markForScience;
	}
	public double calculateAverage() {
		return (markForEnglish+markForMaths+markForScience)/3.00;
	}
	public String toString() {
		DecimalFormat df = new DecimalFormat("#.00");//保留两位小数
		return ("Student Info:"+"\n"+"Number:"+studentNumber+"\n"+"Name:"+studentName+"\n"+
	              "Math:"+markForMaths+"\n"+"English:"+markForEnglish+"\n"+"Science:"+markForScience+
	              "\n"+"Ave:"+df.format(this.calculateAverage()));
	}
}

你可能感兴趣的:(java,经验分享)