概述
4-2 jmu-java-m04-Person、Teacher与Student (10分)
该程序包含
Person类:
属性:int id, String name, LocalDate birthDate。注意:定义时需按照顺序从上到下定义。
方法:构造方法(id, name, year, month, dayOfMonth), id与birthDate的getter方法, name的getter/setter方法,Eclipse自动生成的tostring。
Student类继承自Person:
属性:int enrollmentYear, String clazzName。
方法:构造方法(id, name, year, month, dayOfMonth, enrollmentYear, clazzName),建议使用super复用Person的构造函数。toString方法(包含两行,第一行为父类的toString方法,第二行为Student类自身属性的toString方法,具体格式见输出样例)
Teacher类继承自Person:
属性:String department, double salary。
方法:构造方法(id, name, year, month, dayOfMonth, department, salary),建议使用super复用Person的构造函数。toString方法(包含两行,第一行为父类的toString方法,第二行为Teacher类自身属性的toString方法,具体格式见输出样例)
main方法:
public static void main(String[] args) {
String[] types = {“Student”,“Teacher”};
//输入种子seed,并用其初始化Random对象
//输入对象生成数量n
//以下是生成n个对象的循环
//使用random.nextInt(2)从types数组中随机选取元素x并输出
//根据x选择输入相应的参数
//n个对象的"id,name,birthdate"
//输出数组中的每个对象的toString
}
输入格式:
随机数种子seed
产生对象数量n
n行,每行是每个对象的信息信息。
如果要生成Student对象则依次输入:姓名 年 月 日 入学年份 班级
如果要生成Teacher对象则依次输入:姓名 年 月 日 系 薪水
注意:每个对象的id为每次循环的序数,依次从0到n-1
输出格式:
n个对象的类型
n个对象"id,name,birthDate"
n个对象的toString
输入样例:
3
6
Liz 1986 10 10 CS 2000
Tom 1978 5 7 CS 2500.5
Sally 2000 1 3 2018 Net01
Jerry 1988 6 7 Mechanical 1990
Eliza 2001 2 5 2019 Software02
Li 1978 5 7 cs 2400
输出样例:
Teacher
Teacher
Student
Teacher
Student
Teacher
0,Liz,1986-10-10
1,Tom,1978-05-07
2,Sally,2000-01-03
3,Jerry,1988-06-07
4,Eliza,2001-02-05
5,Li,1978-05-07
Person [id=0, name=Liz, birthDate=1986-10-10]
Teacher [department=CS, salary=2000.0]
Person [id=1, name=Tom, birthDate=1978-05-07]
Teacher [department=CS, salary=2500.5]
Person [id=2, name=Sally, birthDate=2000-01-03]
Student [enrollmentYear=2018, clazzName=Net01]
Person [id=3, name=Jerry, birthDate=1988-06-07]
Teacher [department=Mechanical, salary=1990.0]
Person [id=4, name=Eliza, birthDate=2001-02-05]
Student [enrollmentYear=2019, clazzName=Software02]
Person [id=5, name=Li, birthDate=1978-05-07]
Teacher [department=cs, salary=2400.0]
作者
郑如滨
单位
集美大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.*;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String[] types = {"Student","Teacher"};
int seed=sc.nextInt();
Random r = new Random(seed);//输入种子seed,并用其初始化Random对象
int n=sc.nextInt();//输入对象生成数量n
Person[] p = new Person[n];
String[] strr = new String[n];
for(int i=0;i<n;i++)//以下是生成n个对象的循环
{
String str =types[r.nextInt(2)];
//使用random.nextInt(2)从types数组中随机选取元素x并输出
strr[i]=str;///
//根据x选择输入相应的参数
if(str.equals("Student"))
{
String name=sc.next();
int year=sc.nextInt();
int month=sc.nextInt();
int dayOfMonth=sc.nextInt();
int enrollmentYear=sc.nextInt();
String clazzName=sc.next();
Student s = new Student(i,name,year,month,dayOfMonth,enrollmentYear,clazzName);
p[i]=s;
}
else if(str.equals("Teacher"))
{
String name=sc.next();
int year=sc.nextInt();
int month=sc.nextInt();
int dayOfMonth=sc.nextInt();
String department=sc.next();
double salary=sc.nextDouble();
Teacher t = new Teacher(i,name,year,month,dayOfMonth,department,salary);
p[i]=t;
}
}
for(String f:strr)
{
System.out.println(f);
}
for(Person x:p)
{
System.out.println(x.getId()+","+x.getName()+","+x.getBirthDate());
}
for(Person x:p)
{
System.out.println(x);
}
}
}
class Person
{
int id;
String name;
LocalDate birthDate;
int year;
int month;
int dayOfMonth;
public Person(int id,String name,int year,int month,int dayOfMonth)
{
this.id=id;
this.name=name;
this.year=year;
this.month=month;
this.dayOfMonth=dayOfMonth;
this.birthDate=LocalDate.of(this.year,this.month,this.dayOfMonth);
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDate getBirthDate() {
return birthDate;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]";
}
}
class Student extends Person
{
int enrollmentYear;
String clazzName;
public Student(int id,String name,int year,int month,int dayOfMonth,int enrollmentYear,String clazzName)
{
super(id,name,year,month,dayOfMonth);//使用super复用Person的构造函数
this.enrollmentYear=enrollmentYear;
this.clazzName=clazzName;
}
public String toString()
{
return super.toString()+"n"+ "Student [enrollmentYear="+enrollmentYear+", clazzName="+clazzName+"]";
}
}
class Teacher extends Person
{
String department;
double salary;
public Teacher(int id,String name,int year,int month,int dayOfMonth,String department,double salary)
{
super(id,name,year,month,dayOfMonth);
this.department=department;
this.salary=salary;
}
public String toString()
{
return super.toString()+"n"+"Teacher [department="+department+", salary="+salary+"]";
}
}
最后
以上就是激情心情为你收集整理的4-2 jmu-java-m04-Person、Teacher与Student (10分)的全部内容,希望文章能够帮你解决4-2 jmu-java-m04-Person、Teacher与Student (10分)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复