Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Create a new Employee Class 
Build a new Employee class("Employee.java").  This class will be a sub-class of Person.  So it will inherit all the properties of Person(FirstName, LastName, Address and Email).  Also add 2 new properties, an employee id number and a salary.  Create this class, make sure to add 2 constructors(one that takes no args and 1 that takes all props from both the Person class and the Employee class) and a display() method that prints out all the props from both the Person class and the Employee class.  Then use main to test out this class.

Main Code -->

Employee e1;
e1 = new Employee(2323, "Bill", "Clinton", "Marietta", "bc@msn.com", 43000.00);
e1.display();      //prints all 6 properties


What I have tried:

Person class code:

Java
public class Person {
	
	//   ========================== Properties ===========================
	private String FirstName;
	private String LastName;
	private String Address;
	private String Email;
	
	//constructor with parameters
	Person(String FirstName,String LastName,String Address,String Email) {
		this.FirstName = FirstName;
		this.LastName = LastName;
		this.Address = Address;
		this.Email = Email;
	}
	
	//constructor with no parameters
	Person() {
		this.FirstName = "";
		this.LastName = "";
		this.Address = "";
		this.Email = "";
	}
	
	//   ==========================  Behaviors  ==========================
	public void setFirstName(String fn) {
		this.FirstName = fn;
	}
	
	public void setLastName(String ln) {
		this.LastName = ln;
	}
	
	public void setAddress(String a) {
		this.Address = a;
	}
	
	public void setEmail(String e) {
		this.Email = e;
	}
	
	
	
	public String getFirstName() {
		return this.FirstName;
	}
	
	public String getLastName() {
		return this.LastName;
	}
	
	public String getAddress() {
		return this.Address;
	}
	
	public String getEmail() {
		return this.Email;
	}
	
	//method that displays person data
	public void display() {
		System.out.println("First Name : "+this.FirstName);
		System.out.println("Last Name : "+this.LastName);
		System.out.println("Address : "+this.Address);
		System.out.println("Email : "+this.Email);
	} //end display()
	
	//overriding toString method
	public String toString() {
		return "FirstName: "+getFirstName() + "\nLastName: "+getLastName() + "\n" +this.Address.toString()
		+"\nEmail: "+getEmail();
	}
	
	//main method
	public static void main(String args []) {
		
		Person p1;
		p1 = new Person("Rodney","Duncan","70 Bowman St. South Windsor, CT 06074","rduncan@gmail.com");
		p1.display();
		
	}
	} 


Employee class code so far:

Java
public class Employee extends Person {

private int employeeId; 
private int salary; 


public Employee() {
		super(); 
		this.employeeId = 0; 
		this.salary = "";
	}
Posted
Updated 27-Mar-18 12:52pm
v2
Comments
Patrice T 27-Mar-18 17:25pm    
What is the question ?

1 solution

I see a problem in Employee constructor... :-)

Replace
Quote:
public Student() {

With
public Employee() {

And then of course complete the task: provide the constructor accepting all the parameters and the display method of the class Emplyee. Build them incrementally, exploiting the Person class code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900