我是Java新手,很可能在我的代码上犯了一个愚蠢的错误。特别是,我对主要参数有问题,要求并保存扫描器对象以链接到其他子类。我相信人员,学生,员工,教师和职员部分是正确的,但我无法弄清楚驾驶员部分。以下是作业问题。提前感谢您的帮助或建议!Java继承和多态计划
家庭问题: 设计一个名为Person的类及其两个子类Student和Employee。使员工的教师和职员的子类。
Person对象具有名称,地址,电话号码和电子邮件地址(所有字符串)。
学生具有班级地位(大一,大二,大三或大四)。将状态定义为最终的字符串变量。
员工有一个办公室号码,工资(两个整数)和聘用日期。使用下面定义的指明MyDate类来创建雇用日期的对象:
class MyDate{
private String date; //date in the form mm/dd/yy
public MyDate(String date){
this.date = date;
}
public String getDate(){
return date;
}
}
教员对象有办公时间和秩(两个都是字符串),而工作人员对象有一个标题(字符串)。
对于Student,Faculty和Staff类,创建存储有关对象信息的toString方法(以下面示例中所示的格式)。
在Driver类(在同一个文件中)中测试你的类,询问用户他们想要创建什么类型的对象以及他们希望拥有哪些信息。 程序然后使用该对象的toString方法来打印关于该对象的信息。 样品试验#1
输入 1)要创建一个学生 2)要创建一个雇员:1
输入名称:瓦尔特·D·格雷厄姆
输入地址:4737体育场驱动Whitinsville,MA 01588
进入电话号码:508-266-2108
输入电子邮件:[email protected]
输入学生的地位:初级
学生:瓦尔特·D·格雷厄姆 状态:初级 地址:4737体育场驱动Whitinsville,MA 0158 8 电话号码:508-266-2108 电子邮件地址:walterdgraham @ university。 EDU 样品试验#2
输入 1)要创建一个学生 2)要创建一个雇员:2
输入名称:金佰利D.戴维斯
输入地址:795大学山路迪凯特,IL 62522
输入电话号码:217-519-6747
输入电子邮件:kimdavis @ school。EDU
输入 1)创建一个教员 2)要创建一个职员:1
输入办公室号码:724
输入工资:84101
输入日期雇用(MM/DD/YY):06/12/92
输入办公时间:5至7点,周一至周四
进入排名:兼职教授
学院:金佰利D.戴维斯 等级:兼职教授 薪水:聘请$ 84101 日期:92年6月12日
办公时间:周一至周五,晚5-7点 办公室:724 地址:795 University Hill Road Decatur,IL 62522 电话:217-519-6747 电子邮件:kimdavis @ school。 EDU
驱动器部分,我写:
package person;
import java.util.Scanner;
public class Driver {
//Testing
public static void main(String[] args) {
int studentOrEmployee;
int facultyOrStaff;
Scanner input = new Scanner(System.in);
System.out.println("Enter");
System.out.println("1)To create a student:");
System.out.print("2)To create and employee: ");
studentOrEmployee= input.nextInt();
System.out.print("\n Enter name: ");
Person name = input.next();
System.out.print("\n Enter address: ");
Person address = input.nextString();
System.out.print("\n Enter phone number: ");
Person phone = input.nextString();
System.out.print("\n Enter email ");
Person email = input.nextString();
if (studentOrEmployee==1) {
System.out.print("\n Enter student’s status: ");
Person classStanding = input.nextString();
System.out.println(student.toString() + "\n");
}
else if (studentOrEmployee==2) {
facultyOrStaff= input.nextInt();
if (facultyOrStaff==1) {
System.out.print("\nEnter office number: ");
Person office = input.nextInt();
System.out.print("\nEnter salary");
Person salary = input.nextInt();
System.out.print("\nEnter date hired: ");
Person dateHired = input.next();
System.out.print("\nEnter office hours: ");
Person hours = input.nextString();
System.out.print("\nEnter rank: ");
Person rank = input.nextString();
System.out.println(faculty.toString() + "\n");
}
else if (facultyOrStaff==2) {
System.out.print("\nEnter office number: ");
System.out.print("\nEnter office title: ");
Person title = input.nextString();
System.out.println(staff.toString() + "\n");
}
}
Person person = new Person(name,address, phone, email);
Person student = new Student(name, classStanding, address, phone, email);
Person employee = new Employee(name, address, phone, email, office, salary, dateHired);
Person faculty = new Faculty(name, rank, salary, dateHired, hours, address, phone, email, office);
Person staff = new Staff(name, title, address, phone, email, office, salary, dateHired);
}
}
您能为我们构建一个[** m ** cve](https://stackoverflow.com/help/mcve)吗? – WorldSEnder
我不知道如何回答这个问题目前的形式。你有什么实际问题? –
程序不接受扫描器输入:线程“main”中的异常java.lang.RuntimeException:不可编译的源代码 - 不兼容的类型:java.lang.String不能转换为person.Person \t at person.Driver.main( – 3viline