Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
our company needs to increase salary of all employees with rate 10% who works for 2 years and add another 10% per year after 2 years of experience. program should display their salaries before and after calculate increasing rate

What I have tried:

i tried but didn't work with me anything

Java
public class Employee {
     
    private String firstName; 
    private String lastName; 
    private double monthlySalary;
 
    public Employee(String name, String name2, double salary) {
        firstName = name;
        lastName = name2;
        monthlySalary = salary;
    }
 
    public void setFirstName(String name) {
        firstName = name;
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setLastName(String name) {
        lastName = name;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setmonthlySalary(double salary) {
        monthlySalary = salary;
    }
 
    public double getmonthlySalary() {
        return monthlySalary;
    }
 
    public double yearlySalary() {
        double yearlySalary;
        yearlySalary = (monthlySalary * 12);
        return yearlySalary;
    }
 
    public double yearlySalaryIncrease() {
        double yearlySalaryIncrease;
        yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary()));
        return yearlySalaryIncrease;
    }
 
    public void displayYearlySalary() {
        System.out.printf("%s %s's Yearly Salary is $%.2f\n", firstName, lastName,
           yearlySalary());
    }
 
    public void displayYearlySalaryIncrease() {
        System.out.printf("%s %s = $%.2f\n", firstName, lastName, yearlySalaryIncrease());
    }
 
}





Java
import java.util.Scanner;
 
public class EmployeeTest {
    public static void main(String[] args) {
         
        Employee employee1 = new Employee("first", "last", 0.0);
        Employee employee2 = new Employee("first", "last", 0.0);
 
    Scanner input = new Scanner(System.in);
         
        String firstName;
        String lastName;
        double monthlySalary;
 
        System.out.println("Enter details of employee1:\n");
         
        System.out.print("Enter First Name: ");
        firstName = input.next();
        employee1.setFirstName(firstName);
        employee1.getFirstName();
        System.out.print("Enter Last Name: ");
        lastName = input.next();
        employee1.setLastName(lastName);
        employee1.getLastName();
        System.out.print("Enter Monthly Salary: ");
        monthlySalary = input.nextDouble();
        if (monthlySalary > 0) //if monthly salary is not positive do not set its value
            employee1.setmonthlySalary(monthlySalary);
        employee1.getmonthlySalary();
 
        System.out.println();
 
        System.out.println("Enter details of employee2:\n");
 
        System.out.print("Enter First Name: ");
        firstName = input.next();
        employee2.setFirstName(firstName);
        employee2.getFirstName();
        System.out.print("Enter Last Name: ");
        lastName = input.next();
        employee2.setLastName(lastName);
        employee2.getLastName();
        System.out.print("Enter Monthly Salary: ");
        monthlySalary = input.nextDouble();
        if (monthlySalary > 0) //if monthly salary is not positive do not set its value
            employee2.setmonthlySalary(monthlySalary);
        employee2.getmonthlySalary();
 
        System.out.println();
 
        employee1.yearlySalary();
        employee2.yearlySalary();
 
        employee1.displayYearlySalary();
 
        System.out.println();
 
        employee2.displayYearlySalary();
 
        System.out.println();
 
        employee1.yearlySalaryIncrease();
        employee2.yearlySalaryIncrease();
 
        System.out.printf("Congratulations to %s %s and %s %s. You just earned"
                + " for yourselves a 10%c increase in your yearly salaries. "
                + "\nYour new yearly salaries are:\n\n", employee1.getFirstName(),
                employee1.getLastName(), employee2.getFirstName(), 
                employee2.getLastName(), '%');
 
        employee1.displayYearlySalaryIncrease();
 
        System.out.println();
 
        employee2.displayYearlySalaryIncrease();
 
    }
}
Posted
Updated 25-Mar-18 0:43am
v3
Comments
Patrice T 24-Mar-18 19:40pm    
Show your code with sample input and output.
Member 13744927 24-Mar-18 20:22pm    
public class Employee {

private String firstName;
private String lastName;
private double monthlySalary;

public Employee(String name, String name2, double salary) {
firstName = name;
lastName = name2;
monthlySalary = salary;
}

public void setFirstName(String name) {
firstName = name;
}

public String getFirstName() {
return firstName;
}

public void setLastName(String name) {
lastName = name;
}

public String getLastName() {
return lastName;
}

public void setmonthlySalary(double salary) {
monthlySalary = salary;
}

public double getmonthlySalary() {
return monthlySalary;
}

public double yearlySalary() {
double yearlySalary;
yearlySalary = (monthlySalary * 12);
return yearlySalary;
}

public double yearlySalaryIncrease() {
double yearlySalaryIncrease;
yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary()));
return yearlySalaryIncrease;
}

