I am having trouble with this answer to a past question about Java HW. I am runn
ID: 3745970 • Letter: I
Question
I am having trouble with this answer to a past question about Java HW.
I am running Eclipse. Here is the reference answer:
https://www.chegg.com/homework-help/questions-and-answers/72029-create-hourlyemployee-class-inherits-employee-two-new-instance-variables-hours-repre-q20522949
I am getting 2 errors in Eclipse. That is, after I renamed public class TestHourlyEmployee as public class Driver (as required) and deleted all the ### lines.
Eclipse is suggesting to put each as a separate class, but this assignment has to be a single class. My question is exactly the same as the referenced example.
Problem 1: The very first line, "public abstract class Employee" says it has:
Multiple markers at this line
- The public type Employee must be defined in its
own file
- Syntax error on token(s), misplaced construct(s)
Problem 2: At line "public class HourlyEmployee extends Employee" (about half way through) I get the following red x error:
Multiple markers at this line
- The public type HourlyEmployee must be defined in its
own file
- Breakpoint:HourlyEmployee
Explanation / Answer
In Java if you declare all class as public than you must save the file with same as class name, as your file containing multiple public classes your getting this error. so please remove public infront of all classes except the main class I think here it is Driver so now save the file with Driver.java. in your file only Driver class must be public
import java.util.Scanner;
abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee(String first, String last, String ssn)
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName(String first)
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName(String last)
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber(String ssn)
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString()
{
return String.format("%s %s social security number: %s",
getFirstName(), getLastName(), getSocialSecurityNumber());
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
// HourlyEmployee.java
// HourlyEmployee class extends Employee.
class HourlyEmployee extends Employee
{
private double wage; // wage per hour
private double hours; // hours worked for week
// five-argument constructor
public HourlyEmployee(String first, String last, String ssn,
double hourlyWage, double hoursWorked)
{
super(first, last, ssn);
setWage(hourlyWage); // validate and store hourly wage
setHours(hoursWorked); // validate and store hours worked
} // end five-argument HourlyEmployee constructor
// set wage
public void setWage(double hourlyWage)
{
wage = (hourlyWage < 0.0) ? 0.0 : hourlyWage;
} // end method setWage
// return wage
public double getWage()
{
return wage;
} // end method getWage
// set hours worked
public void setHours(double hoursWorked)
{
hours = ((hoursWorked >= 0.0) && (hoursWorked <= 168.0)) ?
hoursWorked : 0.0;
} // end method setHours
// return hours worked
public double getHours()
{
return hours;
} // end method getHours
// calculate earnings; override abstract method earnings in Employee
public double earnings()
{
return getWage() * getHours();
} // end method earnings
// return String representation of HourlyEmployee object
public String toString()
{
return String.format("hourly employee: %s %s: $%,.2f %s: %,.2f %s: %,.2f",
super.toString(),
"hours", getHours(),
"wage", getWage(),
"earnings", earnings());
} // end method toString
} // end class HourlyEmployee
public class Driver6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// taking user input
System.out.print("Enter first name: ");
String first = sc.next();
System.out.print("Enter last name: ");
String last = sc.next();
System.out.print("Enter social security number: ");
String ssn = sc.next();
System.out.print("Enter hours worked: ");
double hoursWorked = sc.nextDouble();
System.out.print("Enter wage: ");
double hourlyWage = sc.nextDouble();
// creating an object of HourlyEmployee
HourlyEmployee emp = new HourlyEmployee(first, last, ssn, hourlyWage, hoursWorked);
System.out.println(emp);
}
}
save this file with the Driver6.java and let me know the behaviur
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.