CSC 224: Program 2

Due: Third Week

Objective of this Program: (a) To write your first program in Java, (b) how to use decisions in a program, i.e., to distinguish a computer from a calculator, (c) getting input and output from a Java program, and (d) to learn more about the constructor, which is called each time a new object is created.

This programming assignment is an exercise in using the if...then...else construct as well as the switch construct, and the while construct. The overall program is to prompt the user (both computer science and drug dealers refer to their clients as "users") for the date of the month and whether they want the current day or the next day or the previous day. E.g., the user would see a prompt at the console window:


Enter todays date(1-30):
and after responding, (say with a '4')

Enter todays date(1-30): 4
the user would see another prompt in the console window:

Do you want today or next or previous day or quit the program(t/n/p/q):

The program should output the next day's or previous day's date: e.g, for the input of:


Enter todays date(1-30): 4

Do you want today or next or previous day or quit the program(t/n/p/q): n

Tomorrow is the 5th.
For the response of 'p' to the original question with the current date of 4, one would see:

Yesterday was the 3rd.
For the response of 't' to the original question with the current date of 4, one would see:
Today is the 4th.

For the response of 'q', one will quit the program.

Note that after a response of 'n' to the above question, the class of Date now has the current value of date to be 5. That is to say, the asking for the next or previous changes the value of the date in the object of the class.

You should also check for certain erroneous input. Specifically, three possible bad inputs: (1) the data is not between 1 and 30, inclusive, (2) one is asked the preceding date of the 1st, and (3) one is asked the next day of the 30th. If bad input is given, then each of them should have an appropriate message telling of the bad input, e.g.,


Enter todays date(1-30): 36

gets the response:

Please enter a date between 1 and 30.

and the program would ask again for a valid date. It would continue asking for a valid date until one was given (one between 1 and 30 inclusive).

While, the following:


Enter todays date(1-30): 30

Do you want today or next or previous day or quit the program(t/n/p/q): n

would generate:

There is no date after the 30th.

Similarly, for the previous day when the current day is 1, would generate

There is no date before the 1st.

You do not have to verify whether one correctly types in 't' or 'n' or 'p' correctly. I.e., if the user incorrectly types in the response, you are not responsible for what happens. However, it should give be case insensitive to the users response. I.e., it should recognize both 't' and 'T' as meaning 'today'.

The program will keep prompting the user with the

Do you want today or next or previous day or quit the program(t/n/p/q):

until the user responds with a q, quit the program. I.e., one will need some kind of a while loop as the control structure.

Implementation of Program 2

One should have two classes, one for the testing, and one for the implementation. All java programs should have at least two classes.

The Date Class

