学校要进行年终总结,需要对教师和学生的评分结果进行统计。学生的统计数据有三个,教师的统计数据有四个。请你实现一个统计系统,对输入的数据进行整理。
请你实现一个Person类表示人员,并实现一些必要的方法,再实现Teacher类和Student类,通过类的继承机制完成这个任务。
输入格式:
首先输入一个数字N,表示输入统计的人数。
接下来是N行,每行是用空格隔开的一系列数字。
输出格式:
N行,每行是一个标识符加一个平均得分(向下取整的整数),用空格隔开。
学生的标识符是Student,教师的标识符是Teacher。
输入样例:
2
2 3 4
2 3 4 5
输出样例:
Student 3
Teacher 3
import java.util.Scanner;
class Person
{
double x,y,z;
Person(double x,double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
}
class Triangle extends Person
{
double e;
Triangle(double x, double y,double z,double z2) {
super(x, y,z);
this.e=z2;
}
int show()
{
return ((int)(x+y+z+e)/4);
}
}
class Rectangle extends Person
{
Rectangle(double x, double y,double z) {
super(x, y, z);
}
int show()
{
return (int)((x+y+z)/3);
}
}
public class Main
{
public static void main(String[] args)
{
double n,x,y,z,e;
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
int []na=new int[(int)n];
for(int i=1;i<=n;i++)
{
if(i%2!=0)
{
x=cin.nextInt();
y=cin.nextInt();
z=cin.nextInt();
Rectangle re=new Rectangle(x, y, z);
na[i-1]=re.show();
}
else
{
x=cin.nextInt();
y=cin.nextInt();
z=cin.nextInt();
e=cin.nextInt();
Triangle th=new Triangle(x, y, z, e);
na[i-1]=th.show();
}
}
for(int i=0;i<n;i++)
{
if(i%2==0)
System.out.println("Student "+na[i]);
else
System.out.println("Teacher "+na[i]);
}
}
}
```
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
class Person {
private int aveGrades = 0;
private String personType = null;
public Person(double x1,double x2,double x3){
setAveGrades((int) ((x1+x2+x3)/3));
personType = "Student";
}
public Person(double x1,double x2,double x3,double x4){
setAveGrades((int) ((x1+x2+x3+x4)/4));
personType = "Teacher";
}
public int getAveGrades() {
return aveGrades;
}
public void setAveGrades(int aveGrades) {
this.aveGrades = aveGrades;
}
public String getPersonType() {
return personType;
}
public void setPersonType(String personType) {
this.personType = personType;
}
public String toString(){
return getPersonType()+" "+getAveGrades();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Person> list = new ArrayList<Person>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while(n>0){
int[] x = new int[4];
String str = in.nextLine();
int count=0;//pan duan xia han you duosao shuju.
StringTokenizer st = new StringTokenizer(str," ");
while(st.hasMoreTokens()){
x[count++] = Integer.parseInt(st.nextToken());
}
Person p;
if(count==4){
p = new Person(x[0],x[1],x[2],x[3]);
}else{
p = new Person(x[0],x[1],x[2]);
}
list.add(p);
n--;
}
Iterator<Person> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next().toString());
}
}
}
兄弟你学过构造方法的重构没有
#请你实现一个Person类表示人员,并实现一些必要的方法,再实现Teacher类和Student类 .....
注意,类名是规定好的...
方法重构
就是说父类和子类中有相同的方法,在子类重新构造。