Spring 2024 [Bono]
The first few sections below have some general information about labs that apply to all labs.
Recommended advanced preparation for this lab
Before coming to lab this week we recommend you read over the lab at
least through Exercise 1, and to complete that exercise. It's a
pencil and paper exercise (can put answers in the README). When you
come to lab you can compare your results with your lab partner.
(Note: you do not need to use Vocareum for the lab until Exercise 2.)
Unlike with Lab 1, unless you are a DEN student you will not be submitting your labs on Vocareum, but will get it checked-off face-to-face during your lab section (it is due by the end of your lab section). Check-off for DEN students is described at the end of each lab.
General note on saving your work: You should save all of your completed assignments, because sometimes we will build on what we have done in one assignment in a later assignment. Also, if there are any grading issues later, you will have evidence of the work you have done. As long as you don't delete your files in Vocareum, they will still be available in the workspace for the given lab throughout the semester. We recommend doing this even for different parts of a lab when you have already gotten the previous part checked off: thus the different file names for the different parts of this lab. Make sure you put the assignment number (e.g., CS 455 Lab2), and the names and roles of the lab partnership (described further in the next section) in a comment at the top of all the files for the lab (generally source code and README).
The questions to be answered by the scribe are in bold and labeled Question 1.1, 1.2, etc. You should put your answers to those into a README text file that you edit, 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.
Hint: as you can see on the Java API page, this class has no constructors. The methods to create a new instance are static methods of LocalDate whose return type is LocalDate. (The call to new is hidden inside those methods.) To call a static method you use the class name instead of an object name before the dot; e.g., Math.sqrt(10)
1/20/1995(We're looking for something that would work no matter what date we started with.)
Hint: In the documentation look for methods that start with "get".
Hint: LocalDate is an immutable class (like String).
1/20/1995 2/9/1995
How to copy a file from another lab. If you want to start from the Hello program you started with in the first lab, and then modify it to create this program, this is a good opportunity to try copying a file in Vocareum. (You could also do this by just cutting and pasting the contents of a file, but that becomes more unwieldy with larger files later.) Here's how you copy the file itself:
- if you are starting from inside the Lab 2 workspace, you can get out of that by clicking "Lab 2" at the upper left. That will get you back to the list of assignments, and can switch to a different lab.
- go into the workspace for "Lab 1". Select Hello.java from the list of files in your work folder.
- click Copy (upper left).
- go back to Lab 2. (technique explained above in step 1.)
- click Paste (upper left).
- Now you can rename the file. There are two way to do this. Either:
- Select the the file in the file list. Then click the Rename button (upper left; you type the new name in the textbox that appears right below that button); Or:
- in the Terminal window use the linux mv command:
mv Hello.java Date.java
Hint: Remember that the name of the main class has to match the file prefix.
Hint2: even before you write your print statements, an easy way to test if you created the correct object is to add a debug print statement and compile and run the code before adding anything else. When you put an an object variable in a println statement it will print out the data in that object. In this case, Java implicitly calls a method called toString; look at the documentation for toString to see what will get printed for a LocalDate. Here's an example of such a call:
System.out.println("DEBUG: " + myDate); // Java calls myDate.toString()You can comment out your debug print statements when you are done testing.
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 are using an undefined variable.
- 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.
- you are calling a method that doesn't exist for the class you are using (e.g., myString.sqrt(100)).
- you are calling a method with the wrong number or type of arguments
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]: 1 Enter your birth day of month: 5 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:
Your birthday has not yet happened this year.Note: the output of the program depends on what day you run the program. Your program should work correctly, without changing the code, 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, a letter, or the month 5000 at the prompt, your program does not have to handle that condition.
Hint: 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.
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.javaThis is useful if you mess up and want to revert to back to a subset you know works. Once you copy the file 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):
Enter your birth month [1..12]: 9 Enter your birth day of month: 30 Enter your birth year [4-digit year]: 1991 Your birthday has not yet happened this year. You're 32 years old.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.
Happy Birthday!Hint: for this one we definitely recommend planning out your logic before jumping into coding.
When you click the Submit button, it will be looking for and compiling the files README, Date.java, and Birthday.java in your home (aka, work directory). The lab is due by 11:59pm on Sunday Pacific Time at the end of the week for this lab.