gpt4 book ai didi

java - 链接列表和 Try/Catch

转载 作者:行者123 更新时间:2023-12-01 11:06:59 28 4
gpt4 key购买 nike

所以问题是:

Create a Employee class which cannot be instantiated and has the following      fields:

• String name;
• String ppsNumber;
• int employeeNumber;
• int numberOfEmployees;
• protected double pay;

These data members are tracked using appropriate setter and getter methods. Hourly
employees are paid €15.00 per hour and commission based employees are paid 10% of the
value of their sales figure. In the class you are required to appropriately plan for a earnings
method (double earnings() and ensure that the method must be implemented in subclasses.

Create a HourlyEmployee class and a CommissionEmployee class that are derived from
Employee. In each of these classes, create an overridden method named earnings.

Finally, you are required to test your classes using a client class and to store the instances of
employees into a LinkedList, using try/catch blocks where appropriate.

我已经完成了大部分工作,只是卡在了最后,我在其中的某个地方犯了一些错误,这里是:

public abstract class Employee {

//private instances
private String name;
private String ppsNumber;
private int employeeNumber;
private int numberOfEmployees;
protected double earnings;
protected double pay;
protected double sales;

//default constructor
public Employee(String name, String ppsNumber, int employeeNumber, int numberOfEmployees, double earnings, double pay, double sales) {
name = "Not Given";
ppsNumber = "Not Givwn";
employeeNumber = 0;
numberOfEmployees = 0;
earnings = 0;
pay = 0;
sales = 0;
}

//getters and setters
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}

public String getPPSNumber(){
return ppsNumber;
}
public void setPPSNumber(String ppsNumber){
this.ppsNumber = ppsNumber;
}

public int getEmployeeNumber(){
return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber){
this.employeeNumber = employeeNumber;
}

public int getNumberOfEmployees(){
return numberOfEmployees;
}
public void setNumberOfEmployees(int numberOfEmployees){
this.numberOfEmployees = numberOfEmployees;
}

public double getPay(){
return pay;
}
public void setPay(double pay){
this.pay = pay;
}

public double getEarnings(){
return earnings;

}
public void setEarnings(double earnings){
this.earnings = earnings;
}

public double getSales(){
return sales;
}
public void setSales(double sales){
this.sales = sales;
}

//calculateEarnings method
abstract double calculateEarnings();

//toString method
@Override
public String toString(){
return name + ppsNumber + getEmployeeNumber();
}

}//end class

.

import java.util.Scanner;

public class HourlyEmployee extends Employee {
public HourlyEmployee(){
super();
}

//constructor
public HourlyEmployee(String name, String ppsNumber, int employeeNumber, int numberOfEmployees, double earnings, double pay){
super(name, ppsNumber, employeeNumber, numberOfEmployees, earnings, pay, pay);
}

//calculate earnings method
@Override
double calculateEarnings() {
//input hours worked to calculate earnings
Scanner input = new Scanner(System.in);
System.out.println("Enter Hours Worked: ");
pay = input.nextDouble();
//calculate earnings
earnings = pay * 15;
System.out.println("Employee's Earnings: " + earnings);
return earnings;

}//end method
}//end class

.

import java.util.Scanner;

public class CommissionEmployee extends Employee {
public CommissionEmployee(){
super();
}

//constructor
public CommissionEmployee(String name, String ppsNumber, int employeeNumber, int numberOfEmployees, double earnings, double sales){
super(name, ppsNumber, employeeNumber, numberOfEmployees, earnings, sales, sales);
}

//calculate earnings methods
@Override
double calculateEarnings() {
//input sales value to calculate earnings
Scanner input = new Scanner(System.in);
System.out.println("Enter Sales Value: ");
sales = input.nextDouble();
//calculate earnings
earnings = sales * 0.1;
System.out.println("Employee's Earnings: " + earnings);
return earnings;

}//end method
}//end class

.

import java.util.LinkedList;
import java.util.Scanner;
import java.util.List;
import java.lang.ArrayIndexOutOfBoundsException;
import java.util.InputMismatchException;

