Choose an object for which you would like to write a program. Use a UML diagram
ID: 3791853 • Letter: C
Question
Choose an object for which you would like to write a program. Use a UML diagram to design a class for the object. Your class will have instance variables (properties) and methods. There will be 3 Instance variables. One instance variable will be of type String, one of type int, and another of type double. Write getters and setters (accessors and matadors) for each of the instance variables. Your program should also include a default constructor and a constructor that includes parameters. Both constructors will initialize each of the instance variables. Create another class that includes main which tests the methods of the class. To do this you will create/instantiate 2 objects. The first object will be created/instantiated using the default constructor and the second using the constructor with parameters. After each of the objects is created, use get methods to return and print the values stored in each object. Next, use the set methods to change the values stored in the second object. Then use get methods to return and print the values stored in the object. Be sure to include an algorithm for main. Add Javadoc comments to the class.Explanation / Answer
Please find the required solution:
public class Employee {
private int id;
private String name;
private double salary;
/**
* Default constructor
*/
public Employee() {
this.id = 999;
this.name = "defemp";
this.salary = 10000.0d;
}
/**
*
* @param id
* @param name
* @param salary
*/
public Employee(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
/**
*
* @return id of employee
*/
public int getId() {
return id;
}
/**
*
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
*
* @return name of employee
*/
public String getName() {
return name;
}
/**
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return salary of employee
*/
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void main(String[] args) {
// create employee e1
Employee e1 = new Employee();
System.out.println("Employee 1 properties");
// test getters
System.out.println(e1.getName());
System.out.println(e1.getId());
System.out.println(e1.getSalary());
// create employee e2
Employee e2 = new Employee(10, "raghu", 20000.0d);
System.out.println("Employee 2 properties");
// test getters
System.out.println(e2.getName());
System.out.println(e2.getId());
System.out.println(e2.getSalary());
// test getters
e2.setName("Raghunath");
e2.setSalary(30000.0d);
System.out.println("Employee 2 properties after changing");
// recheck getters
System.out.println(e2.getName());
System.out.println(e2.getId());
System.out.println(e2.getSalary());
}
}
Sample output:
Employee 1 properties
defemp
999
10000.0
Employee 2 properties
raghu
10
20000.0
Employee 2 properties after changing
Raghunath
10
30000.0