Question

In: Computer Science

Write a Java class called Employee (Parts of the code is given below), which has three...

Write a Java class called Employee (Parts of the code is given below), which has three private
fields firstName (String), lastName (String) and yearsEmployed (double). Implement
accessor/mutator methods for the private fields and toString() method, that returns a String
representation, which contains employee name and years she was employed.
public class Employee {
private String firstName;
...
}
Write a client program called EmployeeClient that creates an instance of Employee class, sets
values for the fields (name: John Doe, yearsEmployed: 4.5), and prints out the information about
an object.
Please note that you need to create 2 source files – one for the class implementation
(Employee.java) and one for the client code (EmployeeClient.java).

Solutions

Expert Solution

EmployeeClient.java


public class EmployeeClient {

   public static void main(String[] args) {
       Employee emp = new Employee();
       emp.setFirstName("John");
       emp.setLastName("Doe");
       emp.setYearsEmployed(4.5);
       System.out.println(emp);
   }

}

Employee.java


public class Employee {
   private String firstName , lastName ;
   private double yearsEmployed ;
   public Employee() {
      
   }
   public String getFirstName() {
       return firstName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }
   public String getLastName() {
       return lastName;
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
   public double getYearsEmployed() {
       return yearsEmployed;
   }
   public void setYearsEmployed(double yearsEmployed) {
       this.yearsEmployed = yearsEmployed;
   }
   public String toString() {
       return "Name: "+firstName+" "+lastName+", yearsEmployed: "+yearsEmployed;
   }
}

Output:

Name: John Doe, yearsEmployed: 4.5


Related Solutions

Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, 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 app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T>...
Please provide the missng code(parts) for the given code in java. ASAP. import java.util.Stack; class StackQueue<T> implements Queue<T>{ private Stack<T> inStack; private Stack<T> outStack; private int size; public StackQueue(){ inStack = new Stack<T> (); outStack = PART(a); size = 0; } public int size(){ return size; } public boolean isEmpty(){ return size==0; } public T first(){ if (size == 0) return null;; if (outStack.empty()) while (!inStack.empty()) outStack.push(inStack.pop()); return PART(b); //return the element at the top of the outStack without removing...
Exercise: Three UML class diagrams are given below. Class Employee is the superclass and the other...
Exercise: Three UML class diagrams are given below. Class Employee is the superclass and the other two classes, Manager and Programmer, are subclasses of Employee. Employee - name: String # salary: double + Employee( ) + setEmployee( String, double ): void + getName( ): String + getSalary( ): double + print( ): void Manager Programmer - numEmps: int - language: String + Manager( ) + setManager( String, double, int ): void + getNumEmps( ): int + getSalary( ): double +...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT