/* For this lab, you should write: - A constructor that initializes the fields 1
ID: 3534202 • Letter: #
Question
/* For this lab, 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.Scanner; import java.io.*; 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); } } class Employee { private String empName; private int empId; private String department; private String position; public Employee(String name, int num, String dept, String pos) { empName = name; empId = num; department = dept; position = pos; } public Employee () { empName = ""; empId = 0; department = " "; position = " "; } public void setName (String Name) { empName=Name; } public void setId (int Number) { empId = Number; } public void setDepartment (String Name) { department= Name; } public void setPosition (String Name) { position= Name; } }Explanation / Answer
Check this please, fixed :
import java.util.Scanner;
import java.io.*;
public class Lab12Driver
{
public static void main(String [] args)
{
Scanner inScan=new Scanner(System.in);
System.out.println("Enter employee's Name: ");
String name=inScan.nextLine();
System.out.println("Enter employee's ID Number: ");
int ID=inScan.nextInt();
inScan.nextLine();
System.out.println("Enter employee's department: ");
String dept=inScan.nextLine();
System.out.println("Enter employee's position: ");
String pos=inScan.nextLine();
Employee firstGuy=new Employee(name,ID,dept,pos);
System.out.println(firstGuy);
Employee emptyGuy=new Employee();
emptyGuy.setName("Fred");
emptyGuy.setId(9001);
emptyGuy.setDepartment("Fred");
emptyGuy.setPosition("Underwater Basket Weaver");
System.out.println(emptyGuy);
}
}
class Employee
{
private String empName;
private int empId;
private String department;
private String position;
public Employee(String name, int num, String dept, String pos)
{
empName = name;
empId = num;
department = dept;
position = pos;
}
public Employee ()
{
empName = "";
empId = 0;
department = " ";
position = " ";
}
public void setName (String Name)
{
empName=Name;
}
public void setId (int Number)
{
empId = Number;
}
public void setDepartment (String Name)
{
department= Name;
}
public void setPosition (String Name)
{
position= Name;
}
public String toString()
{
return "Employee Name : "+empName+" " +
"Employee ID : "+empId+" " +
"Employee Department : "+department+" " +
"Employee Position : "+position+" ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.