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

TestWorker class and Name class using java

you are going to create two classes. The first class is called Name, which will be defined as below. The second class will be called TestWorker, which will also be defined below. TestWorker will give you practice calling a constructor.
Definition of Class “Name”
The Name class will have three strings as instance variables: firstName, middleName, and lastName. The class will contain a constructor taking a single string, space separated, which may contain two or three tokens. If it is only two tokens, the tokens are the first and last name. If it is three tokens, the tokens are the first, middle, and last name. Name must also have a copy constructor, which (of course) accepts a Name argument to copy. Name has a method setName, which accepts a new first name, middle name, and last name as strings. Finally, the toString method of name returns the name in the format “LastName, FirstName M.” or “LastName, FirstName”. For instance, “Scott, Ryan D.” or “Scott, Ryan” are valid.
Definition of Class “TestWorker”
The TestWorker class simply has a main method in it. In main, TestWorker will create three worker objects, w1, w2, and w3. The variable w1 will be set to a worker named “Robert William Hunter”, starting on “23/10/2005”, making a salary of 35000. The variable w2 will be set to a worker named “John Smith”, starting on “15/11/2005”, making 25000. Lastly, w3 will be set to a worker named “Mary Jane Hull”, starting on “06/09/2007” and making 20000.
After creating the three objects, the salary of worker w2 will be set to 20000. The supervisor of w2 and w3 will be set to w1. Print the line “Number of workers = ” and then display how many workers have been created (see the static variable in Worker, and its use). Print on another line “Supervisor of John is ” followed by the supervisor of w2. Lastly, print descriptions of each of the workers, using their toString methods

Answer:

MyDate class

import java.util.*;

public class MyDate {
	private int day, month, year; 
	
   /*
	* This constructor accepts a string, in the format “DD/MM/YYYY”, 
	* that initializes the day, month, and year instance variables.
	*/
	public MyDate(String Date){
		StringTokenizer myTokens = new StringTokenizer(Date,"/");
		this.day = Integer.parseInt(myTokens.nextToken());
		this.month = Integer.parseInt(myTokens.nextToken()); 
		this.year = Integer.parseInt(myTokens.nextToken());
	}
	
	
   /*
	* This copy constructor accepts another MyDate object as its argument,
	* performing a deep copy of it.
	*/
	public MyDate(MyDate aDate){
		day = aDate.day; 
		month = aDate.month; 
		year = aDate.year; 
	}
	
   /*
	* This is a default (no-argument) constructor that initializes the date to 00/00/0000
	*/
	public MyDate(){
		String day = "00"; 
		String month = "00"; 
		String year = "0000"; 
		System.out.println(day+"/"+month+"/"+year);
	}
	
   /*
	* toString method of MyDate returns a string giving the month
	* name, followed by the day, followed by a comma, followed by
	* the last two digits of the year
	*/
	public String toString(){
		String monthName = ""; 
		int lastDigits = year % 100; 
		if(month == 1){monthName = "January"; }
		if(month == 2){monthName = "February"; }
		if(month == 3){monthName = "March"; }
		if(month == 4){monthName = "April"; }
		if(month == 5){monthName = "May"; }
		if(month == 6){monthName = "June";}
		if(month == 7){monthName = "July";}
		if(month == 8){monthName = "August"; }
		if(month == 9){monthName = "September"; }
		if(month == 10){monthName = "October";}
		if(month == 11){monthName = "November";}
		if(month == 12){monthName = "December";}
		
		if(lastDigits < 10){
        return day + " " + monthName  +  ",0" + lastDigits;  
        }
        return day + " " + monthName  +  "," + lastDigits;
	}
	
	/*
	 * This method called lessThan, which accepts another MyDate object,
	 * and compares the argument to the current object. If the current 
	 * date is less than the given date, it returns true. Else, it returns false.
	*/
	public boolean lessThan(MyDate d){
		if (year < d.year || (year == d.year && month < d.month) || (year ==d.year && month == d.month && day < d.day))
			return true;
		else
			return false;
	}
	
	/*
	 * This method called equals method which accepts another MyDate object, and compares
	 * the argument to the current object to see if all of their values are the same.
	 */
	public boolean equals (MyDate d){
		if (year == d.year && month == d.month && day == d.day)
			return true;
		else
			return false;
	}
	
	
}

Name class

import java.util.*;

public class Name {
	private String firstName;
	private String middleName;
	private String lastName; 
	
