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 I
Exercise:
Programming Projects
Question:8 | ISBN:9780132830317 | Edition: 5

Question

Redefine the class Date in Display 4.13 so that the instance variable for the month is of type int instead of type String. None of the method headings should change in any way. In particular, no String type parameters should change to int type parameters. You must redefine the methods to make things work out. Any program that uses the Date class from Display 4.13 should be able to use your Date class without any changes in the program. In particular, the program in Display 4.14 should work the same whether the Date class is defined as in Display 4.13 or is defined as you do it for this project. Write a test program (or programs) that tests each method in your class definition.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;

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

	public Date() 
	{
		month = 1;
		day = 1;
		year = 1000;
	}

	public Date(int mmInteger, int day, int year) 
	{
		setDate(mmInteger, day, year);
	}

	public Date(String mmString, int day, int year) 
	{
		setDate(mmString, day, year);
	}

	public Date(int year) 
	{
		setDate(1, 1, year);
	}

	public Date(Date dd) 
	{
		if (dd == null) 
		{
			System.out.println("found Error.");
			System.exit(0);
		}

		month = dd.month;
		day = dd.day;
		year = dd.year;
	}

	public void setDate(int mmInteger, int day, int year) 
	{

		if (dateConform(mmInteger, day, year)) 
		{
			this.month = mmInteger;
			this.day = day;
			this.year = year;
		} else {
			System.out.println("Fatal Error");
			System.exit(0);
		}
	}

	public void setDate(String mmString, int day, int year) 
	{

		if (dateConform(mmString, day, year)) 
		{
			this.month = mmInteger(mmString);
			this.day = day;
			this.year = year;
		} else {
			System.out.println("found Error");
			System.exit(0);
		}
	}

	public void setDate(int year) 
	{
		setDate(1, 1, year);
	}

	public void setYear(int year) 
	{

		if (year < 1000 || year > 9999) {
			System.out.println("Error");
			System.exit(0);
		} else
			this.year = year;
	}

	public void setMonth(int monthNum) 
	{

		if (monthNum <= 0 || monthNum > 12) 
		{
			System.out.println("Error");
			System.exit(0);
		} else
			month = monthNum;
	}

	public void setDay(int day) 
	{

		if (day <= 0 || day > 31)
		{
			System.out.println("Error");
			System.exit(0);
		} else
			this.day = day;
	}

	public int getMonth() 
	{
		return month;
	}

	public int getDay() 
	{
		return day;
	}

	public int getYear() 
	{
		return year;
	}

	public String toString() 
	{
		return mmString(month) + " " + day + ", " + year;
	}

	public boolean equals(Date ddMatch)
	{
		return (month == ddMatch.month) && (day == ddMatch.day) && (year == ddMatch.year);
	}

	public boolean precedes(Date ddMatch) 
	{
		return (year < ddMatch.year) || (year == ddMatch.year && month < ddMatch.month)
				|| (year == ddMatch.year && month < ddMatch.month && day < ddMatch.day);
	}

	public void readInput() 
	{
		boolean choice = true;
		Scanner scn = new Scanner(System.in);
		while (choice) {
			System.out.println("Enter a month number, day, and year.");
			System.out.println("Separate each by space.");
			int mm = scn.nextInt();
			int dd = scn.nextInt();
			int yy = scn.nextInt();
			if (dateConform(mm, dd, yy)) 
			{
				setDate(mm, dd, yy);
				choice = false;
			} else
				System.out.println("Wrong enter, re-enter input.");
		}
	}

	private boolean dateConform(int mmInteger, int dayInt, int yearInt)
	{
		return mmInteger >= 1 && mmInteger <= 12 && dayInt >= 1 && dayInt <= 31 && yearInt >= 1000 && yearInt <= 9999;
	}

	private boolean dateConform(String mmString, int dayInt, int yearInt)
	{
		return monthOK(mmString) && dayInt >= 1 && dayInt <= 31 && yearInt >= 1000 && yearInt <= 9999;
	}

	private boolean monthOK(String mmString) 
	{

		return mmString.equals("January") || mmString.equals("February") || mmString.equals("March")
				|| mmString.equals("April") || mmString.equals("May") || mmString.equals("June")
				|| mmString.equals("July") || mmString.equals("August") || mmString.equals("September")
				|| mmString.equals("October") || mmString.equals("November") || mmString.equals("December");
	}

	private String mmString(int monthNum) 
	{
		switch (monthNum)
		{
		case 1:
			return "January";
		case 2:
			return "February";
		case 3:
			return "March";
		case 4:
			return "April";
		case 5:
			return "May";
		case 6:
			return "June";
		case 7:
			return "July";
		case 8:
			return "August";
		case 9:
			return "September";
		case 10:
			return "October";
		case 11:
			return "November";
		case 12:
			return "December";
		default:
			System.out.println("Fatal Error");
			System.exit(0);
			return "Error";
		}
	}

	private int mmInteger(String mmString) 
	{
		if (mmString.equals("January"))
			return 1;
		else if (mmString.equals("February"))
			return 2;
		else if (mmString.equals("March"))
			return 3;
		else if (mmString.equals("April"))
			return 4;
		else if (mmString.equals("May"))
			return 5;
		else if (mmString.equals("June"))
			return 6;
		else if (mmString.equals("July"))
			return 7;
		else if (mmString.equals("August"))
			return 8;
		else if (mmString.equals("September"))
			return 9;
		else if (mmString.equals("October"))
			return 10;
		else if (mmString.equals("November"))
			return 11;
		else if (mmString.equals("December"))
			return 12;
		else
			return -1;
	}

	public static void main(String args[]) 
	{


		Date d1 = new Date("October", 17, 2016);
		Date d2 = new Date(); 
		Date d3 = new Date(2016);
		Date d4 = new Date(10, 11, 1963);
		System.out.println(d1.toString());
		System.out.println(d2.toString());
		System.out.println(d3.toString());
		System.out.println(d4.toString());
		System.out.println();
		System.out.println("Date 1's month: " + d1.getMonth());
		System.out.println("Date 1's day: " + d1.getDay());
		System.out.println("Date 1's year: " + d1.getYear());
		d1.setDate(10, 17, 2016);
		System.out.println("After setting new date:");
		System.out.println("Date 1's month: " + d1.getMonth());
		System.out.println("Date 1's day: " + d1.getDay());
		System.out.println("Date 1's year: " + d1.getYear());
		System.out.println();
		
		System.out.println("Both dates are equal:"+d2.precedes(d1));

		System.out.println("Both dates are equal :"+d1.equals(d3));

	}

}

Result Output:

October 17, 2016
January 1, 1000
January 1, 2016
October 11, 1963

Date 1's month: 10
Date 1's day: 17
Date 1's year: 2016
After setting new date:
Date 1's month: 10
Date 1's day: 17
Date 1's year: 2016

Both dates are equal:true
Both dates are equal :false

 

 

0 0

Discussions

Post the discussion to improve the above solution.