0% found this document useful (0 votes)
60 views2 pages

Constructor

The Staff.java document defines a Staff class with name, age, salary, newSalary, and deduction attributes. It contains two constructors to initialize the attributes differently and methods to calculate new salary after deduction and display the staff data. The TestStaff.java document tests the Staff class by creating two Staff objects with different parameters, calling the getNewSalary() and displayData() methods to output the staff details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views2 pages

Constructor

The Staff.java document defines a Staff class with name, age, salary, newSalary, and deduction attributes. It contains two constructors to initialize the attributes differently and methods to calculate new salary after deduction and display the staff data. The TestStaff.java document tests the Staff class by creating two Staff objects with different parameters, calling the getNewSalary() and displayData() methods to output the staff details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Staff.

java
class Staff{
private String name;
private int age;
private double salary;
private double newSalary;
private double deduction;

Staff(String name1, int age1, double salary1){


name = name1;
age = age1;
salary = salary1;
deduction = 400;
}
Staff(int age1, double deduction1)
{
name = "Ah Chong";
age = age1;
salary = 1876.75;
deduction = deduction1;
}

public void getNewSalary()


{
newSalary = salary-deduction;
}

public void displayData(){


[Link]("Name\t\t: "+name);
[Link]("Age\t\t: "+age);
[Link]("Salary \t\t: RM"+salary);

[Link]("Deduction\t: RM"+deduction);
[Link]("New salary\t: RM"+newSalary);
[Link]("\n");
}
}

[Link]
public class TestStaf
{
public static void main(String args[]){
Staff s1 = new Staff("Afiz bin Hamzah",45,5690.20);
Staff s2 = new Staff(23,150);
[Link]();
[Link]();
[Link]();
[Link]();
}
}

You might also like