Hello everyone,

I have a reall small questions and i am stuck in the end of my code. I need just someone explane it to me please or maybe i am confused. Take a look...

Questions:
Create a class called "Employee" that includes three pieces of information as instance variables: a first name, last name, and monthly salary. Your class should have a constructor that initializes the three instance variables. Provide a set and get method for each instance variable. If the monthly salary is not positive, set it to zero.
Write a test application, aka client code, named UseEmployee that demonstrate the class you just created. 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.
The Employee Class: add another method to the employee class, named adjustSalary that would accept a percentage value and adjusts the salary of a given employee object accordingly. Make sure to go to the employee class and make use of this method as follows: increase emp1's salary by 10% and decrease emp2's salary by 5%.

I did all what he asked me but i stouck in the 5% !!! If i divided it; it will be the same as multibliy so how i can make the second employee have decrease 5%

There is my code

public class Employee {
    // Instance Variables 
    private String firstName; // instance variable that stores the first name
    private String lastName; // instance variable that stores the last name
    private double monthlySalary; // instance variable

    // Default Constructor
    public Employee(){
        this.firstName = "";
        this.lastName = "";
        this.monthlySalary = 0.0;
    }
    // non\default Constructor
    public Employee (String firstName, String lastName, double monthlySalary){
        this.firstName = firstName;
        this.lastName = lastName;
        this.monthlySalary = monthlySalary;

        // if the monthly salary is not positive, set it to 0.0.
        if (monthlySalary < 0)
            monthlySalary = 0.0;
    } 

    public void setFirstName (String firstName)
    {
        this.firstName = firstName; 

    } 
    public String getFirstName (){

        return firstName;
    } 
    public void setLastName (String lastName){

        this.lastName = lastName;
    } 

    public String getLastName ()
    {
        return lastName;
    } 
    public void setMonthlySalary (double monthlySalary){

        this.monthlySalary = monthlySalary;
    } 

    public double getMonthlySalary ()
    {
        return monthlySalary;
    } 

    public void adjustSalary(double percentage){
        this.monthlySalary = (this.monthlySalary * percentage) + this.monthlySalary;
    }
    public void adjSalary(double percentage){
            this.monthlySalary = (this.monthlySalary / percentage) + this.monthlySalary;
    }
    public String toString(){
        String output = "";
        output += "\nEmployee Information: ";
        output += "\nEmployee firsName: " + this.firstName;
        output += "\nEmployee lastName: " + this.lastName;
        output += "\nEmployee monthlySalary: " + this.monthlySalary;

        return output;
    }

    } 

public class UseEmployee {

    public static void main(String[] args) {
        Employee sameer = new Employee (" Sameer ", " Karali ", 5000);
        System.out.println(sameer);
        sameer.adjustSalary(0.10);

        System.out.println(sameer);

        Employee nithan = new Employee ( " Nithan ", " Smith ", 6000);
        System.out.println(nithan);
        nithan.adjSalary(0.05);

    } 

}

For

public void adjustSalary(double percentage)

how do you specify the percentage? If you are giving percentage as a number from 0-100 then you have to use the calculation

this.monthySalary = this.monthlySalary + (percentage / 100.0);

If you are specifying 5% instead as 0.05 then you don't have to divide by 100.

Just remember to handle negative results by setting to zero.

A decrease is just a negative increase.
So a decrease of 5% is the same as adjSalary(-0.05);

Oh thank you the adjSalary(-0.05) worked well thank you all.

Ok if i want to add another method that called ""calculateTravelAssistance()"" so it will calculate the
amount of money an employee gets as travel assistance and adds this amount to the Employee salary. Assume that the company employs people in the following three counties:
if the county is Howard county then monthly travel assistance will be $50
If the county is Marion then travel assist will be $150
If the county is Hamilton then travel assist will be $150
Return to class useEmployee class and set the travel assistance for the employee objects you created. Display
the new employee record after the updates. make sure to also update the toString method so the travel assistance value is displayed as well.

Do i need to use if statment or you think switch will work better!!??

With three options it's not clear. For two it'sif, for 4+ it's switch. But in real life the number of choices always increases over time, so in my opinion the switch will become a better and better choice.
But really I would go for a map (Map<County, TravelAssist>) approach because that scales best of all, and can be based on a data/config file for when more counties are added.

I did the " If statment " but i am not sure if it's corect or not and i don't know how to fix the UseEmployee class (:

That's what i have added to my code

public String getCounty(){
            return county; 
        }
        public void setCounty(String county){
            this.county = county;
        }
        public void calculateTravelAssistance(String county){
            int howard = 50;
            int marion = 150;
            int hamilton = 150;
            String monthlyTravel;
        if ( howard == 50){
            monthlyTravel = " 50 ";
        } else if ( marion == 150 ) { 
            monthlyTravel = " 150 ";
        }else if ( hamilton == 150 ) {
            monthlyTravel = " 150 ";
        } else
            monthlyTravel = " 0 ";
        }
        public String toString(){
            String output = "";
            output += "\nEmployee Information: ";
            output += "\nEmployee firsName: " + this.firstName;
            output += "\nEmployee lastName: " + this.lastName;
            output += "\nEmployee monthlySalary: " + this.monthlySalary;
            output += " \nEmployee county: " + this.county;

            return output;
        }

        } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.