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:
Defining Classes Ii
Exercise:
Programming Projects
Question:9 | ISBN:9780132830317 | Edition: 5

Question

Use javadoc to generate HTML documentation for the code in Display 5.19. Use the @author and @version tag for the description of the entire class. Add a comment for every public method or constructor using the @param and @return tags when appropriate.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Refer Code Display 5.19  in text book.

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

	public Person(String initName, Date bdayDate, Date deathDate)
	{
		if (consistent(bdayDate, deathDate))
		{
			name = initName;
			born = new Date(bdayDate);
			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);
	}

	public void set(String nameNew, Date bdayDate, Date deathDate)
	{
		if (consistent(bdayDate, deathDate))
		{
			name = nameNew;
			born = new Date(bdayDate);
			if (deathDate == null)
				died = null;
			else
				died = new Date(deathDate);
		} else 
		{
			System.out.println("Inconsistent dates. Aborting.");
			System.exit(0);
		}
	}

	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 setbdayDate(Date newDate)
	{
		if (consistent(newDate, died))
			born = new Date(newDate);
		else
		{
			System.out.println("Inconsistent dates. Aborting.");
			System.exit(0);
		}
	}

	public void setDeathDate(Date newDate) 
	{

		if (!consistent(born, newDate))
		{
			System.out.println("Inconsistent dates. Aborting.");
			System.exit(0);
		}

		if (newDate == null)
			died = null;
		else
			died = new Date(newDate);
	}

	public void setName(String nameNew)
	{
		name = nameNew;
	}

	public void setBirthYear(int newYear)
	{
		if (born == null) 
		{
			System.out.println("Fata; Error. Aborting.");
			System.exit(0);
		}
		born.setYear(newYear);
		if (!consistent(born, died))
		{
			System.out.println("Inconsistent dates. Aborting.");
			System.exit(0);
		}
	}

	public void setDeathYear(int newYear) 
	{
		if (died == null) 
		{
			System.out.println("Fata; Error. Aborting.");
			System.exit(0);
		}
		died.setYear(newYear);
		if (!consistent(born, died)) {
			System.out.println("Inconsistent dates.Aborting.");
			System.exit(0);
		}
	}

	public String getName() {
		return name;
	}

	public Date getbdayDate() {
		return new Date(born);
	}

	public Date getDeathDate() {
		if (died == null)
			return null;
		else
			return new Date(died);
	}

	private static boolean consistent(Date bdayDate, Date deathDate) {
		if (bdayDate == null)
			return false;
		else if (deathDate == null)
			return true;
		else
			return (bdayDate.precedes(deathDate) || bdayDate.equals(deathDate));
	}
}

OUTPUT:

 

0 0

Discussions

Post the discussion to improve the above solution.