public class EmployeeTester {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

int numberOfEmployees = 0;

while (numberOfEmployees == 0){
try{
System.out.print("Enter Number of Employees: ");
numberOfEmployees = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("Exception Thrown " + e);
}
input.nextLine();//clears input
}//end while

Employee[] employees = new Employee[numberOfEmployees];

//create linked list
List<Employee> employeeList = new LinkedList<Employee>();

//declare & create an HourlyEmployee
employeeList.add(new HourlyEmployee());

for(int i=0; i<employees.length; i++){

String name;
String ppsNumber;
int employeeNumber;
int x = 0;

System.out.print("Name: ");
name = input.next();
System.out.print("PPS Number: ");
ppsNumber = input.next();
System.out.print("Employee Number: ");
employeeNumber = input.nextInt();

employeeList.get(x).setName(name);

x++; //increment x


try{//if not using length to control loop
employees[i].setName(name);
employees[i].setPPSNumber(ppsNumber);
employees[i].setEmployeeNumber(employeeNumber);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out of Bounds");
}

}//end for

//enhanced for loop
for(Employee element:employees){
System.out.println(element.toString());
System.out.println(element.calculateEarnings());
}


//declare & create an CommissionEmployee
employeeList.add(new CommissionEmployee());

for(int i=0; i<employees.length; i++){

String name;
String ppsNumber;
int employeeNumber;
int x = 0;

System.out.print("Name: ");
name = input.next();
System.out.print("PPS Number: ");
ppsNumber = input.next();
System.out.print("Employee Number: ");
employeeNumber = input.nextInt();

employeeList.get(x).setName(name);

x++; //increment x


try{//if not using length to control loop
employees[i].setName(name);
employees[i].setPPSNumber(ppsNumber);
employees[i].setEmployeeNumber(employeeNumber);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out of Bounds");
}

}//end for

//enhanced for loop
for(Employee element:employees){
System.out.println(element.toString());
System.out.println(element.calculateEarnings());
}

}//end main()
}//end class

所以最初我的问题是,当我运行代码时,它会询问有多少员工,并询问姓名、pps 号码和员工号码,然后我收到错误:

Exception in thread "main" java.lang.NullPointerException
at EmployeeTester.main(EmployeeTester.java:54)

然后我意识到我忘记添加小时雇员和佣金雇员的构造函数,所以我这样做了,现在父类(super class)出现了错误:

public class HourlyEmployee extends Employee {
public HourlyEmployee(){
super();
}

错误读取

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The constructor Employee() is undefined

at HourlyEmployee.<init>(HourlyEmployee.java:5)
at EmployeeTester.main(EmployeeTester.java:31)

我仍然遇到第一个问题,如果有人可以帮助我修复它并让代码再次运行那就太好了,提前致谢

最佳答案

我注意到的一些问题(假设这是第 54 行):

try{//if not using length to control loop
employees[i].setName(name);
employees[i].setPPSNumber(ppsNumber);
employees[i].setEmployeeNumber(employeeNumber);
}

在此代码中,您假设存储在 employees 数组中的对象已初始化,但事实并非如此。你需要做这样的事情

try{//if not using length to control loop
employees[i] = new HourlyEmployee(); //or some other employee.
employees[i].setName(name);
employees[i].setPPSNumber(ppsNumber);
employees[i].setEmployeeNumber(employeeNumber);
}

其次,这个错误:线程“main”java.lang.Error中的异常: Unresolved 编译问题:
构造函数 Employee() 未定义
意味着Employee类没有无参数构造函数。在 Java 中,默认情况下每个对象都有一个无参数构造函数,但是当您提供自己的构造函数时,它将被删除。

因此,您需要在 Employee 类中创建一个新的无参数构造函数。

编辑:作为旁注,虽然展示您尝试过的内容很好,但在提出 future 问题时请不要提供所有代码,而应仅提供相关内容并补充一些显示问题的代码。

关于java - 链接列表和 Try/Catch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32847400/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com