Employee and EmployeeDemo how to get the following result for two separate employees?
I need to create an Employee class and a Demo class that uses it. I have the following:
Employee class:
public class KNW_Employee
{
private int empID;
private double payRate;
private double weeklyHrs;
public KNW_Employee()
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
}
public KNW_Employee(int empID)
{
this.empID = empID;
}
public KNW_Employee(int empID, double payRate, double weeklyHrs)
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
}
public void setID(int empID)
{
this.empID = empID;
}
public void setPayRate(double payRate)
{
this.payRate = payRate;
}
public void setWeeklyHrs(double weeklyHrs)
{
this.weeklyHrs = weeklyHrs;
}
public int getID()
{
return empID;
}
public double getPayRate()
{
return payRate;
}
public double getWeeklyHrs()
{
return weeklyHrs;
}
public double getWeeklyPay()
{
return weeklyHrs * payRate;
}
public String toString()
{
return "ID for First Employee: " + getID() +
"\nPayRate for First Employee: " + getPayRate() +
"\nWeekly Hours for First Employee: " + getWeeklyHrs() +
"\nWeekly Pay for First Employee: " + getWeeklyPay() +
"\n\nID for Second Employee: " + getID() +
"\nPayRate for Second Employee: " + getPayRate() +
"\nWeekly Hours for Second Employee: " + getWeeklyHrs() +
"\nWeekly Pay for Second Employee: " + getWeeklyPay();
}
}
Demonstration employee:
class EmployeeDemo
{
public static void main (String[]args)
{
//Create scanner object
Scanner input = new Scanner(System.in);
//Declare variables
int emp1;
int emp2;
double hour1;
double hour2;
double pay1;
double pay2;
//Ask for employee ID
System.out.println("Enter ID for Employee 1: ");
emp1 = input.nextInt();
System.out.println("Enter ID for Employee 2: ");
emp2 = input.nextInt();
//Ask for employee hours
System.out.println("Enter Employee 1 Hours: ");
hour1 = input.nextDouble();
System.out.println("Enter Employee 2 Hours: ");
hour2 = input.nextDouble();
//Ask for employee pay
System.out.println("Enter Employee 1 Pay: ");
pay1 = input.nextDouble();
System.out.println("Enter Employee 2 Pay: ");
pay2 = input.nextDouble();
//Call Employee Class
KNW_Employee e = new KNW_Employee();
e.getID();
e.getWeeklyHrs();
e.getPayRate();
System.out.println(e.toString());
}
}
I just need to print toString()
for two separate employees. It allows me to enter data, but as soon as it comes time to print the data, I just get zeros. Does anyone have any idea?
You have not provided set
values for the objects employee
to be executed as shown in the code below, with inline comments:
//create employee objects first
KNW_Employee emploee1 = new KNW_Employee();
KNW_Employee emploee2 = new KNW_Employee();
//Ask for employee ID
System.out.println("Enter ID for Employee 1: ");
emp1 = input.nextInt();
emploee1.setID(emp1);//set the id value for employee1
System.out.println("Enter ID for Employee 2: ");
emp2 = input.nextInt();
emploee2.setID(emp2);//set the id value for employee2
//Ask for employee hours
System.out.println("Enter Employee 1 Hours: ");
hour1 = input.nextDouble();
emploee1.setWeeklyHrs(hour1);
System.out.println("Enter Employee 2 Hours: ");
hour2 = input.nextDouble();
emploee1.setWeeklyHrs(hour2);
//Ask for employee pay
System.out.println("Enter Employee 1 Pay: ");
pay1 = input.nextDouble();
emploee1.setPayRate(pay1);
System.out.println("Enter Employee 2 Pay: ");
pay2 = input.nextDouble();
emploee1.setPayRate(pay2);
System.out.println(emploee1);//print employee1 (this calls toString())
System.out.println(emploee2);//print employee2 (this calls toString())
Another important point is that you do not need to explicitly call the method toString()
on the object employee
, and if you use System.out.println(emploee1)
, it calls implicitly toString()
, you can link to here on this (text copied below, emphasis mine).
The Object toString () method returns a string representation of the object, which is very useful for debugging. String representation since object depends entirely on object, so you need to override toString () in your classes. You can use toString () together s> System.out.println () to display the text representation of an object ,> like a book instance:
System.out.println (firstBook.toString ()) ;
source to share
The problem is creating your instance and setting the value for KNW_Employee in EmployeeDemo. Replace the // Call Employee Class code with the following code.
KNW_Employee employee1 = new KNW_Employee(emp1,pay1,hour1);
KNW_Employee employee2 = new KNW_Employee(emp2,pay2,hour2);
System.out.println(employee1.toString());
System.out.println(employee2.toString());
source to share
package org.sunil.addressbook.dao;
public class KNW_Employee
{
private int empID;
private String empNumber;
private double payRate;
private double weeklyHrs;
public KNW_Employee(int empID,String empNumber,double payRate,double weeklyHrs)
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
this.empNumber=empNumber;
}
public int getEmpID() {
return empID;
}
public void setEmpID(int empID) {
this.empID = empID;
}
public String getEmpNumber() {
return empNumber;
}
public void setEmpNumber(String empNumber) {
this.empNumber = empNumber;
}
public KNW_Employee(int empID)
{
this.empID = empID;
}
public KNW_Employee(int empID, double payRate, double weeklyHrs)
{
this.empID = empID;
this.payRate = payRate;
this.weeklyHrs = weeklyHrs;
}
public void setID(int empID)
{
this.empID = empID;
}
public void setPayRate(double payRate)
{
this.payRate = payRate;
}
public void setWeeklyHrs(double weeklyHrs)
{
this.weeklyHrs = weeklyHrs;
}
public int getID()
{
return empID;
}
public double getPayRate()
{
return payRate;
}
public double getWeeklyHrs()
{
return weeklyHrs;
}
public double getWeeklyPay()
{
return weeklyHrs * payRate;
}
public String toString()
{
return "ID for "+getEmpNumber()+" Employee: " + getID() +
"\nPayRate for "+getEmpNumber()+" Employee: " + getPayRate() +
"\nWeekly Hours for "+getEmpNumber()+" Employee: " + getWeeklyHrs() +
"\nWeekly Pay for "+getEmpNumber()+" Employee: " + getWeeklyPay();
}
}
And the Demo class should be like this: -
package org.sunil.addressbook.dao;
import java.util.Scanner;
class EmployeeDemo
{
public static void main (String[]args)
{
//Create scanner object
Scanner input = new Scanner(System.in);
//Declare variables
int emp1;
int emp2;
String emp1Number;
String emp2Number;
double hour1;
double hour2;
double pay1;
double pay2;
//Ask for employee ID
System.out.println("Enter ID for Employee 1: ");
emp1 = input.nextInt();
System.out.println("Enter ID for Employee 2: ");
emp2 = input.nextInt();
System.out.println("Enter Name for Employee 1: ");
emp1Number = input.next();
System.out.println("Enter Name for Employee 2: ");
emp2Number = input.next();a
//Ask for employee hours
System.out.println("Enter Employee 1 Hours: ");
hour1 = input.nextDouble();
System.out.println("Enter Employee 2 Hours: ");
hour2 = input.nextDouble();
//Ask for employee pay
System.out.println("Enter Employee 1 Pay: ");
pay1 = input.nextDouble();
System.out.println("Enter Employee 2 Pay: ");
pay2 = input.nextDouble();
//Call Employee Class
KNW_Employee e1 = new KNW_Employee(emp1,emp1Number,hour1,pay1);
KNW_Employee e2 = new KNW_Employee(emp2,emp2Number,hour2,pay2);
System.out.println(e1.toString());
System.out.println(e2.toString());
}
}
source to share