Lab 2
Lab 2
Lab 2
1 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
The third command (which we also used in lab1) makes lab2 your current directory.
Lab pairs
Labs will generally be done working in partnerships (2 people). This is a version of the programming
technique called Pair Programming. The pair of you will sit at one workstation. One person will be the
designated driver, doing the typing and mousing to use the tools, and the other will be the scribe, writing
down the answers to lab questions. In subsequent labs you will switch off roles. This does not mean only one
person is doing the work. In pair programming both members of the team are active participants in solving
the problem, discussing as you go, but with each with a designated role. If one of you has more programming
experience, we recommend the lesser experienced of you is the driver this time.
The questions to be answered by the scribe are in bold and labeled Question 1.1, 1.2, etc. You could put the
answers into a README text file that you edit (which would be easier at a neighboring computer), but if it's
more convenient, you could, alternatively, write down the answers on paper. To keep your work organized, it
may be helpful to use a lab notebook for this purpose. Wherever the lab mentions the README, we mean a
text file or on-paper lab write-up.
10/6/2015 6:39 PM
Lab 2 CS 455
2 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
DEN students
DEN students will generally not be working in pairs since they are working remotely and away from other
students in the class. DEN students will need to electronically submit their labs, so their answers to the lab
questions will go in the README file, rather than on paper. Each lab includes instructions about what DEN
students need to submit, and how (for example, see the end of this lab).
Look up GregorianCalendar in Java API (we're using the 1.7 version of the API this semester, linked
above). Use it to help you answer the following questions about the GregorianCalendar class.
Question 1.0 In the README file (or lab write-up), put the first and last names and the roles (driver/scribe)
of each of the partners, the course name and lab number.
Question 1.1 What import statement is necessary to use the GregorianCalendar class?
Question 1.2 Use this documentation to figure out how to write a statement to create a GregorianCalendar
for the date January 20, 1995. Write down the statement in your README. Hint: look at the constructors.
Question 1.3 Write a statement (or statements) to use the get method to print out the the date (using a month
number rather than a name); don't worry too much about formatting. The documentation shows examples of
using the get method.
Expressions such as Calendar.MONTH bear a little explanation here. They are constants related to calendars.
They use the dot notation, because in Java, named constants are normally nested within classes. (And for
constants it's the name of a class, not an object, that is placed before the dot.) The class in this case is
Calendar which is the "superclass" of the GregorianCalendar class -- we'll be discussing more about such
10/6/2015 6:39 PM
Lab 2 CS 455
3 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
The get method itself is also from the Calendar class. We can use it on a GregorianCalendar, because a
subclass inherits all the stuff defined in its superclass. That's why get's documentation doesn't appear on the
GregorianCalendar page: it's on the Calendar page instead.
Question 1.4 Look up the detailed documentation for Calendar.MONTH. What is the the number we associate
with the MONTH field code to indicate January?
Hint: you can get to the detailed documentation for this constant by following the appropriate link under the
"Fields inherited from java.util.Calendar" heading (right after the "Field Summary").
Question 1.5 Once you've created your date object, if you wanted to change the date to one 20 days later,
what method would you use? Write down the method call to accomplish this. (We're looking for something
that would work no matter what the old value of the date was.)
Try using copy and paste in emacs to copy the code you use to print out the date (i.e., because you need to
use the code twice to get the results above).
Reminder of how to copy a file. If you want to start from the Hello program we started with in the first lab,
and then modify it to create this program you can use the Unix, cp command--the format is:
cp fromFile toFile
Or, alternatively, if you are editing a Date.java file in emacs, use the emacs command
C-x i
to insert the contents of another file into to the file you are already editing (it will prompt for the file to
insert). The Hello program should be in your lab1 directory; otherwise, it is also in: ~csci455/labs
/lab1/Hello.java.
Remember that the name of the main class has to match the file prefix.
Common compile error. One common compile error is "cannot find symbol". When you get this message
pay close attention to what part of your code it is flagging and what it is complaining about. Some possible
causes of this error:
you didn't import the right class or package that has this symbol.
you changed the name of your variable, method, or class but you are still using the old name in a few
places.
10/6/2015 6:39 PM
Lab 2 CS 455
4 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
you are calling a method that doesn't exist for the class you are using. (Maybe because you are looking
at 1.6 documentation, when we have a 1.5 compiler).
you are calling a method with the wrong number or type of arguments
Question 2.1 Describe two compile errors that you got while developing this code and their fixes. (It's ok if
both errors had the same high-level message, e.g., "cannot find symbol".)
For Exercise 2 check-off, show the lab TA your program running, your source code, and your answer to the
question above.
Modify Birthday.java so it prompts the user for their birthday, and then prints out whether their birthday has
happened yet this year. If their birthday is the same date the program is run, it should report that their birthday
has already happened.
Your output must look exactly like the example shown. Here is what it should look like running (user input
shown in italics):
Enter your birth month [1..12]: 3
Enter your birth day of month: 17
Enter your birth year [4-digit year]: 1991
Your birthday has already happened this year.
and if their birthday hadn't happened yet, the last line of output would be:
Note: the output of the program depends on what day you run the program. Your program should work
correctly, unchanged, any day you run it.
For the purposes of this lab, you do not have to error-check input. E.g., if the user inputs negative numbers or
the month 5000 at the prompt, your program does not have to handle that condition.
Hints:
The GregorianCalendar class stores a date including an exact time of day. Any of the methods that
compare two such objects (e.g., equals) are comparing all of that.
we recommend you look up the relevant documentation and plan out the logic involved in solving this
problem before you jump into coding. You can use your lab notebook to write down your ideas.
Question 3.1 Describe two compile errors that you got and their fixes while developing this code.
Question 3.2 Devise three test cases (not the one above) for which you and the TA already know the answer,
to help verify that your code is working correctly. Describe these test cases and the expected results in the
README. Then try out your test cases to help verify your program works properly.
10/6/2015 6:39 PM
Lab 2 CS 455
5 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
For Exercise 3 check-off, show your program running, your source code, and your answers to the question
above to the lab TA. When you run the program include the test cases mentioned above in your runs.
cp -p Birthday.java Birthday3.java
This is useful if you mess up and want to revert to back to a subset you know works. Continue to work with
Birthday.java for this part of the lab.
Keep all the old functionality of the program, but now it should also print out the current age of the user. See
example below:
Your output must look exactly like the example shown. Here is what it should look like running (user input
shown in italics):
Note: as in the last problem, the exact output of the program depends on what day you run the program. Your
program should work correctly, unchanged, any day you run it.
For Exercise 4 check-off, show your program running and your source code to the lab TA. When you run the
program include the test cases from Exercise 3 in your runs.
by 11:59pm on Sunday Pacific Time at the end of the week for this lab.
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy,
integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such
10/6/2015 6:39 PM
Lab 2 CS 455
6 of 6
http://scf.usc.edu/~csci455/curr/labs/lab2/lab2.html
content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees
10/6/2015 6:39 PM