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);
}
}
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);
}
}
please give a full coding
ReplyDeletemy 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
ReplyDeleteimport 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);
}
}
}
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.
Deleteimport 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);
}
}