Question: Create a class called Employee that includes three instance variables — a first name (type String), a last name (type String) and a monthly salary (double). Provide 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, do not set its value. 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.
To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case Employee.java and EmployeeTest.java). Then compile both classes and run EmployeeTest. Employee will not run because it does not have a main method.
To compile both classes at the same time using the command prompt, use the command
javac Employee.java EmployeeTest.java
To run EmployeeTest.java, use the command
java EmployeeTest.java
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 3.14 - Employee Class<br /> * This Program Calculates The Yearly Salary Of Employees<br /> *<br /> */ <br /><br />public class Employee {<br /> <br /> private String firstName; <br /> private String lastName; <br /> private double monthlySalary;<br /><br /> public Employee(String name, String name2, double salary) {<br /> firstName = name;<br /> lastName = name2;<br /> monthlySalary = salary;<br /> }<br /><br /> public void setFirstName(String name) {<br /> firstName = name;<br /> }<br /><br /> public String getFirstName() {<br /> return firstName;<br /> }<br /><br /> public void setLastName(String name) {<br /> lastName = name;<br /> }<br /><br /> public String getLastName() {<br /> return lastName;<br /> }<br /><br /> public void setmonthlySalary(double salary) {<br /> monthlySalary = salary;<br /> }<br /><br /> public double getmonthlySalary() {<br /> return monthlySalary;<br /> }<br /><br /> public double yearlySalary() {<br /> double yearlySalary;<br /> yearlySalary = (monthlySalary * 12);<br /> return yearlySalary;<br /> }<br /><br /> public double yearlySalaryIncrease() {<br /> double yearlySalaryIncrease;<br /> yearlySalaryIncrease = (((yearlySalary() * (0.1)) + yearlySalary()));<br /> return yearlySalaryIncrease;<br /> }<br /><br /> public void displayYearlySalary() {<br /> System.out.printf("%s %s's Yearly Salary is $%.2fn", firstName, lastName,<br /> yearlySalary());<br /> }<br /><br /> public void displayYearlySalaryIncrease() {<br /> System.out.printf("%s %s = $%.2fn", firstName, lastName, yearlySalaryIncrease());<br /> }<br /><br />}<br /> |
Below is Class EmployeeTest.java to test Class Employee.java
1 2 |
//Exercise 3.14 - Employee Class Test<br />//This Program Demonstrates Class Employee Capabilities<br /><br />import java.util.Scanner;<br /><br />public class EmployeeTest {<br /> public static void main(String[] args) {<br /> <br /> Employee employee1 = new Employee("first", "last", 0.0);<br /> Employee employee2 = new Employee("first", "last", 0.0);<br /><br /> Scanner input = new Scanner(System.in);<br /> <br /> String firstName;<br /> String lastName;<br /> double monthlySalary;<br /><br /> System.out.println("Enter details of employee1:n");<br /> <br /> System.out.print("Enter First Name: ");<br /> firstName = input.next();<br /> employee1.setFirstName(firstName);<br /> employee1.getFirstName();<br /> System.out.print("Enter Last Name: ");<br /> lastName = input.next();<br /> employee1.setLastName(lastName);<br /> employee1.getLastName();<br /> System.out.print("Enter Monthly Salary: ");<br /> monthlySalary = input.nextDouble();<br /> if (monthlySalary > 0) //if monthly salary is not positive do not set its value<br /> employee1.setmonthlySalary(monthlySalary);<br /> employee1.getmonthlySalary();<br /><br /> System.out.println();<br /><br /> System.out.println("Enter details of employee2:n");<br /><br /> System.out.print("Enter First Name: ");<br /> firstName = input.next();<br /> employee2.setFirstName(firstName);<br /> employee2.getFirstName();<br /> System.out.print("Enter Last Name: ");<br /> lastName = input.next();<br /> employee2.setLastName(lastName);<br /> employee2.getLastName();<br /> System.out.print("Enter Monthly Salary: ");<br /> monthlySalary = input.nextDouble();<br /> if (monthlySalary > 0) //if monthly salary is not positive do not set its value<br /> employee2.setmonthlySalary(monthlySalary);<br /> employee2.getmonthlySalary();<br /><br /> System.out.println();<br /><br /> employee1.yearlySalary();<br /> employee2.yearlySalary();<br /><br /> employee1.displayYearlySalary();<br /><br /> System.out.println();<br /><br /> employee2.displayYearlySalary();<br /><br /> System.out.println();<br /><br /> employee1.yearlySalaryIncrease();<br /> employee2.yearlySalaryIncrease();<br /><br /> System.out.printf("Congratulations to %s %s and %s %s. You just earned"<br /> + " for yourselves a 10%c increase in your yearly salaries. "<br /> + "nYour new yearly salaries are:nn", employee1.getFirstName(),<br /> employee1.getLastName(), employee2.getFirstName(), <br /> employee2.getLastName(), '%');<br /><br /> employee1.displayYearlySalaryIncrease();<br /><br /> System.out.println();<br /><br /> employee2.displayYearlySalaryIncrease();<br /><br /> }<br />}<br /> |
10 Comments
printf doesnt work for me
wow i think it's a good job, i get new lesson from this post and this post can be inspiring for all, thakns for your post
give a java program to print name ,salary and calculate tax of an employee
import java.util.ArrayList; import java.util.Scanner;
public class Employee{
public static void main(String[] args) {
ArrayList obj = new ArrayList<>();
String empFirstName;
do {
Scanner input = new Scanner(System.in);
System.out.printf("Employee First Name :");
empFirstName = input.nextLine();
if (empFirstName.equals("johndoe")) {
break;
}
System.out.printf(("Employee Last Name :"));
String empLastName = input.nextLine();
System.out.printf("Enter current salary :$");
double empPayAmount = input.nextDouble();
System.out.printf("Enter hourly rate :$");
double hourRate = input.nextDouble();
System.out.printf("Enter number of hours :");
double numHours = input.nextDouble();
Employee objEmp = new Employee();
objEmp.setName(empFirstName, " " + empLastName);
objEmp.addPay(empPayAmount);
objEmp.addPay(numHours, hourRate);
obj.add(objEmp);
} while (true);
for (Employee obEmployee : obj) {
System.out.println("Total Pay for Employee " + obEmployee.getName() + " is " + obEmployee.getTotalPayAmount());
}
System.out.printf("Company employees are here : ", obj.size());
}
}
write a code in java that input salary and grade of an employee and apply below conditions:
i) in case of grade 15 or above than bonus is 15%
ii) in case of grade 16 or above than bonus is 20%
iii) in case of grade 18 or above than bonus is 25%
iv) after calculating total salary deduct 13% GST in case that salary is 15000 or above. deduct 15% GST and in case that salary is 22000 or above.
v) Add 6% bonus at the end
Calculate net salary according to above condition and display it.
Give me solution of this question I need emergency solution of this question. Thanks
Can you please tell me what's the point of having the parameterized constructor when we never really use the parameters. Since we get our arguments from the user.
Thanks 5faya, you are welcome. I'm glad you got a lot of ideas studying my code. If you don't mind, please share it with us.
thanks A lot , i just got a lot of ideas when i was studying your code
you're the best
Keep on :<3
I'm glad you learnt something from the code. You are welcome.
Very much thanks to you! Saved me a bit of time and I learned something by reading your code!