北邮JAVA高级语言程序设计(选修课)第二次OJ作业
题目描述:
定义一个StudentList类用来存储Student对象
属性有
1)Student[] list; //list存储学生对象
2)int total; //学生总人数
方法有:
1)StudentList(int length) //length是数组长度
2)boolean add(Student stu) //增加stu到数组中,成功,返回true,否则false
3)boolean remove(int no) //删除第no个数组元素,删除成功,返回true,否则false
4)boolean remove(String number) //删除学号为number的学生,删除成功,返回true,否则false
5)boolean isEmpty() //判断数组是否为空,若是,返回true,否则false
6)Student getItem(int no) //返回第no个学生
7)Student getItem(String number) //返回学号为number的学生,若该生不存在,返回null。
8) int getTotal() 返回学生总人数
编写StudentList类,并且编写一个StudentListTest类,对StudentList类进行测试。
StudentListTest类运行效果:
菜单如下,请输入 1~8代表您要执行的操作:
1.增加1个学生 2.根据学号删除学生 3.根据位置删除学生 4.判断是否为空 5.根据位置返回学生 6.根据学号返回学生 7.输出全部学生信息 8.退出程序
输入样例,每行表示一个操作,第一个元素为操作代码,之后的元素为操作需要用到的数据。具体参看样例。
添加操作,你需要输出当前所有学生信息,格式如下:
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
查询操作,若有该学生,则输出其信息:
Student Info:
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
否则,输出:
No such student
删除操作,若有学生,输出:
Remove successfully
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
若不存在该学生,输出:
No such student
输入样例:
1 2011211301 LiHang 88 79 90
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
#(input)#1 2011211311 WangZhe 80 79 93
2 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
#(input)#5 10
No such student
#(input)#5 2
Student Info:
Number:2011211311
Name:WangZhe
Math:80
English:79
Science:93
Ave:84.00
#(input)#3 2
Remove successfully
1 Students at the moment as described below:
Number:2011211301
Name:LiHang
Math:88
English:79
Science:90
Ave:85.67
源代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class StudentListTest {
public static void main(String args[])
{
StudentList list =new StudentList(10);
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
String line = in.nextLine();
String str[] = line.split(" ");
String s1 = str[0];
int number = Integer.parseInt(s1);
if(number == 1)
{
String StudentNumber = str[1];
String StudentName = str[2];
int markForMath = Integer.parseInt(str[3]);
int markForEnglish = Integer.parseInt(str[4]);
int markForScience = Integer.parseInt(str[5]);
Student stu = new Student(StudentNumber,StudentName,markForMath,markForEnglish,markForScience);
if(list.add(stu))
{
System.out.println(list.total+" Students at the moment as described below:");
list.print();
}
}
if(number == 2)
{
if(list.remove(str[1]))
{
System.out.println("Remove successfully");
System.out.println(list.total+" Students at the moment as described below:");
list.print();
}
else
{
System.out.println("No such student");
}
}
if(number == 3)
{
int location = Integer.parseInt(str[1]);
if(list.remove(location))
{
System.out.println("Remove successfully");
System.out.println(list.total+" Students at the moment as described below:");
list.print();
}
else
{
System.out.println("No such student");
}
}
if(number == 4)
{
if(list.isEmpty())
{
System.out.println("空");
}
else
{
System.out.println("不空");
}
}
if(number == 5)
{
int A = Integer.parseInt(str[1]);
if(list.getItem(A - 1) != null)
{
System.out.println("Student Info:\n"+list.list[A-1].toString());
}
else
{
System.out.println("No such student");
}
}
if(number == 6)
{
if(list.getItem(str[1]) != null)
{
System.out.println("Student Info:\n"+list.getItem(str[1]).toString());
}
else
{
System.out.println("No such student");
}
}
if(number == 7)
{
list.print();
}
if(number == 8)
{
System.exit(0);
}
}
}
}
class Student
{
String StudentNumber;
String StudentName;
int markForMath;
int markForEnglish;
int markForScience;
double AVE;
DecimalFormat df=new DecimalFormat("#.00");
Student(String StudentNumber,String StudentName,int markForMath,int markForEnglish,int markForScience)
{
this.StudentNumber = StudentNumber ;
this.StudentName = StudentName;
this.markForMath = markForMath;
this.markForEnglish = markForEnglish;
this.markForScience = markForScience;
AVE = (markForMath + markForEnglish + markForScience )/3.00;
}
public String toString()
{
return ("Number:"+this.StudentNumber+"\n"+"Name:"+this.StudentName+"\n"+
"Math:"+this.markForMath+"\n"+"English:"+this.markForEnglish+
"\n"+"Science:"+this.markForScience+"\n"+"Ave:"+df.format(AVE));
}
}
class StudentList
{
Student[] list;
int total;
StudentList(int length)
{
list = new Student[length];
}
boolean add(Student stu)
{
if(total > list.length)
{
return false;
}
else
{
list[total] = new Student(stu.StudentNumber,stu.StudentName,stu.markForMath,stu.markForEnglish,stu.markForScience);
total++;
return true;
}
}
boolean remove(int no)
{
if(no > total)
{
return false;
}
else
{
for(int i = no - 1; i < total - 1; i++)
{
list[i] = list[i + 1];
}
total--;
return true;
}
}
boolean remove(String number)
{
if(!isEmpty())
{
for(int i = 0;i < total;i++)
{
if(list[i].StudentNumber == number)
{
for(int j = i;j < total - 1;j++)
{
list[j] = list[j + 1];
}
}
total--;
return true;
}
}
return false;
}
boolean isEmpty()
{
if(list.length != 0)
{
return false;
}
else
{
return true;
}
}
Student getItem(int no)
{
if(no >= total)
{
return null;
}
else
{
return list[no - 1];
}
}
Student getItem(String number)
{
if(!isEmpty())
{
for(int i = 0;i < total - 1;i++)
{
if(list[i].StudentNumber == number)
{
return list[i];
}
}
}
return null;
}
int getTotal()
{
return total;
}
public void print()
{
for(int i = 0;i < total;i++)
{
System.out.println(list[i].toString());
}
}
}