public void displayYearlySalary() {
System.out.printf("%s %s's Yearly Salary is $%.2f\n", firstName, lastName,
yearlySalary());
}

public void displayYearlySalaryIncrease() {
System.out.printf("%s %s = $%.2f\n", firstName, lastName, yearlySalaryIncrease());
}

}




import java.util.Scanner;

public class EmployeeTest {
public static void main(String[] args) {

Employee employee1 = new Employee("first", "last", 0.0);
Employee employee2 = new Employee("first", "last", 0.0);

Scanner input = new Scanner(System.in);

String firstName;
String lastName;
double monthlySalary;

System.out.println("Enter details of employee1:\n");

System.out.print("Enter First Name: ");
firstName = input.next();
employee1.setFirstName(firstName);
employee1.getFirstName();
System.out.print("Enter Last Name: ");
lastName = input.next();
employee1.setLastName(lastName);
employee1.getLastName();
System.out.print("Enter Monthly Salary: ");
monthlySalary = input.nextDouble();
if (monthlySalary > 0) //if monthly salary is not positive do not set its value
employee1.setmonthlySalary(monthlySalary);
employee1.getmonthlySalary();

System.out.println();

System.out.println("Enter details of employee2:\n");

System.out.print("Enter First Name: ");
firstName = input.next();
employee2.setFirstName(firstName);
employee2.getFirstName();
System.out.print("Enter Last Name: ");
lastName = input.next();
employee2.setLastName(lastName);
employee2.getLastName();
System.out.print("Enter Monthly Salary: ");
monthlySalary = input.nextDouble();
if (monthlySalary > 0) //if monthly salary is not positive do not set its value
employee2.setmonthlySalary(monthlySalary);
employee2.getmonthlySalary();

System.out.println();

employee1.yearlySalary();
employee2.yearlySalary();

employee1.displayYearlySalary();

System.out.println();

employee2.displayYearlySalary();

System.out.println();

employee1.yearlySalaryIncrease();
employee2.yearlySalaryIncrease();

System.out.printf("Congratulations to %s %s and %s %s. You just earned"
+ " for yourselves a 10%c increase in your yearly salaries. "
+ "\nYour new yearly salaries are:\n\n", employee1.getFirstName(),
employee1.getLastName(), employee2.getFirstName(),
employee2.getLastName(), '%');

employee1.displayYearlySalaryIncrease();

System.out.println();

employee2.displayYearlySalaryIncrease();

}
}
Richard MacCutchan 25-Mar-18 3:34am    
And what is the problem? Apart from the fact that you should not use float/double types for financial calculations.
nv3 25-Mar-18 7:37am    
What do you mean by "java or C++"? Is it the one or the other? And besides, if you can't solve a problem like this by yourself, you probably don't deserve that 10% raise in salary :-)

1 solution

You should use better class design in coding by only do the operation of increasing the member salary. Than is the object updated to the correct value. Than the getter should return the correct value.
Java
public void yearlySalaryIncrease() {
  setmonthlySalary( monthlySalary * 1.1 );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900