Create objects of that class type, using its various constructor methods. Do something with the objects,
Question:
Create objects of that class type, using its various constructor methods.
• Do “something” with the objects, by exercising their various API methods.
• Display the objects before and after, by using their available display methods.
• Make sure you understand if the objects are behaving as described by their API.
Then, in your CS12DateClientFL.java code, do the following:
f) Set up one Scanner object for user inputs. See the example given in the Scanner app note and
its sample code. When you need a month, day, or year, the needed code will look something
like this:
import java.util.Scanner; // this one line appears BEFORE the public class... statement
Scanner input = new Scanner(System.in); // done ONCE, inside your main()
g) Upfront, declare only (for now) the following “scratch” (reusable) data in your program:
• 4 CS12Date variables (objects): today, myBday, halloween, christmas;
• 3 int variables month, day, and year
You will reuse these variables/objects to address the various program tasks.
Just declare these for now, no need to instantiate them just yet. I prefer to see you instantiate
each of these dates problem-by-problem, as the program spec calls for them.
h) Use code such as this for EACH month, day, or year to be read for the upcoming steps:
System.out.print(“Enter month for... > “);
month = input.nextInt(); // use existing declared variables for each M/D/Y input
Requirements:
In your CS12DateClientFL.java code, do each of the following. For each problem:
• Use the specified constructor form and/or method! Different
constructor forms and/or methods are specified for various problems.
• Make sure all of your outputs are clearly labeled with the problem number
• Don’t cram all outputs together, give yourself some blank space between outputs
• From here on: please provide overall commenting (narrative) of your program’s logic flow.
o Tell the reader what each problem does, beyond just the simple problem #.
o Use summary before-comments, NOT end-comments on each and every line!
1) Using the CS12Date default constructor, instantiate a new date object today with its default
values (today’s date). Print out this date object explicitly using its toString() method with
System.out.println(), AND also implicitly, using System.out.println() with just the name of the
date object.
Both dates should print identically, but implicit (object name only) is simpler and always the
way to go.
2) Print today one more time, except this time use its print() method. Use the overloaded print
form, and provide it some descriptive labeling message.
3) Using one of the CS12Date month-day-year constructor forms, instantiate a new date object
called halloween (note the initial lower case convention), and set its date to that of Halloween
this year (October 31st). Print the date using whatever means you wish.
4) Using the CS12Date month-day constructor form, instantiate a new date object called myBday
to November 1st of this year. (THIS SHOULD REALLY BE NOV 1st, NOT YOUR BIRTHDAY. Yeah,
this probably isn’t right, but you’ll update it later). Print this date using whatever means you
wish. DO NOT use your own birthdate in this step, we’ll update it later.
5) Using the CS12Date default constructor, instantiate a new default date object called christmas
(note the initial lower case convention), then update it from the default date to the date of this
coming Christmas using the setDate() method. This represents 2 separate operations. Print the
date using whatever means you wish.
6) Advance the step 4 date myBday by 3 days, by using the nextDate() method 3x. Print the new
date using whatever means you wish.
Then advance the date myBday by one more week, using the laterDate() method, and print the
new date using whatever means you wish.
7) Prompt the user for the month, day and year of YOUR birthday this year (3 prompts) using
Scanner, as described above in (e) and using the variables declared in (f). Then, using all 3 of
the individual mutator (set) methods, and the 3 int vars just read in, update current date of
myBday to the date of your birthday this year. Print the date using whatever means you wish.
8) On what date number of the year (1-365 or 1-366) is/was this year’s birthday? Use the myBday
date and getDateNum() to find the result, then print both your birthday AND this ordinal date
together as a one-line String output. For this problem, try and inline the needed expression
directly into your print statement!
9) Print both today’s date and your birthday from #7, and check whether today is your birthday (if
it is, Happy Birthday!). Use either date object (today or myBday) and its equals() method,
compare it to the other date using equals(), and print out the T/F verdict. For this problem, try
and inline the needed expression directly into your print statement!
Since all Java objects are ultimately descended from type Object, use the other CS12Date object
for the input parameter Object obj. See the example code in CS12DateUsage.java on Canvas for
an example of this if needed.
10) Using the today and myBday dates, compare one date to the other one using the compare()
method, using both orderings (see the sample outputs). Print the results. For this problem, try
and inline the needed expressions directly into your print statements!
Then, also compare today to itself, and print the result. So there should be 3 results in all.
The compare() method tells us the relative ordering of two CS12Date dates:
• compare() > 0: input CS12Date is in future relative to CS12Date object’s date
• compare() < 0: input CS12Date is in past relative to CS12Date object’s date
• compare() = 0: both CS12Dates are the same
See the API for details. You may need to use compare() on a future assignment!
11) Before proceeding, reset the following individual integer variables: month=0, day=0, year=0.
Reset the 3 local int variables only, not by using the 3 mutator methods.
Then, using the 3 individual accessor (get) methods, reprint the date of this year’s birthday in
myBday, except print it in “dashes” form, MM-DD-YYYY. You will need to use string
concatenation. Can you inline the needed 3 expressions directly into the print statement??
Print the new date as a String using println().
Note that you aren’t “altering” the original myBday, you are just taking it apart using the
accessors, and “reassembling” it to create a new String, which you will then print using System.out.println().
The code should prin
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang