Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am not sure what is happening here. For some reason I am initialzing the paren

ID: 3696384 • Letter: I

Question

I am not sure what is happening here. For some reason I am initialzing the parent class fields and methods. But when I create the object, it does not have the methods of the extended class. Here is the code and any help would be appreciated. The error is happening at lines 136 and 137 when I use the pw.getShift or pw.getPayRate.

/***************************************************
* Greg Roller
* Chapter 11= 1
* CISP II 1020 70
* Prof. Daniel Devers
****************************************************/

/***************************************************
* WorkerDemo.java
*
*
****************************************************/

package workerdemo;


import java.text.DecimalFormat;

class Employee {

// Fields
public String emplName, emplNumber, emplHireDate;

public Employee(){
  String emplName = " ";
  String emplNumber = " ";
  String emplHireDate = " ";
}

public Employee( String Name, String Number, String HireDate){


  setName(Name);
  setEmployeeNumber(Number);
  setHireDate(HireDate);
}

public void setName(String epName){
  emplName = epName;
}
public void setEmployeeNumber(String epNum){
  emplNumber = epNum;

}
public void setHireDate(String epHire){
  emplHireDate = epHire;
}


public String getName(){
  return emplName;
}
public String getEmployeeNumber(){
  return emplNumber;
}
public String getHireDate(){
  return emplHireDate;
}

}

class ProductionWorker extends Employee {

private int shift;
private double payRate;
public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2;

      

public ProductionWorker(){
      
  shift = DAY_SHIFT;
  payRate = 0.00;
}

public ProductionWorker(String Name, String number, String HireDate, int Shift, double HourlyRate){
                super(Name, number, HireDate);
  setName(Name);
                setEmployeeNumber(number);
                setHireDate(HireDate);
  setShift(Shift);
  setPayRate(HourlyRate);

}

public final void setShift(int S){
  shift = S;
}
public final void setPayRate(double R){
  payRate = R;
}

public final int getShift(){
  return shift;
}
public final double getPayRate(){
  return payRate;
}

}

/**
          Chapter 11
          Programming Challenge 1, Employee and
          ProductionWorker Class Demo Program
*/

public class WorkerDemo
{
        public static void main(String[] args)
        {
                String shift;   // To hold a shift value

                // Create a ProductionWorker object and pass the initialization
                // data to the constructor.
                Employee pw =
                     new ProductionWorker("John Smith", "123-A", "11-15-2005",
                                          ProductionWorker.DAY_SHIFT, 16.50);

                // Display the data.
                System.out.println("Here's the first production worker.");

                System.out.println("Name: " + pw.getName());
                System.out.println("Employee number: " + pw.getEmployeeNumber());
                System.out.println("Hire Date: " + pw.getHireDate());
                System.out.println("Shift: " + pw.getShift());
                System.out.println("Pay Rate: " + pw.getPayRate());
               
                       
               
                System.out.println();

                // Make some changes:
                pw.setName("Jon Smyth");
                pw.setEmployeeNumber("123-B");
                pw.setHireDate("11-15-2004");
                System.out.println("After making some changes.");
                System.out.println("             Name: " + pw.getName());
                System.out.println(
                    " Employee Number: " + pw.getEmployeeNumber());
                System.out.println("        Hire Date: " + pw.getHireDate());
                System.out.println();

                // Create another ProductionWorker object and use the
                // set methods.
                ProductionWorker pw2 = new ProductionWorker();
                pw2.setName("Joan Jones");
                pw2.setEmployeeNumber("222-L");
                pw2.setHireDate("12-12-2005");
                pw2.setShift(ProductionWorker.NIGHT_SHIFT);
                pw2.setPayRate(18.50);

                // Display the data.
                System.out.println(" Here's the second production worker.");
                System.out.println("Name: " + pw2.getName());
                // System.out.println(pw2);
        }
}

Explanation / Answer