	/*
	This constructor is taking a single string, space separated, which may
	contain two or three tokens. If it is only two tokens, the tokens are
	the first and last name. If it is three tokens, the tokens are the first, 
	middle, and last name
	*/
	public Name(String FullName){
		
		StringTokenizer myTokens = new StringTokenizer(FullName," ");
		firstName = myTokens.nextToken();
		
		/* Check if we have only first and last name */
		if(myTokens.countTokens() == 2){
			middleName =  myTokens.nextToken();
			lastName = myTokens.nextToken(); 
		}
		
		/* If we have full name first,middle and last name */
		else{
			lastName =  myTokens.nextToken();
		}
	}

	/*
	This copy constructor which accepts a Name argument to copy
	*/
	public Name(Name aName){
		
		firstName = aName.firstName; 
		middleName = aName.middleName;  
		lastName = aName.lastName; 
	}
	
	/* Default constructor */
	public Name(){
		
	}
	
	/*
	This method is called setName, which accepts a new first name,
	middle name, and last name as strings
	*/
	public void setName (String fName, String mName, String lName){
		this.firstName = fName;
		this.middleName = mName;
		this.lastName = lName;
	}
	
	/*
	This method is called toString method returns the name in the format
	“LastName, FirstName M.” or “LastName, FirstName”.
	*/
	public String toString(){
		
		if(middleName != null){
			return lastName + ", " + firstName + " " + middleName.charAt(0) + "." ;
		}
		else{
			return lastName + ", " + firstName + " " ;
		}
	}
	
}

TestWorker class

public class TestWorker {

	public static void main(String[] args) {
		
		Worker w1, w2, w3;
		w1 = new Worker ("Robert  William Hunter", "23/10/2005", 35000.00);
		w2 = new Worker ("John Smith", "15/11/2005", 25000.00);
		w3 = new Worker ("Mary Jane Hull", "06/09/2007",20000);
		w2. setSalary(20000.00);
		w2.setSupervisor(w1);
		w3.setSupervisor(w1);
		System.out.println("Number of workers = " + Worker.getHowManyWorkers() +" \n");
		System.out.println("Supervisor of John is " + w2.getSupervisorName());
		System.out.println(w1.toString()+" \n");
		System.out.println(w2.toString()+" \n");
		System.out.println(w3.toString()+" \n");
	
	}	

}

Worker class

public class Worker {
	
	private static int howManyWorkers = 0;
	
	private int workerNumber;
	private Name workerName;
	private MyDate dateJoiningCompany;
	private double salary;
	private Worker supervisor;
	
	// Sets name, date joined company, and salary
	public Worker(String workerName, String dateJoiningCompany, double salary) {
		this.workerName = new Name(workerName);
		this.dateJoiningCompany = new MyDate(dateJoiningCompany);
		this.salary = salary;
		
		// Keep track of how many workers there have been
		// and give the worker a number
		howManyWorkers++;
		workerNumber = howManyWorkers;
	}
	
	// Sets name, date joined company
	// salary defaults to 0.0
	public Worker(String workerName, String dateJoiningCompany) {
		this(workerName, dateJoiningCompany, 0.0);
	}
	
	// Copy constructor
	public Worker(Worker worker) {
		workerNumber = worker.workerNumber;
		workerName = new Name(worker.workerName);
		dateJoiningCompany = new MyDate(worker.dateJoiningCompany);
		salary = worker.salary;
		
		// No deep copy for supervisor
		// This is useful, for example, if supervisor changes
		// their name, salary, or worker number
		supervisor = worker.supervisor;
	}
	
	// Set the salary of the worker
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	// Set the supervisor of the worker
	public void setSupervisor(Worker supervisor) {
		this.supervisor = supervisor;
	}
	
	// Get how many workers have been created
	public static int getHowManyWorkers() {
		return howManyWorkers;
	}
	
	// Return a new copy of the supervisor's name, if supervisor exists
	public Name getSupervisorName() {
		if(supervisor == null) {
			return null;
		} else {
			return new Name(supervisor.workerName);	
		}		
	}
	
	// Return a description containing:
	// worker number, worker name (toString()),
	// date joined (toString()), supervisor name (toString()),
	// salary
	public String toString() {
		String desc = "";
		
		desc += "Number:     "     + workerNumber                  + "\n";
		desc += "Name:       "     + workerName.toString()         + "\n";
		desc += "Joined:     "     + dateJoiningCompany.toString() + "\n";
		desc += "Supervisor: " + ((supervisor == null) ?
				"<NO SUPERVISOR>" : getSupervisorName())           + "\n";
		desc += "Salary:     "     + salary                        + "\n";
		
		return desc;
	}
	
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!