Skip to main content
A program calculates and displays bonus amounts to pay various types of employees.
There are 3 separate departments, numbered 1, 2, and 3. Department 1 employees are
paid a bonus based on their sales: If their sales amount is over $5000 they get 5% of those
sales, otherwise they get nothing. Department 2 employees are paid a bonus based on the
number of units they sell: They get $100 per unit sold, and an extra $50 per unit if they
sell more than 25 units; if they sell no units, they get nothing. Department 3 employees
assemble parts in the plant and are paid a bonus of 10 cents per part if they reach a certain
level: Part-time employees must assemble more than 250 parts to get the 10-cent-per-part
bonus, and full-time employees must assemble more than 700.
Write a set of 3 overloaded methods called getBonus() that works with the program
below, according to the specifications described above.

public class Calculate
{
    public static void main(String [] args)
    {
        Employee emp1 = new Employee();
        Employee emp2 = new Employee();
        Employee emp3 = new Employee();
        emp1.getbonus(6000.0);       
        emp1.display();   
        //System.out.println("bonus of the employee of first dept. "+emp1.display());
        emp2.getbonus(10);       
        emp2.display();   
        //System.out.println("bonus of the employee of second dept. "+emp2.display());
        emp3.getbonus(240,1);   
        emp3.display();       
        //System.out.println("bonus of the employee of third dept. "+emp3.display());


    }
}

class Employee
{
    double amount=0;
    int units=0;
    int parts=0;
    double bonus=0;
   
   
    public void getbonus(double amount)
    {
        if(amount>5000)
        {
            bonus=amount*0.05;
        }
        else
        {
            bonus=0;
        }
    }
    public void getbonus(int units)
    {
        if(units>0&&units<25)
        {
            bonus=units*100;
        }
        else if(units>=25)
        {
            bonus=units*150;
        }
        else
        {
            bonus=0;
        }
    }
    public void getbonus(int parts,int type)
    {
        if(type==1)
        {
            if(parts>250)
            {
                bonus=parts*0.1;
            }
            else
            {
                bonus=0;
            }
        }
        else
        {
            if(parts>700)
            {
                bonus=parts*0.1;
            }
            else
            {
                bonus=0;
            }
        }
    }
    public void display()
    {
        System.out.println("Bonus="+bonus);
    }
}



Comments

  1. my lecturer give a question that totally same with this question but ..... i have to be continued that coding java was given..please check my coding.. whats wrong my coding and please tell me




    import java.util.Scanner;

    public class employees
    {
    int dept;
    double sales;
    double bonus;
    int numUnits;
    int pieces;
    int empType;
    int bonusLimit;

    public void getBonus(double sales)
    {
    if(sales > 5000) {
    bonus = sales * 0.05;
    }
    else {
    bonus = 0.0;
    }
    }
    public void getBonus(int numUnits)
    {
    if(numUnits > 0 && numUnits < 25) {
    bonus = numUnits * 100;
    }
    else if(numUnits >= 25) {
    bonus = numUnits * 150;
    }
    else {
    bonus = 0.0;
    }

    }
    public void getBonus(int pieces, int empType, int bonusLimit)
    {
    if(empType == 1)
    {
    if(pieces > 250)
    {
    bonus = pieces * 0.1;
    }
    else
    {
    bonus=0;
    }
    }
    else
    {
    if(pieces > 700)
    {
    bonus = pieces * 0.1;
    }
    else
    {
    bonus=0;
    }
    }
    }
    class employeesTest
    {

    final static int UNITS_PT = 250;
    final static int UNITS_FT = 700;

    public static void main(String []args)
    {
    Scanner keysIn = new Scanner(System.in);
    System.out.println("Enter department: ");
    int dept = keysIn.nextInt();
    double bonus = 0;
    switch(dept)
    {
    case 1:
    System.out.print("Enter sales: ");
    double sales = keysIn.nextDouble();
    bonus = getBonus(sales);
    break;
    case 2:
    System.out.print("Enter number of units sold: ");
    int numUnits = keysIn.nextInt();
    bonus = getBonus(numUnits);
    break;
    case 3:
    System.out.print("Enter # of pieces completed: ");
    int pieces = keysIn.nextInt();
    System.out.print("Full-time (1) or Part-time (2)? ");
    int empType = keysIn.nextInt();
    int bonusLimit = (empType == 1) ? UNITS_FT : UNITS_PT;
    bonus = getBonus(pieces, bonusLimit);
    break;
    default:
    System.out.print("Error! ");
    }
    System.out.println("Bonus Amount: " + bonus);
    }
    }
    }














    ReplyDelete
    Replies
    1. There were multiple errors. On the basis of information you provided, I have corrected your code. You have not created any object nor any constructor. Default constructor will work by the way. In the methods you created you have not declared any return types. You have nested the class.(I think you should keep it simple.) The method you declared for the third case has 3 parameters, but you passed only two when you called. The code you provided below is working now. Look for any logical error yourself.

      import java.util.Scanner;

      class Employees
      {
      int dept;
      double sales;
      double bonus;
      int numUnits;
      int pieces;
      int empType;
      int bonusLimit;

      double getBonus(double sales){
      if(sales > 5000) {
      bonus = sales * 0.05;
      }
      else {
      bonus = 0.0;
      }
      return bonus;
      }
      double getBonus(int numUnits)
      {
      if(numUnits > 0 && numUnits < 25) {
      bonus = numUnits * 100;
      }
      else if(numUnits >= 25) {
      bonus = numUnits * 150;
      }
      else {
      bonus = 0.0;
      }
      return bonus;
      }
      double getBonus(int pieces, int empType, int bonusLimit){
      if(empType == 1){
      if(pieces > 250){
      bonus = pieces * 0.1;
      }
      else{
      bonus=0;
      }
      }
      else{
      if(pieces > 700){
      bonus = pieces * 0.1;
      }
      else{
      bonus=0;
      }
      }
      return bonus;
      }
      }

      class EmployeesTest
      {
      final static int UNITS_PT = 250;
      final static int UNITS_FT = 700;

      public static void main(String []args)
      {
      Employees emp1 = new Employees();
      Scanner keysIn = new Scanner(System.in);
      System.out.println("Enter department: ");
      int dept = keysIn.nextInt();
      double bonus = 0;
      switch(dept){
      case 1:
      System.out.print("Enter sales: ");
      double sales = keysIn.nextDouble();
      bonus = emp1.getBonus(sales);
      break;
      case 2:
      System.out.print("Enter number of units sold: ");
      int numUnits = keysIn.nextInt();
      bonus = emp1.getBonus(numUnits);
      break;
      case 3:
      System.out.print("Enter # of pieces completed: ");
      int pieces = keysIn.nextInt();
      System.out.print("Full-time (1) or Part-time (2)? ");
      int empType = keysIn.nextInt();
      int bonusLimit = (empType == 1) ? UNITS_FT : UNITS_PT;
      bonus = emp1.getBonus(pieces,empType, bonusLimit);
      break;
      default:
      System.out.print("Error! ");
      }
      System.out.println("Bonus Amount: " + bonus);
      }
      }

      Delete

Post a Comment

Popular posts from this blog

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities.

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities. import java.io.*; class Book {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String book_name;     int isbn;     String author;     String publisher; ...

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities. import java.io.*; class Invoice {     BufferedReader br = new BufferedReader(new InputStreamReader(System.i...

Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again

Create a class called Employee that includes three pieces of information as instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again . import java.io.*; class Employee {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String f_name;     String l_name;     int sal;     Employee()     {         f_name="sarvajeet";     ...