jmu-Java-03面向对象-06-继承覆盖综合练习-Person、Student、Employee、Company (20 分) ///答案,不包括解析
定义person抽象类,student类、company类,employee类。
person类的属性:string name, int age, boolean gender
person类的方法:
public person(string name, int age, boolean gender); public string tostring(); //返回"name-age-gender"格式的字符串 public boolean equals(object obj);//比较name、age、gender,都相同返回true,否则返回false
student类继承自person
,属性:string stuno, string clazz
student类的方法:
//建议使用super复用person类的相关有参构造函数 public student(string name, int age, boolean gender, string stuno, string clazz); public string tostring(); //返回 “student:person的tostring-stuno-clazz”格式的字符串 public boolean equals(object obj);//首先调用父类的equals方法,如果返回true,则继续比较stuno与clazz。
company类属性:string name
company类方法:
public company(string name); public string tostring(); //直接返回name public boolean equals(object obj);//name相同返回true
employee类继承自person
,属性:company company, double salary
employee类方法:
//建议使用super复用person类的相关有参构造函数 public employee(string name, int age, boolean gender, double salary, company company); public string tostring(); //返回"employee:person的tostring-company-salary"格式的字符串 public boolean equals(object obj);//首先调用父类的equals方法,如果返回true。再比较company与salary。 //比较salary属性时,使用decimalformat df = new decimalformat("#.#");保留1位小数
编写equals方法重要说明:
- 对employee的company属性的比较。要考虑传入为
null
的情况。如果company不为null且传入为null,返回false - 对所有string字符类型比较时,也要考虑
null
情况。
提示
- 排序可使用collections.sort
- equals方法要考虑周全
main方法说明
-
创建若干student对象、employee对象。
输入s
,然后依次输入name age gender stuno clazz
创建student对象。
输入e
,然后依次输入name age gender salary company
创建employee对象。
然后将创建好的对象放入list<person> personlist
。输入其他字符,则结束创建。
创建说明:对于string类型,如果为null
则不创建对象,而赋值为null
。对于company属性,如果为null则赋值为null
,否则创建相应的company对象。 -
对personlist中的元素实现先按照姓名升序排序,姓名相同再按照年龄升序排序。提示:可使用
comparable<person>
或comparator<person>
-
接受输入,如果输入为
exit
则return
退出程序,否则继续下面步骤。 -
将personlist中的元素按照类型分别放到stulist与emplist。注意:不要将两个内容相同的对象放入列表(是否相同是根据equals返回结果进行判定)。
-
输出字符串
stulist
,然后输出stulist中的每个对象。 -
输出字符串
emplist
,然后输出emplist中的每个对象。
1-3
为一个测试点 4-6
为一个测试点
输入样例:
s zhang 23 false 001 net15 e wang 18 true 3000.51 ibm s zhang 23 false 001 net15 e bo 25 true 5000.51 ibm e bo 25 true 5000.52 ibm e bo 18 true 5000.54 ibm e tan 25 true 5000.56 ibm e tan 25 true 5000.51 ibm s wang 17 false 002 null s wang 17 false 002 null e hua 16 false 1000 null s wang 17 false 002 net16 e hua 16 false 1000 null e hua 18 false 1234 microsoft ! continue
输出样例:
employee:bo-18-true-ibm-5000.54 employee:bo-25-true-ibm-5000.51 employee:bo-25-true-ibm-5000.52 employee:hua-16-false-null-1000.0 employee:hua-16-false-null-1000.0 employee:hua-18-false-microsoft-1234.0 employee:tan-25-true-ibm-5000.56 employee:tan-25-true-ibm-5000.51 student:wang-17-false-002-null student:wang-17-false-002-null student:wang-17-false-002-net16 employee:wang-18-true-ibm-3000.51 student:zhang-23-false-001-net15 student:zhang-23-false-001-net15 stulist student:wang-17-false-002-null student:wang-17-false-002-net16 student:zhang-23-false-001-net15 emplist employee:bo-18-true-ibm-5000.54 employee:bo-25-true-ibm-5000.51 employee:hua-16-false-null-1000.0 employee:hua-18-false-microsoft-1234.0 employee:tan-25-true-ibm-5000.56 employee:tan-25-true-ibm-5000.51 employee:wang-18-true-ibm-3000.51
答案
import java.awt.list;
import java.awt.texturepaint;
import java.lang.reflect.array;
import java.text.decimalformat;
import java.util.arraylist;
import java.util.arrays;
import java.util.collections;
import java.util.comparator;
import java.util.scanner;
import javax.naming.binaryrefaddr;
import javax.security.auth.subjectdomaincombiner;
import javax.sound.sampled.line;
class person{
private string name;
private int avg;
private boolean gender;
private string str;
public person(string name, int avg, boolean gender,string str) {
this.name = name;
this.avg = avg;
this.gender = gender;
this.str = str;
}
public string tostring() {
return name+"-"+avg+"-"+gender;
}
public boolean equals(object obj) {
person aperson = (person)obj;
if(aperson.name==null||this.name==null) {
return false;
}
if(aperson.name.compareto(this.name)==0&&aperson.avg==this.avg&&aperson.gender==this.gender) {
return true;
}
return false;
}
public string getname() {
return name;
}
public int getavg() {
return avg;
}
public string getstr() {
return str;
}
public boolean getgender() {
return gender;
}
}
class student extends person {
private string stuno;
private string clazz;
public student(string name, int avg, boolean gender, string str, string stuno, string clazz) {
super(name, avg, gender, str);
this.stuno = stuno;
this.clazz = clazz;
}
public string tostring() {
return super.tostring()+"-"+stuno+"-"+clazz;
}
public boolean equals(object obj) {
student a = (student)obj;
if(super.equals(obj)==true) {
if(this.stuno==null|this.clazz==null||a.clazz==null||a.stuno==null) {
return false;
}
if(this.clazz.compareto(a.clazz)==0&&this.stuno.compareto(a.stuno)==0)
return true;
}
return false;
}
public string getstuno() {
return stuno;
}
public string getclazz() {
return clazz;
}
}
class company{
private string name;
public company(string name) {
this.name = name;
}
@override
public string tostring() {
return name;
}
@override
public boolean equals(object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getclass() != obj.getclass())
return false;
company other = (company) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
class employee extends person{
private company company;
private double salary;
public employee(string name, int avg, boolean gender, string str, company company, double salary) {
super(name, avg, gender, str);
this.company = company;
this.salary = salary;
}
@override
public string tostring() {
return super.tostring()+"-"+company.tostring()+"-"+salary;
}
@override
public boolean equals(object obj) {
if(super.equals(obj)==true) {
employee other = (employee)obj;
if(this.company.tostring()==null||other.company.tostring()==null) {
return false;
}
string df1 = new decimalformat("#.#").format(this.salary);
string df2 = new decimalformat("#.#").format(other.salary);
if(this.company.tostring().compareto(other.company.tostring())==0&&df1.compareto(df2)==0) {
return true;
}
}
return false;
}
public company getcompany() {
return company;
}
public double getsalary() {
return salary;
}
}
public class main {
public static void main(string[] args) {
// todo auto-generated method stub
scanner sc = new scanner(system.in);
string c;
string namestring;
int avg;
boolean gender;
arraylist<person> persons = new arraylist<person>();
arraylist<student> students = new arraylist<student>();
arraylist<employee> employees = new arraylist<employee>();
string tempstring;
string stunostring;
string company;
string clazzstring;
double salary;
while(true) {
c = sc.next();
if(c.compareto("s")==0) {
namestring = sc.next();
avg = sc.nextint();
gender = sc.nextboolean();
stunostring = sc.next();
clazzstring = sc.next();
student tempstudent = new student(namestring, avg, gender, c, stunostring, clazzstring);
persons.add(tempstudent);
}else if(c.compareto("e")==0) {
namestring = sc.next();
avg = sc.nextint();
gender = sc.nextboolean();
salary = sc.nextdouble();
company = sc.next();
company company2 = new company(company);
employee tempemployee = new employee(namestring, avg, gender, c, company2, salary);
persons.add(tempemployee);
}else {
persons.sort(comparator.comparing(person::getname).thencomparingint(person::getavg));
for(int i=0;i<persons.size();i++) {
if(persons.get(i).getstr().compareto("s")==0) {
system.out.println("student:"+persons.get(i).tostring());
int flag = 0;
for(int j=0;j<students.size();j++) {
if(students.get(j).equals(persons.get(i))) {
flag=1;
break;
}
}
if(flag == 0) {
students.add((student) persons.get(i));
}
}else {
system.out.println("employee:"+persons.get(i).tostring());
int flag = 0;
for(int j=0;j<employees.size();j++) {
if(employees.get(j).equals(persons.get(i))) {
flag = 1;
break;
}
}
if(flag == 0) {
employees.add((employee)persons.get(i));
}
}
}
tempstring = sc.next();
if(tempstring.compareto("exit")==0||tempstring.compareto("return")==0) {
return;
}
system.out.println("stulist");
for(int i=0;i<students.size();i++) {
system.out.println("student:"+students.get(i).tostring());
}
system.out.println("emplist");
for(int i=0;i<employees.size();i++) {
system.out.println("employee:"+employees.get(i).tostring());
}
}
}
}
}
上一篇: KTV点歌系统
下一篇: 谷歌重磅发布Cloud AutoML