Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Person Class and NewWorker Class using java

you will define two classes. The classes you will define are Person and NewWorker, where NewWorker is based on Worker. The descriptions of Person and NewWorker are provided below.
Class Person:
Person has two instance variables. The first is of type Name, and is called personName. The second is of type Person, and is called spouse. Person will have a constructor that accepts a string, which is the name of the person. Person has a method called setSpouse, which accepts a Person as an argument and sets the instance variable “spouse” to the received person. Person has a getPersonName method which returns the Name instance variable personName, and a method called getSpouseName which returns the Name instance variable of the spouse. Person has a method called getFamilyIncome, which accepts no arguments. The getFamilyIncome method returns the sum of income for a Person and its spouse, and they only contribute income if they are of NewWorker type. Lastly, Person has a toString method which returns “Name is ”, followed by the name of the person, and if the person has a spouse it appends “\nMarried to ”, followed by the name of the spouse.
Class NewWorker:
In the previous version of Worker, we defined some variables (such as name) and methods that should be encapsulated in a higher-level class, Person. Now that we have a Person class, we will use inheritance to reduce duplicate code and make it easier to expand the classes. NewWorker will extend Person. NewWorker therefore does not need to duplicate the variables or methods of class Person. NewWorker needs an int instance variable called workerNumber, which is set to one for the first NewWorker created and increases by one for each new NewWorker created. It also needs a MyDate instance variable called dateJoiningCompany. NewWorker also has a double instance variable called salary. Finally, it has a NewWorker instance variable called supervisor. NewWorker has two constructors. The first accepts a string for the name of the worker, a string for the date that the worker joined the company, and a double which is the salary of the worker. The second accepts only the name and the date. The salary should be initialized to zero in this constructor. The NewWorker class must have an integer variable called howManyWorkers that keeps track of how many workers have been created throughout the course of the runtime of the program. NewWorker has several methods as well. The first is called setSalary, accepting a
double as an argument, which sets the salary of the worker. Another is called getSalary, which returns the salary of the worker. Another is called setSupervisor, which accepts a NewWorker object and sets the supervisor instance variable to this object. The next is called getSupervisor, which returns the name of the supervisor. The toString method must return a string that says “Worker Number ”, followed by the workerNumber, followed by “\nWorker Name ” and the name of the worker. On another line, it will print “Date Joined: ” followed by dateJoiningCompany, and on another line it will either print “Supervisor: ” followed by the supervisor’s name (if it has one) or “No Supervisor” otherwise. Lastly, toString will print on another line “Salary: ” followed by the salary of the worker. Finally, the class NewWorker has a method called getHowManyWorkers, which returns howManyWorkers.
Sample output for Lab4Tester:
Supervisor of w1 is Hull, Mary J.
w2 is Worker Number 2
Worker Name Smith, John
Date Joined: November 15, 05
Supervisor: Hull, Mary J.
Salary: $25000.0
w3 is Worker Number 3
Worker Name Hull, Mary J.
Date Joined: September 6, 07
Supervisor: Harry, Richard M.
Salary: $40000.0
w4 is Worker Number 4
Worker Name Harry, Richard M.
Date Joined: August 1, 04
No Supervisor
Salary: $58000.0
w5 is Worker Number 5
Worker Name Chowhan, Jessica
Date Joined: January 21, 06
Supervisor: Harry, Richard M.
Salary: $30000.0
Total family income of Robert 65000.0
Total family income of Edward 40000.0

Answer:

Tester Class

public class Lab4Tester {
	public static void main(String[] args){
		NewWorker w1, w2, w3, w4, w5;
		Person p1, p2;

		p1 = new Person("Edward Teller");
		p2 = new Person("Liz Powell");

		w1 = new NewWorker ("Robert William Hunter", "23/10/2005", 35000.00);
		w2 = new NewWorker ("John Smith", "15/11/2005", 25000.00);
		w3 = new NewWorker ("Mary Jane Hull", "06/09/2007");
		w4 = new NewWorker ("Richard M. Harry", "01/08/2004");
		w5 = new NewWorker ("Jessica Chowhan", "21/01/2006", 30000.00);

		w3.setSalary(40000.00);
		w4.setSalary(53000.00);
		w4.setSalary(55000.00);
		w4.setSalary(58000.00);

		w2.setSupervisor(w3);
		w3.setSupervisor(w4);
		w1.setSupervisor(w3);
		w5.setSupervisor(w4);

		w3.setSpouse(p1);
		p1.setSpouse(w3);

		w2.setSpouse(p2);
		p2.setSpouse(w2);

		w1.setSpouse(w5);
		w5.setSpouse(w1);

		System.out.println("Supervisor of w1 is " + w1.getSupervisor() + "\n");
		System.out.println("w2 is " + w2 + "\n");
		System.out.println("w3 is " + w3 + "\n");
		System.out.println("w4 is " + w4 + "\n");
		System.out.println("w5 is " + w5 + "\n");
		System.out.println("Total family income of Robert "
			                + w1.getFamilyIncome());
		System.out.println("Total family income of Edward "
			                + p1.getFamilyIncome());
		}

}

MyDate Class

import java.util.StringTokenizer;

public class MyDate{
	private int day;
	private int month;
	private int year;

	public MyDate(String unformattedDate){
		StringTokenizer splitDate = new StringTokenizer(unformattedDate, "/");

		this.day = Integer.parseInt(splitDate.nextToken());
		this.month = Integer.parseInt(splitDate.nextToken());
		this.year = Integer.parseInt(splitDate.nextToken());
	}

	public MyDate(MyDate myDate){
		this.day = myDate.day;
		this.month = myDate.month;
		this.year = myDate.year;
	}

	public String toString(){
		String twoDigitYear = String.valueOf(this.year).substring(2,4);
		String monthName [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
				"October", "November", "December"};
	
		return monthName[month-1] + " " + day + ", " + twoDigitYear;
		
	}

	public boolean lessThan(MyDate myDate){
		if(this.year < myDate.year){
			return true;
		}
		else if (this.year > myDate.year){
			return false;
		}
		else if (this.month < myDate.month){
			return true;
		}
		else if (this.month > myDate.month){
			return false;
		}
		else if (this.day < myDate.day){
			return true;
		}
		else
			return false;
	}

	public boolean equals(MyDate myDate) {
		if (myDate == null) return false;
		if((this.day == myDate.day) && (this.month == myDate.month) && (this.year == myDate.year)){
			return true;
		}
		else
			return false;
	}
}

Name Class

import java.util.StringTokenizer;
public class Name{

	private String firstName = "";
	private String middleName = "";
	private String lastName = "";

	public Name(String inputName){
		StringTokenizer splitName = new StringTokenizer(inputName);
		int numTokens = splitName.countTokens();

		firstName = splitName.nextToken();
		if(numTokens == 3){
			middleName = splitName.nextToken();
		}
		else{
			middleName = null;
		}
		lastName = splitName.nextToken();
	}
	
	public Name(Name nameCopy){
		this.firstName = nameCopy.firstName;
		this.middleName = nameCopy.middleName;
		this.lastName = nameCopy.lastName;
	}
	
	public void setName(String newFirstName, String newMiddleName, String newLastName){
		this.firstName = newFirstName;
		this.middleName = newMiddleName;
		this.lastName = newLastName;	
	}

	public String toString(){
		if(middleName != null){
			return (lastName +", " + firstName + " " + middleName.charAt(0) + ".");
		}
		else
			return (lastName +", " + firstName);
	}
}

NewWorker Class

public class NewWorker extends Person {

	private static int howManyWorkers = 1; 
	private int workerNumber;  
	private MyDate dateJoiningCompany; 
	private double Salary; 
	private NewWorker Supervisor;  
	
	/*First Constructor accepts a string for the name of the worker, a string for the 
	date that the worker joined the company, and a double which is the salary of
	the worker*/
	public NewWorker(String workerName,String dateJoiningCompany,double Salary){
		super(workerName); 
		this.dateJoiningCompany = new MyDate(dateJoiningCompany); 
		this.Salary = Salary; 
		workerNumber = howManyWorkers; 
		howManyWorkers++;		
	} 
	
	/* Second Constructor accepts only the name and the date.
	and the salary initialized to zero in this constructor*/
	public NewWorker(String workerName, String dateJoiningCompany){
		super(workerName); 
		this.dateJoiningCompany = new MyDate(dateJoiningCompany);
		Salary = 0.0; 
		workerNumber = howManyWorkers; 
		howManyWorkers++;
	}
	
	/* setSalary Method accepting a double as an argument, which sets the salary 
	of the worker.*/
	public void setSalary(double Salary){
		this.Salary = Salary; 
	}
	
	/* getSalary Method returns the salary of the worker.*/
	public double getSalary(){
		return Salary; 
	}
	
	/* setSupervisor Method accepts a NewWorker object and sets the supervisor 
	instance variable to this object*/
	public void setSupervisor(NewWorker Supervisor){
		this.Supervisor = Supervisor; 
	}
	
	/* getSupervisor Method returns the name of the supervisor.*/
	public Name getSupervisor(){
		if(Supervisor != null){
			return Supervisor.getPersonName();
		}
		else {
			return null;
		}
	}
	
	public String toString(){
		String s;
		s = "Worker Number " + workerNumber + "\nWorker Name " + getPersonName() 
		+ ".\n" + "Date Joined: " + dateJoiningCompany; 
		if (getSupervisor() != null) { 
			s += "\n" + "Supervisor: " + getSupervisor(); 
		}
		
		else {
			s += "\n" + "No Supervisor ";
		}
		s += "\n" + "salary: $" + Salary;  
		return s;

	}
	public int howManyWorkers(){
		return howManyWorkers; 
	}
	
}

person Class

public class Person {
	
	private Name personName;
	private Person spouse; 
	
	/*constructor takes name string and sets it in the instance variable*/
	public Person(String fullName)
	{
		personName = new Name(fullName); 
	}
	
	/*set spouse takes another person as an argument*/
	public void setSpouse(Person aPerson)
	{
		this.spouse = aPerson;
		
	}
	/*get name returns the person's name*/
	public Name getPersonName()
	{
		return personName; 
	}
	/*getFamilyIncome checks if this person is married and if they or their spouse is working
	to get salary the person needs to be casted as a NewWorker*/ 
	public double getFamilyIncome()
	{
		double allTheMoney = 0.0; 
		if(spouse != null)
		{
			if(this instanceof NewWorker)
			{
				allTheMoney +=((NewWorker)this).getSalary(); 
			}
			if(spouse instanceof NewWorker)
			{
				allTheMoney +=((NewWorker)spouse).getSalary();  
			}
		}
		else
		{
			if(this instanceof NewWorker)
			{
				allTheMoney +=((NewWorker)this).getSalary();
			}
		}
		
		return allTheMoney; 
	}
	/*toString checks if this person has a spouse or not and returns
	both names if there is */
	public String toString()
	{
		if(spouse != null)
		{
			return "Name is " + personName.toString() + "\n Married to " + spouse.personName.toString(); 
		}
			return "Name is " + personName.toString(); 
	}
	
	

	
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!