Finally write an appropriate tostring method that prints


Write a class called Employee that has the following fields:

? name - the name field references a String object that holds the employee's name.

? idNumber - the idNumber is an int variable that holds the employee's ID number.

? department - the department field references a String object that holds the name of the department where the employee works.

? position - the position field references a String object that holds the employee's job title.

The class should have the following constructors:

o A constructor that accepts the following values as arguments and assigns them to the appropriate fields: employee's name, employee's ID number, department, and position.

o A no-argument constructor that assigns empty strings ("") to the name, department, and position fields and 0 to the idNumber field.

Also write appropriate accessor and mutator methods for each field.

Finally, write an appropriate toString() method that prints the state of the object. Example: Name: Mark Jones ID Number: 39119 Department: IT Position: Programmer

 

/*

 

For this program, you should write:

- A constructor that initializes the fields

1) name

2) ID

3) department

4) position

- A no argument constructor

- Mutator methods for each field

 

*/

import java.util.*;

 

public class Lab12Driver

{

public static void main(String [] args)

{

Scanner inScan=new Scanner(System.in);

// Reads the name

System.out.println("Enter employee's Name: ");

String name=inScan.nextLine();

// Reads the ID Number

System.out.println("Enter employee's ID Number: ");

int ID=inScan.nextInt();

// Om nom nom

// Flush the buffer

inScan.nextLine();

// Reads the department

System.out.println("Enter employee's department: ");

String dept=inScan.nextLine();

// Reads the position

System.out.println("Enter employee's position: ");

String pos=inScan.nextLine();

// Use the constructor

Employee firstGuy=new Employee(name,ID,dept,pos);

System.out.println(firstGuy);

// Use the empty constructor

Employee emptyGuy=new Employee();

// Use the mutator methods to fill the fields

emptyGuy.setName("Fred");

emptyGuy.setID(9001);

emptyGuy.setDepartment("Fred");

emptyGuy.setPosition("Underwater Basket Weaver");

System.out.println(emptyGuy);

}

}

 

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Finally write an appropriate tostring method that prints
Reference No:- TGS0646876

Expected delivery within 24 Hours