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

Household Class and Student Class using java

You will need Name and MyDate, as well as Person and NewWorker classes. You will also define a class called Student and another called Household. the definitions of new classes, are provided below.
Class Person:
Person will be an abstract class. It will now contain a public abstract method called “getSalary” which returns a double. Because we have a getSalary defined in Person, which is a superclass of NewWorker and Student, we can now clean up the code in getFamilyIncome() (HINT: you no longer should be casting or using instanceof).
Class NewWorker:
Should not need any changes.
Class MyDate and Class Name:
Should not need any changes.
Class Household:
Household will have an instance variable which is an array of type Person called householdMembers, which holds all of the members of the household. Assume no household has more than 10 individuals. Household will have two integer instance variables, one is the total number of members in the house at any given time and the other is the maximum number of individuals allowed in the house, called totalMembers and maxMembers respectively. Household will have a default constructor that initializes the array to length 10 and sets maxMembers to 10 as well. Another constructor accepts a single integer which is the maximum number of individuals in the household, which initializes the array and sets maxMembers. Household will have a method called insertNewHouseholdMember, which accepts a single Person to add to the household. If the household is full, then the method must return false. Otherwise, the supplied Person must be added to the array at the lowest vacant index and totalMembers must be incremented. True must be returned in this case. Finally, Household has a toString method which prints the toString of each household member on a new line.
Class Student:
Student is another class that extends Person. In addition to the properties and methods inherited from Person, student has a String instance variable called major. Student must also have a single constructor that accepts the name of the student (as a String), and the major of the student (also as a String). Student has an override for the abstract method getSalary, and for a student this must simply return 0. Finally, the class Student has an override for the toString method, which returns toString of Person along with “\nMajor: “ and then the major of the student.
Sample output for Lab5Tester:
Total family income of Robert and Mary 75000.0
Description of the family is as follows:
Name is Teller, Edward
Married to Powell, Liz
Major: Physics
Name is Powell, Liz
Married to Teller, Edward
Major: Computer Science
Name is Porter, Tom
Major: Computer Science
Worker Number 1
Worker Name Hunter, Robert W.
Date Joined: October 23, 05
No Supervisor
Salary: $35000.0
Worker Number 2
Worker Name Hull, Mary J.
Date Joined: September 6, 07
No Supervisor
Salary: $40000.0

Answer:

Tester Class

public class Lab5Tester{
	public static void main(String[] args){
		NewWorker w1, w2;
		Student s1, s2, s3;
		Household myFamily;
		s1 = new Student("Edward Teller", "Physics");
		s2 = new Student("Liz Powell", "Computer Science");
		s3 = new Student("Tom Porter", "Computer Science");

		w1 = new NewWorker("Robert William Hunter", 
				"23/10/2005", 35000.00);
		w2 = new NewWorker("Mary Jane Hull", "06/09/2007");

		w2.setSalary(40000.00);

		myFamily = new Household(5);

		myFamily.insertNewHouseholdMember(s1);
		myFamily.insertNewHouseholdMember(s2);
		myFamily.insertNewHouseholdMember(s3);
		myFamily.insertNewHouseholdMember(w1);
		myFamily.insertNewHouseholdMember(w2);

		w1.setSpouse(w2);
		w2.setSpouse(w1);
		s1.setSpouse(s2);
		s2.setSpouse(s1);

		System.out.println("Total family income of Robert and Mary "
				+ w1.getFamilyIncome());
		System.out.println("Description of the family is as follows:\n"
				+ myFamily.toString());
	}
}

Household Class

public class Household {
	Person houseMembers [];
	private int totalMembers = 0;
	private int maxMembers;
	public Household(){
		Household householdMembers = new Household();
		householdMembers.houseMembers = new Person [10];
	}
	public Household (int numOfMem){
		maxMembers = numOfMem;
		houseMembers = new Person [maxMembers];
	}
	public void insertNewHouseholdMember (Person Member){
		if (totalMembers < maxMembers){
			houseMembers [totalMembers] = Member;
			totalMembers++;
		}

	}
	public String toString(){
		String thingy = new String();
		for (Person member:houseMembers){
			thingy +=  member.toString() + "\n";
		}
		return thingy;
	}
}

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 String getSupervisor(){
		if(Supervisor != null){
			return Supervisor.getName();
		}
		else {
			return null;
		}
	}
	
	public String toString(){
		String s;
		s = "Worker Number " + workerNumber + "\nWorker Name " + getName() 
		+ "\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 abstract class Person {
	private Name personName;
	private Person spouse = null;

	public abstract double getSalary();

	/*This constructor takes name string and sets it in the instance variable*/
	public Person (String fullName){
		personName = new Name (fullName);
	}

	/*This method takes another person as an argument*/
	public void setSpouse (Person spouse){
		this.spouse = spouse;
	}

	/*This method returns the person's name*/
	public String getName(){
		return personName.toString();
	}

	/*This method 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 " + this.getName() );
		else
			return ("Name is " + this.getName() + "\nMarried to " + spouse.getName());
	}

	/*This method 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 (){
		if (spouse!= null)
			return getSalary() + spouse.getSalary();
		else
			return getSalary();
	}
}

Student Class

public class Student extends Person  {
	private String major;
	public Student (String name, String major){
		super (name);
		this.major = major;
	}
	public double getSalary(){
		return 0.0;
	}
	public String toString(){
		return (super.toString() + "\nMajors: " + major+"\n\n");
	}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!