Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:16:49+00:00 2026-06-13T04:16:49+00:00

I have programed a Worker and MachineWorker class. It complies fine. but when i

  • 0

I have programed a Worker and MachineWorker class. It complies fine. but when i run it, after three consecutive statements the program stops. I cannot find the problem, and I dont know how and when to use ‘instanceof’.
I am writing the question below and with all the classes…

Question:- (i)Declare an array that can store the references of up to 5 Worker or MachineWorker objects.
a)Allow user to enter the type of object and the data for that type of object(3 values for Worker and 4 values for MachineWorker).
b)Construct the appropriate object storing the reference in the common array.

ii)Now allow the users to enter the weekly data repeatedly. If it is Worker object user must enter ID and hours-worked. If it is a MachineWorker object user must enter ID,hoursWorked and pieces. Once these values read search through the array to locate the object with the given ID before calling the addWeekly().The number of arguments either(1 or 2) to be passed to addWeekly depends on the type of objects being referred. To determine the type of object being referred(Worker or MachineWorker)you may use the instanceof operator.

Please see my codes below:-

 //Worker.java
    public class Worker { 
            public final double bonus=100;    
            protected String name, workerID;
            protected double hourlyRate, totalHoursWorked,tax,grossSalary,netSalary;

            public Worker(){

        }
    public Worker(String name, String workerID, double hourlyRate){
        this.name = name;
        this.workerID = workerID;
        this.hourlyRate = hourlyRate;

        }

    public void addWeekly(double hoursWorked){
        this.totalHoursWorked = this.totalHoursWorked + hoursWorked;
        }

    public double gross(){
        grossSalary = (totalHoursWorked*hourlyRate);
                if(totalHoursWorked>=150){
            grossSalary = grossSalary +100;
            }
            return  grossSalary;
            }
    public double netAndTax(){
        netSalary = grossSalary;
        if(grossSalary>500){
            tax = (grossSalary - 500) *0.3;
            netSalary = (grossSalary - tax);

        }
        return netSalary;
     }
    public String getName(){
        return this.name;
    }

    public String getWorkerID(){
        return this.workerID;
    }

    public double getHourlyRate(){
        return this.hourlyRate;
    }

    public double getTotalHours(){
        return totalHoursWorked;
    }

    public double getGrossSalary(){
        return grossSalary;
        }

    public void addToGross(double amt){
        grossSalary = grossSalary + amt;
    }
    public void displaySalary(){
        System.out.print("Name: " +getName() + "\nID :" + getWorkerID() 
                + "\nHourly Rate: " + getHourlyRate()+ "\nTotalHours Worked" + getTotalHours() + 
                "\nGross pay" + getGrossSalary() + "\nTax: " + netAndTax() + 
                "\nNet Pay: " + netAndTax());
    }

    }




//MachineWorker.java
    public class MachineWorker extends Worker{

        private double targetAmount;
        private double totalPieces, productivityBonus;

        public MachineWorker(String workerName, String workerID, double hourlyRate, double targetAmount)
        {
            super(workerName, workerID, hourlyRate);
            //this.productivityBonus = productivityBonus;
            this.targetAmount = targetAmount;

        }

        public void addWeekly(double hoursWorked, double weeklyAmount)
        {
            totalHoursWorked = hoursWorked + totalHoursWorked;
            totalPieces = weeklyAmount + totalPieces;
        }


        public double productivityBonus()
        {
            productivityBonus = 100 + (totalPieces - targetAmount);
            return productivityBonus;
        }

        public double gross()
        {
            grossSalary = (totalHoursWorked * hourlyRate) + productivityBonus;
            if(totalHoursWorked >= 150)
            {
                grossSalary = grossSalary + bonus;
            }
            return  grossSalary;
        }

        public void addToGross(double amt)
        {
            amt = productivityBonus;
            grossSalary = grossSalary + amt;
        }


        public void displaySalary()
        {

        System.out.println("Name    " + super.name + "\nID  " + 
        super.workerID + "\nHourly rate " + super.hourlyRate + "\nTotal Hours Worked    " + 
        super.totalHoursWorked + "\nGross Pay   $" + super.grossSalary + "\nTax $" + super.tax + "\nNetpay  $" + super.netSalary);
            System.out.println("Productivity Bonus  " + productivityBonus);
        }   
    }





  //Polymorphism PolyWorker.java

