简体   繁体   中英

[Java]Why am I getting these errors in my tester class?

I have a super class, sub class, and a tester class. Can anyone help with what I'm doing wrong?

Here is what I need in my tester class- 1. Create objects of Manager and Employee.

  1. Create a function revise salary to revise the salary set in the constructor of Manager or Employee. (Use concept of polymorphism)

  2. Use the object's Display functions to print the relevant information pertaining to the object's class.

Here are my classes

Superclass:

public class Employee {
    private int employeeNumber;
    private String employeeName;
    private double employeeSalary;

    public Employee(int employeeNumber, String employeeName, double employeeSalary) {
         this.employeeNumber = employeeNumber;
         this.employeeName = employeeName;
         this.employeeSalary = employeeSalary;
    }

         public double getEmployeeSalary() {
            return employeeSalary;
            }

         public void setEmployeeSalary(double employeeSalary) {
            this.employeeSalary = employeeSalary;
            }



         public void display(){
               System.out.println("Employee Number: "+ employeeNumber +"\n"
                     + "Employee Name: " + employeeName + "\n" 
                     + "Employee Salary: "  + employeeSalary);
            }

        }

Subclass:

public class Manager extends Employee {

    private int rewards;
    public Manager(int employeeNumber, String employeeName, double employeeSalary) {
        super(employeeNumber, employeeName, employeeSalary);
    }


    public void display() {
        super.display();
        System.out.println(rewards);
    }
}

Tester:

public class Test {
    public static void main(String [] args) {


         Manager manager = new Manager(11111, "Elon Musk", 42344);
          manager.display();     

         Employee employeeOne = new Employee(324, "Bob Den", 3522);
         Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
         Employee employeeThree = new Employee(42, "Asif Blar", 4321);

         private void reviseSalary() {
         double employeeSalary = manager.getEmployeeSalary();
         manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
         manager.display();
         }

    }

}

**My issue:

I am getting errors on my test class. When I create a manager object, it says the constructor is undefined. Also, for my private void "reviseSalary", it says I cannot use void

Can anyone tell me what I'm doing wrong, and help in creating my reviseSalary function if possible

Thanks

Just put your reviseSalary out of main method.

This error happens because Java does not support nested function.

    public class Test {
    public static void main(String [] args) {


        Manager manager = new Manager(11111, "Elon Musk", 42344);
        manager.display();

        Employee employeeOne = new Employee(324, "Bob Den", 3522);
        Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
        Employee employeeThree = new Employee(42, "Asif Blar", 4321);

        reviseSalary(manager);
    }
    private static void reviseSalary(Manager manager) {
        double employeeSalary = manager.getEmployeeSalary();
        manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
        manager.display();
    }
}

This will be your output:

  • Employee Number: 11111

    • Employee Name: Elon Musk
    • Employee Salary: 42344.0
  • Employee Number: 11111

    • Employee Name: Elon Musk
    • Employee Salary: 46578.4

I see two main problems with your code:

1) You declare method reviseSalary() inside main() method. Java doesn't allow you to declare method inside of the other method. You should rather declare it outside of the main() method and add a keyword static to let you call this method without the need of having an instance of your Test class. Otherwise, if you don't declare this method as static , you would need an instance of Test class and have to call reviseSalary() on this instance like this:

Test t = new Test() ;
t.reviseSalary();

2) You declared this field in the Manager class: private int rewards , but you don't declare any setter method to assign any value to that field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM