I was doing the code as part of my lab tutorial. When I tried running the code, it doesn't work.
Line 68 has an error. Can help?

package t10;

import java.util.*;

class Employee
{
    private String name;
    private String title;

    public void setName(String n)
    {
        name = n;
    }

    public void setTitle(String t)
    {
        title = t;
    }

    public String getDetails()
    {
        return "Name: " + name +
               "\nTitle: " + title;
    }

    public Employee(String name, String title)
    {
        this.name = name;
        this.title = title;
    }
}


class Worker extends Employee
{
    private int hour;
    private double rate;

    public void setHour(int h)
    {
        hour = h;
    }

    public void setRate(double r)
    {
        rate = r;
    }

    public double getSalary()
    {
        return (double) hour * rate;
    }

    public Worker(int h, double r, String name, String title) 
    {
        super(name, title);
        hour = h;
        rate = r;
    }

}

public class Q1 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        Worker w = new Worker();

        System.out.println("Enter your name and title: ");
        w.setName(scan.next());
        w.setTitle(scan.next());

        System.out.println("\nEnter number of hours: ");
        w.setHour(scan.nextInt());

        System.out.println("\nEnter the pay rate (in $): ");
        w.setRate(scan.nextDouble());

        System.out.println();
        System.out.println(w.getDetails());

        System.out.print("\nYour salary: $");
        System.out.println(w.getSalary());

    }
}

Recommended Answers

All 4 Replies

If you are using an IDE like Eclipse or Netbeans you can see that, by simply hovering over line 68, the error reads "The constructor Worker() is undefined". It means that you do not have an empty constructor in your Worker class. You only have one that requires paramaters: public Worker(int h, double r, String name, String title).

How to fix?

Well that depends on what you want to achieve. Create an empty constructor (which will leave you with the possibility of workers that have no information) or supply values when creating the instance, for instance dummy/default values until a user has had a chance to provide the information. Or you could store the information the user provides in variables and not create a Worker instance until all the information is present (and depending on the assignment, valid).

The question to ask is "which variables MUST be set for the instance to be in a valid state?" Eg Maybe every Emplyee MUST have a name, but maybe a title is optional. Maybe every Worker MUST have an hourly rate, but maybe it's OK to default the hours to zero until told otherwise.

Now you goal is to ensure that nobody can create an invalid instance. That certainly means you do not have a no-args constructor.

There are two main options:
1. Only have constructors that guarantee a valid instance (ie have parameters for all mandatory fields)
2. Use a "factory" class to create new instances - this approach copes with very complex cases, and is common in real life, but it's over the top for this simple exercise.

So, in this case, Traevel's last option is the one to go for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.