import java.util.*;
        public class PolyWorkers
    {
        public static void main(String args[])
        {
            Scanner input = new Scanner(System.in);

            Worker[] a = new Worker[5];
            MachineWorker[] b = new MachineWorker[5];

            char option = '0';
            String choice;
            boolean nChar = false;

            for (int i = 0; i < 5; i++){

                System.out.print("\tType of object " + (i+1) + " [W/M]: ");
                choice = input.nextLine();

                if (choice.length() == 1)
                {
                    option = choice.charAt(0); //pick the first character

                    if (option == 'w' || option == 'W')
                    {
                        System.out.println("\n\tEnter name, ID and hours:   ");
                        String name = input.nextLine();
                        System.out.print("  ");
                        String id = input.nextLine();
                        System.out.print("  ");
                        double hours = input.nextDouble();

                        a[i] = new Worker(name, id, hours);
                        System.out.println();

                    }
                    if (option == 'm' || option == 'M')
                    {
                        System.out.print("\n\tEnter name, ID, hours and pieces: ");
                        String name = input.nextLine();
                        System.out.print("  ");
                        String id = input.nextLine();
                        System.out.print("  ");
                        double hours = input.nextDouble();
                        System.out.print("  ");
                        double pieces = input.nextDouble();

                        b[i] = new MachineWorker(name, id, hours, pieces);
                        System.out.println();
                    }
                System.out.print("\tType of object " + (i+1) + " [W/M]: ");
                choice = input.nextLine();
                }


                a[i].displaySalary();
                b[i].displaySalary();
                b[i].productivityBonus();

            }

        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:16:52+00:00Added an answer on June 13, 2026 at 4:16 am

    You might want to use overriden methods readfromInput and displaySalary to distinguish between what Worker and Machinworker does.
    The different behaviour should be implemented within the classes and not in the calling Polyworker class.
    If Machineworker displaySalary shows the bonus this should be called in displaySalary of MachineWorker

    see modified code below

    //Worker.java
    import java.util.Scanner;
    
    /**
     * a generic worker
     */
    public class Worker {
        public final double bonus = 100;
        protected String name, workerID;
        protected double hourlyRate, totalHoursWorked, tax, grossSalary, netSalary;
    
    
        public void addWeekly(double hoursWorked) {
            this.totalHoursWorked = this.totalHoursWorked + hoursWorked;
        }
    
        public double gross() {
            grossSalary = (totalHoursWorked * hourlyRate);
            if (totalHoursWorked >= 150) {
                grossSalary = grossSalary + 100;
            }
            return grossSalary;
        }
    
        public double netAndTax() {
            netSalary = grossSalary;
            if (grossSalary > 500) {
                tax = (grossSalary - 500) * 0.3;
                netSalary = (grossSalary - tax);
    
            }
            return netSalary;
        }
    
        public String getName() {
            return this.name;
        }
    
        public String getWorkerID() {
            return this.workerID;
        }
    
        public double getHourlyRate() {
            return this.hourlyRate;
        }
    
        public double getTotalHours() {
            return totalHoursWorked;
        }
    
        public double getGrossSalary() {
            return grossSalary;
        }
    
        public void addToGross(double amt) {
            grossSalary = grossSalary + amt;
        }
    
        public void displaySalary() {
            System.out.print("Name: " + getName() + "\nID :" + getWorkerID()
                    + "\nHourly Rate: " + getHourlyRate() + "\nTotalHours Worked"
                    + getTotalHours() + "\nGross pay" + getGrossSalary() + "\nTax: "
                    + netAndTax() + "\nNet Pay: " + netAndTax());
        }
    
        public void readFromInput(Scanner input) {
            name = input.nextLine();
            System.out.print("  ");
            this.workerID= input.nextLine();
            System.out.print("  ");
            this.totalHoursWorked = input.nextDouble();
            System.out.println();
        }
    
    } // Worker
    
    //MachineWorker.java
    import java.util.Scanner;
    public class MachineWorker extends Worker {
    
        private double targetAmount;
        private double totalPieces, productivityBonus;
    
        public void addWeekly(double hoursWorked, double weeklyAmount) {
            totalHoursWorked = hoursWorked + totalHoursWorked;
            totalPieces = weeklyAmount + totalPieces;
        }
    
        public double productivityBonus() {
            productivityBonus = 100 + (totalPieces - targetAmount);
            return productivityBonus;
        }
    
        public double gross() {
            grossSalary = (totalHoursWorked * hourlyRate) + productivityBonus;
            if (totalHoursWorked >= 150) {
                grossSalary = grossSalary + bonus;
            }
            return grossSalary;
        }
    
        public void addToGross(double amt) {
            amt = productivityBonus;
            grossSalary = grossSalary + amt;
        }
    
        @Override
        public void displaySalary() {
            super.displaySalary();
            System.out.println("Productivity Bonus  " + productivityBonus);
        }
    
        @Override
        public void readFromInput(Scanner input) {
            super.readFromInput(input);
            this.totalPieces = input.nextDouble();
        }
    }
    
    //Polymorphism PolyWorker.java
    
    import java.util.*;
    
    public class PolyWorkers {
        public static void main(String args[]) {
            Scanner input = new Scanner(System.in);
            Worker[] workers = new Worker[5];
    
            char option = '0';
            String choice;
    
            for (int i = 0; i < 5; i++) {
    
                System.out.print("\tType of object " + (i + 1) + " [W/M]: ");
                choice = input.nextLine();
    
                if (choice.length() == 1) {
                    option = choice.toLowerCase().charAt(0); // pick the first character
                    switch (option) {
                    case 'w': {
                        workers[i] = new Worker();
                        System.out.println("\n\tEnter name, ID and hours:   ");
                    }
                        break;
                    case 'm': {
                        System.out.print("\n\tEnter name, ID, hours and pieces: ");
                    }
                        break;
                    } // switch
                    workers[i].readFromInput(input);
                }
    
                workers[i].displaySalary();
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run this program before and it worked fine. Then I added the
I have this simple program that computes salaries for four different worker types. It's
I have a program with several worker threads, and a main thread that receives
I have programmed a Worker class and the driverclass for Worker.. My working class
My program have several worker threads that calling a function in a dynamically loaded
I have class named worker , I want to create new instance of this
I have a program that spawns 3 worker threads that do some number crunching,
I have a program that runs fine on MacOS and Linux and cross-compiles to
I have a program who run as zombie process. I want to debug it.
Is it possible to have a task automatically run after the WebEngine finishes loading?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.