there are two ways to access sub class methods using super class object reference
1. By using method overriding(difine getShift() and getPayRate() in Employee class)
2. By using abstract class(make Employee class as abstract and declare getShift() and getPayRate() as abstract in Employee class )
/***************************************************
* Greg Roller
* Chapter 11= 1
* CISP II 1020 70
* Prof. Daniel Devers
****************************************************/
/***************************************************
* WorkerDemo.java(Way 1)
*
*
****************************************************/
package workerdemo;

import java.text.DecimalFormat;

class Employee {
   // Fields
   public String emplName, emplNumber, emplHireDate;

   public Employee() {
       String emplName = " ";
       String emplNumber = " ";
       String emplHireDate = " ";
   }

   public Employee(String Name, String Number, String HireDate) {

       setName(Name);
       setEmployeeNumber(Number);
       setHireDate(HireDate);
   }

   public void setName(String epName) {
       emplName = epName;
   }

   public void setEmployeeNumber(String epNum) {
       emplNumber = epNum;
   }

   public void setHireDate(String epHire) {
       emplHireDate = epHire;
   }

   public String getName() {
       return emplName;
   }

   public String getEmployeeNumber() {
       return emplNumber;
   }

   public String getHireDate() {
       return emplHireDate;
   }

   public int getShift() {
       return 0;
   }

   public double getPayRate() {
       return 0;
   }
}

class ProductionWorker extends Employee {
   private int shift;
   private double payRate;
   public static final int DAY_SHIFT = 1;
   public static final int NIGHT_SHIFT = 2;

   public ProductionWorker() {

       shift = DAY_SHIFT;
       payRate = 0.00;
   }

   public ProductionWorker(String Name, String number, String HireDate,
           int Shift, double HourlyRate) {
       super(Name, number, HireDate);
       setName(Name);
       setEmployeeNumber(number);
       setHireDate(HireDate);
       setShift(Shift);
       setPayRate(HourlyRate);
   }

   public final void setShift(int S) {
       shift = S;
   }

   public final void setPayRate(double R) {
       payRate = R;
   }

   public final int getShift() {
       return shift;
   }

   public final double getPayRate() {
       return payRate;
   }
}

/**
* Chapter 11 Programming Challenge 1, Employee and ProductionWorker Class Demo
* Program
*/

public class WorkerDemo {
   public static void main(String[] args) {
       String shift; // To hold a shift value
       // Create a ProductionWorker object and pass the initialization
       // data to the constructor.
       Employee pw = new ProductionWorker("John Smith", "123-A", "11-15-2005",
               ProductionWorker.DAY_SHIFT, 16.50);
       // Display the data.
       System.out.println("Here's the first production worker.");
       System.out.println("Name: " + pw.getName());
       System.out.println("Employee number: " + pw.getEmployeeNumber());
       System.out.println("Hire Date: " + pw.getHireDate());
       System.out.println("Shift: " + pw.getShift());
       System.out.println("Pay Rate: " + pw.getPayRate());

       System.out.println();
       // Make some changes:
       pw.setName("Jon Smyth");
       pw.setEmployeeNumber("123-B");
       pw.setHireDate("11-15-2004");
       System.out.println("After making some changes.");
       System.out.println(" Name: " + pw.getName());
       System.out.println(" Employee Number: " + pw.getEmployeeNumber());
       System.out.println(" Hire Date: " + pw.getHireDate());
       System.out.println();
       // Create another ProductionWorker object and use the
       // set methods.
       ProductionWorker pw2 = new ProductionWorker();
       pw2.setName("Joan Jones");
       pw2.setEmployeeNumber("222-L");
       pw2.setHireDate("12-12-2005");
       pw2.setShift(ProductionWorker.NIGHT_SHIFT);
       pw2.setPayRate(18.50);
       // Display the data.
       System.out.println(" Here's the second production worker.");
       System.out.println("Name: " + pw2.getName());
       // System.out.println(pw2);
   }
}


/***************************************************
* Greg Roller
* Chapter 11= 1
* CISP II 1020 70
* Prof. Daniel Devers
****************************************************/
/***************************************************
* WorkerDemo.java(Way 2)
*
*
****************************************************/
package workerdemo;