The Date class should have one member, the date as an integer, and four methods: (1) Date (which gets the date), (2) today (which outputs the current date) (3) next (which outputs the next date, and (4) previous (which outputs the previous date).

Its UML is:

	 _______________________
	|       Date		|
	|_______________________|
	|  - int date		|
	|  - Scanner console	|
	|_______________________|
	|  + Date()		|
	|  + today():void	|
	|  + previous():void	|
	|  + next():void	|
	|_______________________|

The method Date is called a constructor, because it has the same name as the class. When an object of the class is created, the constructor is called. E.g.,
Date testDate = new Date();
Here, we are creating (we say instantiating) a new object of the class Date, which we are calling 'testDate'. The use of 'Date' after the 'new' calls the constructor. If we do not explicitly implement a constructor, then a default constructor provided by the compiler will be called when we create an object of a class. However, most of the time, one usually wants to do something when one creates (instantiates) a new object of the class.

If the date given is not between 1 and 30, the Date constructor will do two things: (1) send an error message ("Only dates between 1 and 30 are accepted") and (2) ask to resubmit it. Keep on asking for the date until a valid date is submitted. The outline of how to do this is:

	public Date()
	{
		console = new Scanner(System.in);
		System.out.print("Enter today's date(1-30): ");
		date = console.nextInt();
		//if date is not valid (not between 1 and 30 inclusive)
		//ask to resubmit it until a valid date
	}
Notice there is no "void" after the public and before the Date. Constructors are the only methods which do not have a returned type before their name. That is how the compiler recognizes it is a constructor.

The methods next and previous are to handle the incorrect responses. I.e., if the user asks for the next date of '30', then the output message of "There is no date after the 30th" is generated within the method. Otherwise, next will first add one to the date and then output what the next date is (as described above) via System.out.println. Similarly, previous will first subtract one from the current value of date and then output the result. Notice that both next and previous will change the current value of date if it is legal. I.e., the value can not be less than 1 or more than 30.

The output for all 3 methods should handle the special cases of dates which end in 1, 2, or 3. I.e., 1st, 2nd, 3rd, 21st, 22nd, 23rd. Remember that 11th, 12th, 13th are the correct output.

Note: Since all three methods will need these endings, you might want to think of using a private method to handle this.

The ADT of the class Date can be found here.

The Testing class

You are to write the complete testing class. You can give any name that you want. It will have one method, the main which will be a static (class) method. It will create an object of the class Date and then ask the user if they want the next, previous, today's date, or quit the program. Depending on the response of the user, the appropriate method will be called. E.g., if the user asks for the next date, the next method will be called.

Thus, the testing class, should:

(1) create a object of the Date class

(2) asks the user (via Scanner) whether the user wants the present, next, or previous date

(3) calls either today, next, or previous depending on the input of (2).

(4) If in the input of (2), the user response quit, neither of the methods are called and the program will terminate.

I.e., the program continually asks the user for the present, next, or previous date until the user wants to quit. So you will have a loop, here, using the do...while construct.

Note:The input dialog box returns a String. To test what it is, it is easier if one extracts the first character of the String. Then one tests whether the first character is equal to 't' or 'n' or 'p' or 'q'. The testing of whether two char data types are equal is done with the "==" symbol. The code would look something like the following:

	char response;
	String input;

	System.out.print("Do you want to continue (Y/N)? ");
	input = console.next();
	response = input.charAt(0);

	if (response == 'y' || response == 'Y')
		//response was yes

The Scanner Class

The Scanner class was added to Java in version 1.5 to handle input from the console window. Until 1.5, there was no way to get input directly from the console window in Java. I.e., the user had to write his own class to convert strings to other basic Java data types (int, double, etc.). The Scanner class resolves this deficiency.

To use the Scanner class, you must import it as it does not reside in the standard java.lang package. It resides in java.util.Scanner, so you must have something like

import java.util.*;

before your class definition. You create an object of the class by

Scanner console = new Scanner(System.in);

which tells it that the input will be from the console window. If you have the following code:

System.out.print("Type in something: ");

String input = console.next();

Then the console will keep reading until the next white space, which in Java is either a blank line, a tab, or a line separator. If you want to read until the end of the line, you must set the line separator as the delimiter:

String lineSeparator = System.getProperty("line.separator");
console.useDelimiter(lineSeparator);

Then the next use of console.next() will read until a line separator is found. There is also the method of console.nextLine(), which will read in characters until the line-feed character is found. This is the same as the above example.

To read in numbers, e.g., int, one uses the method of nextInt of Scanner:

System.out.print("Enter your Age: ");
int age = console.nextInt();
will read in char-by-char until a non-integer char is found.

Note that you can read in more than one integer value on a line:

System.out.print("Enter your month and year of birth: ");

and the user types in 5 and 1978. I.e., the console window looks like:

Enter your month and year of birth: 5 1978

can be read in via the following two lines of code:

int month = console.nextInt();
Int year = console.nextInt();
There is also a nextDouble to read in data of type double, etc. To learn more about the Scanner class, go to the link of the API of Java given on the homepage and go to Index, then S, and finally Scanner.

Note that the Scanner class can also be used with files to read in text files.

Extra Credit

For the extra credit, you can make your calendar go up to a year, rather than just a month. You will add a new member to the class month:
private int month;
It will as initialized in the constructor as the date in the regular assignment.

Now, when one reaches the end of the month, for example, the 30th day of the 6th month (June 30), when one asks for next, one would change the date to the value of 1 and the month to the value of 7. (July 1). The outputshould be like

Tomorrow is the 1st of July

If one gets to the 31st of December, then the asking of next should give an error message

There is no date after the 31st of December

Same error message for previous when it is the 1st of January.

Note:: You will have to use arrays to do this. In fact, you will need two arrays, one of ints and one of String.

You can decide whether you are doing a leap year or not.

What to Hand-in

You should post your project as a 'zip' file on the COL website. It should be posted under the appropriate assignment. Also indicate how many hours you worked on the program in the Readme.txt file. You should submit 3 files in the 'zip' file. The two .java files and the Readme.txt file.


Homepage of CTI School back to 211 homepage