SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Interfaces And Inner Classes
Exercise:
Programming Projects
Question:8 | ISBN:9780132830317 | Edition: 5

Question

This is a combination of Programming Projects 13.6 and 13.7. Redo the class Person in Display 5.19 so that the class Date is a private inner class of the class Person, and so that the class Person implements the Cloneable interface. Also, do a suitable test program.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//Use Programming Projects 13.6 and 13.7
//for method implementations and 
//Redo the class Person in Display 5.19
import java.io.*;
import java.util.*;

public class Person implements Cloneable {
	private String name;
	private Date born;
	private Date died;

	private class Date {

public Date()

public Date(int monthInt, int day, int year)

public Date(int year)

public Date(Date aDate)

public void setDate(int monthInt, int day, int year)

public void setDate(String monthString, int day, +int year)

public void setDate(int year)

public void setYear(int year)

public void setMonth(int monthNumber)

public void setDay(int day)

public int getMonth()

public int getDay()

public int getYear()

public String toString()

public boolean equals(Date otherDate)

public boolean precedes(Date otherDate)

public void readInput()

private boolean dateOK(int monthInt, int dayInt, 
int yearInt)

private boolean dateOK(String monthString, int dayInt, int yearInt)

private boolean monthOK(String month)

private String monthString(int monthNumber)
	}
	public Person(String initName)
	{
		Date birthDate;
		Date deathDate;
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter birth date information: ");
		System.out.print("Month (number):");
		int month = scan.nextInt();
		System.out.print("Day:");
		int day = scan.nextInt();
		System.out.print("Year:");
		int year = scan.nextInt();
		birthDate = new Date(month, day, year);
		scan.nextLine();
		System.out.println("Is person still alive? (Y/N)");
		String alive = scan.nextLine();
		if(!alive.equalsIgnoreCase("y"))
		{
			System.out.println("Enter death date information: ");
			System.out.print("Month (number):");
			month = scan.nextInt();
			System.out.print("Day:");
			day = scan.nextInt();
			System.out.print("Year:");
			year = scan.nextInt();
			deathDate = new Date(month, day, year);
			scan.nextLine();
		}
		else
			deathDate = null;
		if(consistent(birthDate, deathDate))
		{
			name = initName;
			born = new Date(birthDate);
			if(deathDate == null)
				died = null;
			else
				died = new Date(deathDate);
		}
		else
		{
			System.out.println("Inconsistent dates. Aborting.");
			System.exit(0);
		}
	}
	public Person(Person original)
	{
		if(original == null)
		{
			System.out.println("Fatal error.");
			System.exit(0);
		}
		
		name = original.name;
		born = new Date(original.born);
		if(original.died == null)
			died = null;
		else
			died = new Date(original.died);
	}//end of person class
	public void set(String newName)
	{
		name = newName;
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter birth date data: ");
		System.out.print("Month (number):");
		int month = scan.nextInt();
		System.out.print("Day:");
		int day = scan.nextInt();
		System.out.print("Year:");
		int year = scan.nextInt();
		Date birthDate = new Date(month, day, year);
		scan.nextLine();
		System.out.println("Is person still alive? (Y/N)");
		String alive = scan.nextLine();
		Date deathDate;
		if(!alive.equalsIgnoreCase("y"))
		{
			System.out.println("Enter death date data: ");
			System.out.print("Month (number):");
			month = scan.nextInt();
			System.out.print("Day:");
			day = scan.nextInt();
			System.out.print("Year:");
			year = scan.nextInt();
			deathDate = new Date(month, day, year);
			scan.nextLine();
		}
		else
			deathDate = null;

		if(!consistent(birthDate, deathDate))
		{
			System.out.println("Inconsistent dates.");
			System.exit(0);
		}
		
		born = new Date(birthDate);
		if(deathDate == null)
			died = null;
		else
			died = new Date(deathDate);
	}
	public String toString()
	{
		String diedString;
		if(died == null)
			diedString = "";
		else
			diedString = died.toString();
		
		return name + ", " + born + "-" + diedString;
	}
	public boolean equals(Person otherPerson)
	{
		if(otherPerson == null)
			return false;
		else
			return (name.equals(otherPerson.name) &&
					born.equals(otherPerson.born) &&
					datesMatch(died, otherPerson.died));
	}
	private static boolean datesMatch(Date date1,Date date2)
				{
					if(date1 == null)
						return (date2 == null);
					else if(date2 == null)
						return false;
					else
						return date1.equals(date2);
				}

	public void setBirthDate()
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter birth date data: ");
		System.out.print("Month (number):");
		int month = scan.nextInt();
		System.out.print("Day:");
		int day = scan.nextInt();
		System.out.print("Year:");
		int year = scan.nextInt();
		Date newDate = new Date(month, day, year);
		scan.nextLine();

		if(!consistent(newDate, died))
		{
System.out.println("Inconsistent dates.");
			System.exit(0);
		}
		else
			born = new Date(newDate);
	}
	public void setDeathDate()
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("Is person still alive? (Y/N)");
		String alive = scan.nextLine();
		Date newDate;
		if(!alive.equalsIgnoreCase("y"))
		{
			System.out.println("Enter death date data: ");
								System.out.print("Month (number):");
								int month = scan.nextInt();
								System.out.print("Day:");
								int day = scan.nextInt();
								System.out.print("Year:");
								int year = scan.nextInt();
								newDate = new Date(month, day, year);
								scan.nextLine();
							}
							else
								newDate = null;
		if(!consistent(born, newDate))
		{
			System.out.println("Inconsistent dates.");
			System.exit(0);
		}
		else
			died = new Date(newDate);
	}

	public void setName(String newName)
	{
		name = newName;
	}
	public void setBirthYear(int newYear)
	{
		if (died == null)
		{
			System.out.println("Fatal error.");
			System.exit(0);
		}
		born.setYear(newYear);
		
		if(!consistent(born, died))
		{
			System.out.println("Inconsistent dates.");
			System.exit(0);
		}
	}
	public void setDeathYear(int newYear)
	{
		if (died == null)
		{
			System.out.println("Fatal error.");
			System.exit(0);
		}
		died.setYear(newYear);
		
		if(!consistent(born, died))
		{
			System.out.println("Inconsistent dates.");
			System.exit(0);
		}
	}
	public String getName()
	{
		return name;
	}
	
	public Date getBirthDate()
	{
		return new Date(born);
	}
	
	public Date getDeathDate()
	{
		if(died == null)
			return null;
		else
			return new Date(died);
	}
	private static boolean consistent(Date birthDate, Date deathDate)
				{
					if(birthDate == null)
						return false;
					else if(deathDate == null)
						return true;
					else
						return (birthDate.precedes(deathDate) || birthDate.equals(deathDate));
				}
				
	public Person clone()
	{
		return new Person(this);
	}
}
class Demo
{
	public static void main(String args[])
{
		Person person = new Person("CHUNKY");
		System.out.println("Setting new death date:");
		person.setDeathDate();
		System.out.println(person.getDeathDate());
		System.out.println("Using set method:");
		person.set("Terminator");
		System.out.println(person);
		System.out.println("---");
		Person anotherPerson = person.clone();
		anotherPerson.set("Joker");
		System.out.println("---");
		System.out.println(person);
		System.out.println(anotherPerson);
		
	}
}

 

0 0

Discussions

Post the discussion to improve the above solution.