import java.text.DecimalFormat;

abstract class Employee {
   // Fields
   public String emplName, emplNumber, emplHireDate;

   public Employee() {
       String emplName = " ";
       String emplNumber = " ";
       String emplHireDate = " ";
   }

   public Employee(String Name, String Number, String HireDate) {

       setName(Name);
       setEmployeeNumber(Number);
       setHireDate(HireDate);
   }

   public void setName(String epName) {
       emplName = epName;
   }

   public void setEmployeeNumber(String epNum) {
       emplNumber = epNum;
   }

   public void setHireDate(String epHire) {
       emplHireDate = epHire;
   }

   public String getName() {
       return emplName;
   }

   public String getEmployeeNumber() {
       return emplNumber;
   }

   public String getHireDate() {
       return emplHireDate;
   }

   public abstract int getShift() ;

   public abstract double getPayRate() ;
}

class ProductionWorker extends Employee {
   private int shift;
   private double payRate;
   public static final int DAY_SHIFT = 1;
   public static final int NIGHT_SHIFT = 2;

   public ProductionWorker() {

       shift = DAY_SHIFT;
       payRate = 0.00;
   }

   public ProductionWorker(String Name, String number, String HireDate,
           int Shift, double HourlyRate) {
       super(Name, number, HireDate);
       setName(Name);
       setEmployeeNumber(number);
       setHireDate(HireDate);
       setShift(Shift);
       setPayRate(HourlyRate);
   }

   public final void setShift(int S) {
       shift = S;
   }

   public final void setPayRate(double R) {
       payRate = R;
   }

   public final int getShift() {
       return shift;
   }

   public final double getPayRate() {
       return payRate;
   }
}

/**
* Chapter 11 Programming Challenge 1, Employee and ProductionWorker Class Demo
* Program
*/

public class WorkerDemo {
   public static void main(String[] args) {
       String shift; // To hold a shift value
       // Create a ProductionWorker object and pass the initialization
       // data to the constructor.
       Employee pw = new ProductionWorker("John Smith", "123-A", "11-15-2005",
               ProductionWorker.DAY_SHIFT, 16.50);
       // Display the data.
       System.out.println("Here's the first production worker.");
       System.out.println("Name: " + pw.getName());
       System.out.println("Employee number: " + pw.getEmployeeNumber());
       System.out.println("Hire Date: " + pw.getHireDate());
       System.out.println("Shift: " + pw.getShift());
       System.out.println("Pay Rate: " + pw.getPayRate());

       System.out.println();
       // Make some changes:
       pw.setName("Jon Smyth");
       pw.setEmployeeNumber("123-B");
       pw.setHireDate("11-15-2004");
       System.out.println("After making some changes.");
       System.out.println(" Name: " + pw.getName());
       System.out.println(" Employee Number: " + pw.getEmployeeNumber());
       System.out.println(" Hire Date: " + pw.getHireDate());
       System.out.println();
       // Create another ProductionWorker object and use the
       // set methods.
       ProductionWorker pw2 = new ProductionWorker();
       pw2.setName("Joan Jones");
       pw2.setEmployeeNumber("222-L");
       pw2.setHireDate("12-12-2005");
       pw2.setShift(ProductionWorker.NIGHT_SHIFT);
       pw2.setPayRate(18.50);
       // Display the data.
       System.out.println(" Here's the second production worker.");
       System.out.println("Name: " + pw2.getName());
       // System.out.println(pw2);
   }
}

OUTPUT:
Here's the first production worker.
Name: John Smith
Employee number: 123-A
Hire Date: 11-15-2005
Shift: 1
Pay Rate: 16.5

After making some changes.
Name: Jon Smyth
Employee Number: 123-B
Hire Date: 11-15-2004


Here's the second production worker.
Name: Joan Jones

Note: please check the two solutions , please comment if you want any thing else

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote