SlideShare a Scribd company logo
define a class name Employee whose objects are records for employee. Derive this class from the
class person given in the listing 8.1. An employee record inherits an employee's name from the
class Person. In addition, an employee record contains an annual salary represented as a single
value of type double, a hire date that gives the year hired as a single value of type int, and an
identification number that is a value of type String. Give your class a reasonable complement of
constructors, accessor methods, and mutator methods, as well as an equals method. Write a
program to fully test your class definition
this is the test file
import java.util.*;
public class EmployeeTest
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
char repeat;
do // repeat if user says 'yes'
{
// Test the nine constructors (uses writeOutput method)
Employee e1 = new Employee(); // default constructor
System.out.println("Using default constructor:");
e1.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e2 = new Employee("Mondo Kane");
System.out.println("Using constructor with just name:");
e2.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e3 = new Employee("Fleetis Pascal", 111111.11);
System.out.println("Constructor with name & salary :");
e3.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e4 = new Employee("Carl Wolf", 1968);
System.out.println("Constructor with name and hire date:");
e4.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e5 = new Employee("Sharon Kelly", "123-45-6789");
System.out.println("Constructor with name and ssn:");
e5.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e6 = new Employee("Joann Rousch", 333333.33, 1963);
System.out.println("Constructor with name, salary & hire date:");
e6.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e7 = new Employee("Lucy Sharp", 444444.44, "987-65-4321");
System.out.println("Constructor with name, salary & ssn:");
e7.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e8 = new Employee("Pierre Sokolskis", 1964, "999-99-9999");
System.out.println("Constructor with name, hire date & ssn:");
e8.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e9 = new Employee("Last One", 555.55, 1999,
"888-88-8888");
System.out.println("Constructor with name, salary, hire date, and ssn:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// Test methods to change, return and write values
// change all
System.out.println("Before:");
e9.writeOutput();
System.out.println();
System.out.println("After method to change all:");
System.out.println();
e9.set("Changed Name", 1010.10, 1010, "101-10-1010");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// change name
e9.setName("Jekyl N. Hyde");
System.out.println("After method to change name:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return name
System.out.println("Return method for name: " + e9.getName());
System.out.println();
// write name
System.out.println("Write name method:");
System.out.println();
e9.writeName();
System.out.println();
// change salary
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setSalary(987.65);
System.out.println("After method to change salary:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return salary
System.out.println("Return method for salary: " + e9.getSalary());
System.out.println();
// write salary
System.out.println("Write salary method:");
System.out.println();
e9.writeSalary();
System.out.println();
// change hire date
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setHireDate(2001);
System.out.println("After method to change hire date:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return hire date
System.out.println("Return method for hire date: " + e9.getHireDate());
System.out.println();
// write hire date
System.out.println("Write hire date method:");
System.out.println();
e9.writeHireDate();
System.out.println();
// change social security number
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setSsn("777-77-7777");
System.out.println("After method to change ssn:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return social security number
System.out.println("Return method for ssn: " + e9.getSsn());
System.out.println();
// write social security number
System.out.println("Write ssn method:");
System.out.println();
e9.writeSsn();
System.out.println();
// test equals
// create e10 with the same vales as e9
// and it should test true
Employee e10 = new Employee("Jekyl N. Hyde", 987.65, 2001, "777-77-7777");
System.out.println("Employee 9 data:");
e9.writeOutput();
System.out.println();
System.out.println("Employee 10 data:");
e10.writeOutput();
System.out.println();
if(e9.equals(e10))
System.out.println("TRUE: e9 equals e10!");
else
System.out.println("FALSE: e9 does NOT equal e10.");
System.out.println();
e10.setSsn("777-77-777"); // create a single char difference
System.out.println("After changing just one character"
+ " in the social security number,");
System.out.println("the data for the 2 employees are");
System.out.println();
System.out.println("Employee 9 data:");
e9.writeOutput();
System.out.println();
System.out.println("Employee 10 data:");
e10.writeOutput();
System.out.println();
if(e9.equals(e10))
System.out.println("TRUE: e9 equals e10!");
else
System.out.println("FALSE: e9 does NOT equal e10.");
System.out.println("Do again? (Y for Yes, or N for No)");
repeat = keyboard.next().charAt(0);
} while((repeat == 'y') || (repeat == 'Y'));
}
}
modify the Person class to make the test file work
public class Person
{
private String name;
public Person()
{
name = "No name yet.";
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println("Name: " + name);
}
public boolean hasSameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.getName()));
}
}
this is how the input should look like. please add comments to the code. jGRASP exec java
EmployeeTest Using default constructor: Name: No name yet Salary: 0. lire date 1000 SSN:
000-00-0000 Using constructor with just name Name Mondo Kane Salary: 0.0 lire date: 1000
SSN: 000-00-0000 Constructor with name & salary Name : 1eetis Pascal Hire date: 1000 SSN
000-00-0000 Constructor with name and hire date: Name: Carl Wolf Salary: 0.0 lire date: 1968
SSN 000-00-0000 Constructor with name and ssn: Name Sharon Kelly Salary: 0. lire date 1000
SSN: 123-45-6789
Solution
import java.util.*;
class Person
{
private String name;
public Person()
{
name = "No name yet.";
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println("Name: " + name);
}
public boolean hasSameName(Person otherPerson)
{
return (this.name.equalsIgnoreCase(otherPerson.getName()));
}
}
class Employee extends Person
{
private double salary;
private int hiredate;
private String ssn;
public Employee() //Nine Constructors
{
super();
}
public Employee(String name)
{
super(name);
}
public Employee(String name,double sal)
{
super(name);
salary =sal;
}
public Employee(String name,int hd)
{
super(name);
hiredate = hd;
}
public Employee(String name,String s)
{
super(name);
ssn = s;
}
public Employee(String name,double sal,int hd)
{
super(name);
hiredate = hd;
salary =sal;
}
public Employee(String name,double sal,String s)
{
super(name);
salary =sal;
ssn = s;
}
public Employee(String name,int hd,String s)
{
super(name);
hiredate = hd;
ssn = s;
}
public Employee(String name,double sal,int hd,String s)
{
super(name);
salary =sal;
hiredate =hd;
ssn = s;
}
public void set(String n,double sal,int hd,String s) //set method
{
super.setName(n);
salary = sal;
hiredate = hd;
ssn = s;
}
public void writeOutput() //writeOutput method
{
writeName();
writeSalary();
writeHireDate();
writeSsn();
}
public void writeName()
{
super.writeOutput();
}
public void setSalary(double sal) //set,get and write methods
{
salary = sal;
}
public double getSalary()
{
return salary;
}
public void writeSalary()
{
System.out.println("Salary : "+salary);
}
public void setHireDate(int hd)
{
hiredate = hd;
}
public int getHireDate()
{
return hiredate;
}
public void writeHireDate()
{
System.out.println("Hire Date : " +hiredate);
}
public void setSsn(String s)
{
ssn = s;
}
public String getSsn()
{
return ssn;
}
public void writeSsn()
{
System.out.println("SSN Number : "+ssn);
}
}
class EmployeeTest
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
char repeat;
do // repeat if user says 'yes'
{
// Test the nine constructors (uses writeOutput method)
Employee e1 = new Employee(); // default constructor
System.out.println("Using default constructor:");
e1.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e2 = new Employee("Mondo Kane");
System.out.println("Using constructor with just name:");
e2.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e3 = new Employee("Fleetis Pascal", 111111.11);
System.out.println("Constructor with name & salary :");
e3.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e4 = new Employee("Carl Wolf", 1968);
System.out.println("Constructor with name and hire date:");
e4.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e5 = new Employee("Sharon Kelly", "123-45-6789");
System.out.println("Constructor with name and ssn:");
e5.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e6 = new Employee("Joann Rousch", 333333.33, 1963);
System.out.println("Constructor with name, salary & hire date:");
e6.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e7 = new Employee("Lucy Sharp", 444444.44, "987-65-4321");
System.out.println("Constructor with name, salary & ssn:");
e7.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e8 = new Employee("Pierre Sokolskis", 1964, "999-99-9999");
System.out.println("Constructor with name, hire date & ssn:");
e8.writeOutput();
System.out.println();
System.out.println("===============================");
Employee e9 = new Employee("Last One", 555.55, 1999,
"888-88-8888");
System.out.println("Constructor with name, salary, hire date, and ssn:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// Test methods to change, return and write values
// change all
System.out.println("Before:");
e9.writeOutput();
System.out.println();
System.out.println("After method to change all:");
System.out.println();
e9.set("Changed Name", 1010.10, 1010, "101-10-1010");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// change name
e9.setName("Jekyl N. Hyde");
System.out.println("After method to change name:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return name
System.out.println("Return method for name: " + e9.getName());
System.out.println();
// write name
System.out.println("Write name method:");
System.out.println();
e9.writeName();
System.out.println();
// change salary
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setSalary(987.65);
System.out.println("After method to change salary:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return salary
System.out.println("Return method for salary: " + e9.getSalary());
System.out.println();
// write salary
System.out.println("Write salary method:");
System.out.println();
e9.writeSalary();
System.out.println();
// change hire date
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setHireDate(2001);
System.out.println("After method to change hire date:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return hire date
System.out.println("Return method for hire date: " + e9.getHireDate());
System.out.println();
// write hire date
System.out.println("Write hire date method:");
System.out.println();
e9.writeHireDate();
System.out.println();
// change social security number
System.out.println("Before:");
e9.writeOutput();
System.out.println();
e9.setSsn("777-77-7777");
System.out.println("After method to change ssn:");
e9.writeOutput();
System.out.println();
System.out.println("===============================");
// return social security number
System.out.println("Return method for ssn: " + e9.getSsn());
System.out.println();
// write social security number
System.out.println("Write ssn method:");
System.out.println();
e9.writeSsn();
System.out.println();
// test equals
// create e10 with the same vales as e9
// and it should test true
Employee e10 = new Employee("Jekyl N. Hyde", 987.65, 2001, "777-77-7777");
System.out.println("Employee 9 data:");
e9.writeOutput();
System.out.println();
System.out.println("Employee 10 data:");
e10.writeOutput();
System.out.println();
if(e9.equals(e10))
System.out.println("TRUE: e9 equals e10!");
else
System.out.println("FALSE: e9 does NOT equal e10.");
System.out.println();
e10.setSsn("777-77-777"); // create a single char difference
System.out.println("After changing just one character"
+ " in the social security number,");
System.out.println("the data for the 2 employees are");
System.out.println();
System.out.println("Employee 9 data:");
e9.writeOutput();
System.out.println();
System.out.println("Employee 10 data:");
e10.writeOutput();
System.out.println();
if(e9.equals(e10))
System.out.println("TRUE: e9 equals e10!");
else
System.out.println("FALSE: e9 does NOT equal e10.");
System.out.println("Do again? (Y for Yes, or N for No)");
repeat = keyboard.next().charAt(0);
} while((repeat == 'y') || (repeat == 'Y'));
}
}
output:

More Related Content

PDF
Hi, I need help with a java programming project. specifically practi.pdf
PDF
package employeeType.employee;public abstract class Employee {  .pdf
PDF
I need help for my next project due next tuesday can you help me in .pdf
PDF
Tasks In this assignment you are required to design and imp.pdf
DOCX
2. Create a Java class called EmployeeMain within the same project Pr.docx
PDF
package employeeType.employee;public class Employee {   private .pdf
PDF
HELP IN JAVACreate a main method and use these input files to tes.pdf
PDF
Part 1 Implement a superclass Employee that has the following field.pdf
Hi, I need help with a java programming project. specifically practi.pdf
package employeeType.employee;public abstract class Employee {  .pdf
I need help for my next project due next tuesday can you help me in .pdf
Tasks In this assignment you are required to design and imp.pdf
2. Create a Java class called EmployeeMain within the same project Pr.docx
package employeeType.employee;public class Employee {   private .pdf
HELP IN JAVACreate a main method and use these input files to tes.pdf
Part 1 Implement a superclass Employee that has the following field.pdf

Similar to define a class name Employee whose objects are records for employee..pdf (20)

PDF
Scala 2013 review
PDF
package employeeType.employee;public class Employee {    private.pdf
PDF
First compile all the classes.But to run the program we have to run .pdf
PDF
Implementation Your program shall contain at least the follo.pdf
PDF
Code Include libraries. import javax.swing.JOptionPane;.pdf
PDF
You will need to develop a system that can track employee informatio.pdf
DOCX
maJavaProjectFinalExam.classpathmaJavaProjectFinalExam.p.docx
DOCX
PDF
Should be in JavaInterface Worker should extend Serializable from .pdf
PDF
Define a class named Doctor whose objects are records for clinic’s d.pdf
PDF
Java & OOP Core Concept
PDF
ACP Java Assignment.pdf
DOCX
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
DOC
Cis247 i lab 5 inheritance
DOCX
Assignment 7
PPT
lectulecturleclecturlecturlecturlecturturre15.ppt
DOC
Cis247 a ilab 5 inheritance
DOC
Cis247 a ilab 5 inheritance
PDF
Add a private variable SSN to the person class- Add a private variable.pdf
PDF
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
Scala 2013 review
package employeeType.employee;public class Employee {    private.pdf
First compile all the classes.But to run the program we have to run .pdf
Implementation Your program shall contain at least the follo.pdf
Code Include libraries. import javax.swing.JOptionPane;.pdf
You will need to develop a system that can track employee informatio.pdf
maJavaProjectFinalExam.classpathmaJavaProjectFinalExam.p.docx
Should be in JavaInterface Worker should extend Serializable from .pdf
Define a class named Doctor whose objects are records for clinic’s d.pdf
Java & OOP Core Concept
ACP Java Assignment.pdf
33.docxSTEP 1 Understand the UML Diagram Analyze and under.docx
Cis247 i lab 5 inheritance
Assignment 7
lectulecturleclecturlecturlecturlecturturre15.ppt
Cis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritance
Add a private variable SSN to the person class- Add a private variable.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
Ad

More from fashioncollection2 (20)

PDF
All programming assignments should include the name, or names, of th.pdf
PDF
After a (not very successful) trick or treating round, Candice has 1.pdf
PDF
Why steroids can cross the cell membraneWhy steroids can cross .pdf
PDF
Write a method called uniqueNumbers that takes an int array as param.pdf
PDF
Write a command in unixlinux to Display all the files whose names e.pdf
PDF
Why is “Profit” a problematic target Please consider in your answer.pdf
PDF
Which of the following statements about protists is falsea) There a.pdf
PDF
Which cable type would be used to connect a router to a switchA. .pdf
PDF
What is the role of abiotic factors in the formation of biomesS.pdf
PDF
What information needs to be encoded in a loadstorebranchALU inst.pdf
PDF
What are the major terrestrial adaptations of plants1- List the F.pdf
PDF
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
PDF
The insatiable demand for everything wireless, video, and Web-enable.pdf
PDF
Technology is an ever-growing system that provides advantages and in.pdf
PDF
Suppose that the proportions of blood phenotypes in a particular pop.pdf
PDF
Select all of the following that are true regarding evolution. Altho.pdf
PDF
Role of involvement in consumer decision-making. Identify the level .pdf
PDF
Questionsa What are some of the barriers to understanding an issu.pdf
PDF
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
PDF
Quality software project managementHow are tasks, activities, and .pdf
All programming assignments should include the name, or names, of th.pdf
After a (not very successful) trick or treating round, Candice has 1.pdf
Why steroids can cross the cell membraneWhy steroids can cross .pdf
Write a method called uniqueNumbers that takes an int array as param.pdf
Write a command in unixlinux to Display all the files whose names e.pdf
Why is “Profit” a problematic target Please consider in your answer.pdf
Which of the following statements about protists is falsea) There a.pdf
Which cable type would be used to connect a router to a switchA. .pdf
What is the role of abiotic factors in the formation of biomesS.pdf
What information needs to be encoded in a loadstorebranchALU inst.pdf
What are the major terrestrial adaptations of plants1- List the F.pdf
The total lung capacity of a patient is 5.5 liters. Find the patient.pdf
The insatiable demand for everything wireless, video, and Web-enable.pdf
Technology is an ever-growing system that provides advantages and in.pdf
Suppose that the proportions of blood phenotypes in a particular pop.pdf
Select all of the following that are true regarding evolution. Altho.pdf
Role of involvement in consumer decision-making. Identify the level .pdf
Questionsa What are some of the barriers to understanding an issu.pdf
Rainfall Intensity Duration Frequency Graph ear Reture Period torm Du.pdf
Quality software project managementHow are tasks, activities, and .pdf
Ad

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Structure & Organelles in detailed.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
master seminar digital applications in india
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
TR - Agricultural Crops Production NC III.pdf
RMMM.pdf make it easy to upload and study
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPH.pptx obstetrics and gynecology in nursing
Cell Structure & Organelles in detailed.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
master seminar digital applications in india
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Classroom Observation Tools for Teachers
Pharma ospi slides which help in ospi learning

define a class name Employee whose objects are records for employee..pdf

  • 1. define a class name Employee whose objects are records for employee. Derive this class from the class person given in the listing 8.1. An employee record inherits an employee's name from the class Person. In addition, an employee record contains an annual salary represented as a single value of type double, a hire date that gives the year hired as a single value of type int, and an identification number that is a value of type String. Give your class a reasonable complement of constructors, accessor methods, and mutator methods, as well as an equals method. Write a program to fully test your class definition this is the test file import java.util.*; public class EmployeeTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); char repeat; do // repeat if user says 'yes' { // Test the nine constructors (uses writeOutput method) Employee e1 = new Employee(); // default constructor System.out.println("Using default constructor:"); e1.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e2 = new Employee("Mondo Kane"); System.out.println("Using constructor with just name:"); e2.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e3 = new Employee("Fleetis Pascal", 111111.11); System.out.println("Constructor with name & salary :");
  • 2. e3.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e4 = new Employee("Carl Wolf", 1968); System.out.println("Constructor with name and hire date:"); e4.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e5 = new Employee("Sharon Kelly", "123-45-6789"); System.out.println("Constructor with name and ssn:"); e5.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e6 = new Employee("Joann Rousch", 333333.33, 1963); System.out.println("Constructor with name, salary & hire date:"); e6.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e7 = new Employee("Lucy Sharp", 444444.44, "987-65-4321"); System.out.println("Constructor with name, salary & ssn:"); e7.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e8 = new Employee("Pierre Sokolskis", 1964, "999-99-9999"); System.out.println("Constructor with name, hire date & ssn:"); e8.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e9 = new Employee("Last One", 555.55, 1999, "888-88-8888");
  • 3. System.out.println("Constructor with name, salary, hire date, and ssn:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // Test methods to change, return and write values // change all System.out.println("Before:"); e9.writeOutput(); System.out.println(); System.out.println("After method to change all:"); System.out.println(); e9.set("Changed Name", 1010.10, 1010, "101-10-1010"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // change name e9.setName("Jekyl N. Hyde"); System.out.println("After method to change name:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return name System.out.println("Return method for name: " + e9.getName()); System.out.println(); // write name System.out.println("Write name method:"); System.out.println(); e9.writeName(); System.out.println(); // change salary System.out.println("Before:"); e9.writeOutput(); System.out.println();
  • 4. e9.setSalary(987.65); System.out.println("After method to change salary:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return salary System.out.println("Return method for salary: " + e9.getSalary()); System.out.println(); // write salary System.out.println("Write salary method:"); System.out.println(); e9.writeSalary(); System.out.println(); // change hire date System.out.println("Before:"); e9.writeOutput(); System.out.println(); e9.setHireDate(2001); System.out.println("After method to change hire date:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return hire date System.out.println("Return method for hire date: " + e9.getHireDate()); System.out.println(); // write hire date System.out.println("Write hire date method:"); System.out.println(); e9.writeHireDate(); System.out.println();
  • 5. // change social security number System.out.println("Before:"); e9.writeOutput(); System.out.println(); e9.setSsn("777-77-7777"); System.out.println("After method to change ssn:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return social security number System.out.println("Return method for ssn: " + e9.getSsn()); System.out.println(); // write social security number System.out.println("Write ssn method:"); System.out.println(); e9.writeSsn(); System.out.println(); // test equals // create e10 with the same vales as e9 // and it should test true Employee e10 = new Employee("Jekyl N. Hyde", 987.65, 2001, "777-77-7777"); System.out.println("Employee 9 data:"); e9.writeOutput(); System.out.println(); System.out.println("Employee 10 data:"); e10.writeOutput(); System.out.println(); if(e9.equals(e10)) System.out.println("TRUE: e9 equals e10!"); else System.out.println("FALSE: e9 does NOT equal e10.");
  • 6. System.out.println(); e10.setSsn("777-77-777"); // create a single char difference System.out.println("After changing just one character" + " in the social security number,"); System.out.println("the data for the 2 employees are"); System.out.println(); System.out.println("Employee 9 data:"); e9.writeOutput(); System.out.println(); System.out.println("Employee 10 data:"); e10.writeOutput(); System.out.println(); if(e9.equals(e10)) System.out.println("TRUE: e9 equals e10!"); else System.out.println("FALSE: e9 does NOT equal e10."); System.out.println("Do again? (Y for Yes, or N for No)"); repeat = keyboard.next().charAt(0); } while((repeat == 'y') || (repeat == 'Y')); } } modify the Person class to make the test file work public class Person { private String name; public Person() { name = "No name yet."; }
  • 7. public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.getName())); } } this is how the input should look like. please add comments to the code. jGRASP exec java EmployeeTest Using default constructor: Name: No name yet Salary: 0. lire date 1000 SSN: 000-00-0000 Using constructor with just name Name Mondo Kane Salary: 0.0 lire date: 1000 SSN: 000-00-0000 Constructor with name & salary Name : 1eetis Pascal Hire date: 1000 SSN 000-00-0000 Constructor with name and hire date: Name: Carl Wolf Salary: 0.0 lire date: 1968 SSN 000-00-0000 Constructor with name and ssn: Name Sharon Kelly Salary: 0. lire date 1000 SSN: 123-45-6789 Solution
  • 8. import java.util.*; class Person { private String name; public Person() { name = "No name yet."; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return (this.name.equalsIgnoreCase(otherPerson.getName())); } } class Employee extends Person { private double salary; private int hiredate;
  • 9. private String ssn; public Employee() //Nine Constructors { super(); } public Employee(String name) { super(name); } public Employee(String name,double sal) { super(name); salary =sal; } public Employee(String name,int hd) { super(name); hiredate = hd; } public Employee(String name,String s) { super(name); ssn = s; } public Employee(String name,double sal,int hd) { super(name); hiredate = hd; salary =sal; } public Employee(String name,double sal,String s) { super(name); salary =sal; ssn = s;
  • 10. } public Employee(String name,int hd,String s) { super(name); hiredate = hd; ssn = s; } public Employee(String name,double sal,int hd,String s) { super(name); salary =sal; hiredate =hd; ssn = s; } public void set(String n,double sal,int hd,String s) //set method { super.setName(n); salary = sal; hiredate = hd; ssn = s; } public void writeOutput() //writeOutput method { writeName(); writeSalary(); writeHireDate(); writeSsn(); } public void writeName() { super.writeOutput(); } public void setSalary(double sal) //set,get and write methods
  • 11. { salary = sal; } public double getSalary() { return salary; } public void writeSalary() { System.out.println("Salary : "+salary); } public void setHireDate(int hd) { hiredate = hd; } public int getHireDate() { return hiredate; } public void writeHireDate() { System.out.println("Hire Date : " +hiredate); } public void setSsn(String s) { ssn = s; } public String getSsn() { return ssn; } public void writeSsn() { System.out.println("SSN Number : "+ssn);
  • 12. } } class EmployeeTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); char repeat; do // repeat if user says 'yes' { // Test the nine constructors (uses writeOutput method) Employee e1 = new Employee(); // default constructor System.out.println("Using default constructor:"); e1.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e2 = new Employee("Mondo Kane"); System.out.println("Using constructor with just name:"); e2.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e3 = new Employee("Fleetis Pascal", 111111.11); System.out.println("Constructor with name & salary :"); e3.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e4 = new Employee("Carl Wolf", 1968); System.out.println("Constructor with name and hire date:"); e4.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e5 = new Employee("Sharon Kelly", "123-45-6789"); System.out.println("Constructor with name and ssn:"); e5.writeOutput();
  • 13. System.out.println(); System.out.println("==============================="); Employee e6 = new Employee("Joann Rousch", 333333.33, 1963); System.out.println("Constructor with name, salary & hire date:"); e6.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e7 = new Employee("Lucy Sharp", 444444.44, "987-65-4321"); System.out.println("Constructor with name, salary & ssn:"); e7.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e8 = new Employee("Pierre Sokolskis", 1964, "999-99-9999"); System.out.println("Constructor with name, hire date & ssn:"); e8.writeOutput(); System.out.println(); System.out.println("==============================="); Employee e9 = new Employee("Last One", 555.55, 1999, "888-88-8888"); System.out.println("Constructor with name, salary, hire date, and ssn:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // Test methods to change, return and write values // change all System.out.println("Before:"); e9.writeOutput(); System.out.println(); System.out.println("After method to change all:"); System.out.println(); e9.set("Changed Name", 1010.10, 1010, "101-10-1010"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // change name e9.setName("Jekyl N. Hyde");
  • 14. System.out.println("After method to change name:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return name System.out.println("Return method for name: " + e9.getName()); System.out.println(); // write name System.out.println("Write name method:"); System.out.println(); e9.writeName(); System.out.println(); // change salary System.out.println("Before:"); e9.writeOutput(); System.out.println(); e9.setSalary(987.65); System.out.println("After method to change salary:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return salary System.out.println("Return method for salary: " + e9.getSalary()); System.out.println(); // write salary System.out.println("Write salary method:"); System.out.println(); e9.writeSalary(); System.out.println(); // change hire date System.out.println("Before:"); e9.writeOutput(); System.out.println(); e9.setHireDate(2001); System.out.println("After method to change hire date:"); e9.writeOutput();
  • 15. System.out.println(); System.out.println("==============================="); // return hire date System.out.println("Return method for hire date: " + e9.getHireDate()); System.out.println(); // write hire date System.out.println("Write hire date method:"); System.out.println(); e9.writeHireDate(); System.out.println(); // change social security number System.out.println("Before:"); e9.writeOutput(); System.out.println(); e9.setSsn("777-77-7777"); System.out.println("After method to change ssn:"); e9.writeOutput(); System.out.println(); System.out.println("==============================="); // return social security number System.out.println("Return method for ssn: " + e9.getSsn()); System.out.println(); // write social security number System.out.println("Write ssn method:"); System.out.println(); e9.writeSsn(); System.out.println(); // test equals // create e10 with the same vales as e9 // and it should test true Employee e10 = new Employee("Jekyl N. Hyde", 987.65, 2001, "777-77-7777"); System.out.println("Employee 9 data:"); e9.writeOutput(); System.out.println(); System.out.println("Employee 10 data:"); e10.writeOutput();
  • 16. System.out.println(); if(e9.equals(e10)) System.out.println("TRUE: e9 equals e10!"); else System.out.println("FALSE: e9 does NOT equal e10."); System.out.println(); e10.setSsn("777-77-777"); // create a single char difference System.out.println("After changing just one character" + " in the social security number,"); System.out.println("the data for the 2 employees are"); System.out.println(); System.out.println("Employee 9 data:"); e9.writeOutput(); System.out.println(); System.out.println("Employee 10 data:"); e10.writeOutput(); System.out.println(); if(e9.equals(e10)) System.out.println("TRUE: e9 equals e10!"); else System.out.println("FALSE: e9 does NOT equal e10."); System.out.println("Do again? (Y for Yes, or N for No)"); repeat = keyboard.next().charAt(0); } while((repeat == 'y') || (repeat == 'Y')); } } output: