C:\jexp>javac employeetest2.java
employeetest2.java:57: cannot find symbol
symbol : constructor Employee2()
location: class Employee2
Employee2 employee = new Employee2();
^
1 error
There is no explicit constructor declared in the Employee2 class so the jvm calls the default class constructor which the jvm gives to any class without a constructor.
// This is a method with a void return type, ie, the method
// does not return anything to the caller.
public void Employee2(String name, double hours, double rate)
// This is a constructor: a special method that does not have a
// return type int its declaration and whose name is the same as
// its enclosing class.
public Employee2(String name, double hours, double rate)
For more on constructors see
Providing Constructors for Your Classes.
Works okay now:
import java.util.Scanner;
public class EmployeeTest2
{
public static void main( String[] args)
{
boolean stop = false;
while (!stop)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter employee's name or stop to quit: ");
String EmpName = input.nextLine();
if (EmpName.equals("stop"))
{
System.out.println("This program has ended successfully.");
stop = true;
input.close();
break;
}
else
{
double PayRate;
double HoursWorked;
double total;
System.out.print("Enter the pay rate per hour of " +
"the employee: ");
PayRate = input.nextDouble();
while (PayRate <= 0)
{
System.out.println();
System.out.println("Pay rate must be a positve number. ");
System.out.print("Please enter pay rate per hour again: ");
PayRate = input.nextDouble();
System.out.println();
}
System.out.print("Enter the employee's hours " +
"worked this pay week: ");
HoursWorked = input.nextDouble();
while (HoursWorked <= 0)
{
System.out.println();
System.out.println("Hours worked must be a " +
"positive number. ");
System.out.print("Please enter hours worked " +
"this pay week again: ");
HoursWorked = input.nextDouble();
System.out.println();
}
Employee2 employee =
new Employee2(EmpName, PayRate, HoursWorked);
System.out.printf( "Employee Name: %s, Weekly pay is $%,.2f%n",
employee.empName, employee.getWeeklyPay() );
}
}
}
}
class Employee2
{
String empName;
double hoursWorked;
double payRate;
double total;
public Employee2(String name, double hours, double rate)
{
this.empName = name;
this.hoursWorked = hours;
payRate = rate;
computeWeeklyPay();
}
private void computeWeeklyPay()
{
total = hoursWorked * payRate;
}
public double getWeeklyPay()
{
return total;
}
}