Lab 1
1 Lab Objectives
Get you up and running with a Java environment
Help you configure the check-expect testing library in your environment
Practice a simple data definition conversion
Learn how to handle simple references between data definitions
2 Setting up your Java environment
Decide whether you will use DrJava or Eclipse for this course. We recommend DrJava (at least at first) unless you are comfortable with complicated software interfaces. You can find either of these by typing in the name in the search box under the programs menu on the lab machines.
If you are using your own laptop, download and install whichever tool you wish to use.
2.1 A Basic Java Program
Create a new project and open a new file in your chosen environment. Copy the following class definition into the new file:
class Song { |
String title ; |
int lenInSeconds ; // duration of the song |
|
Song (String title, Integer lenInSeconds) { |
this.title = title ; |
this.lenInSeconds = lenInSeconds ; |
} |
} |
Create an Examples class with two examples of data. Standard Java practice expects you to put each class in a separate file. If you are new to Java, put this in the same file as your Song class until you are comfortable with how the pieces hang together.
2.2 Configuring the tester library
Now, we need to tell Java how to find your Examples class and run its contents when you run your program. Refer to the instructions on the course website.
Run your current file. If it worked, you’ll get a prompt in the DrJava interactions window, or no errors from Eclipse.
3 Nested Class References
Songs are typically affiliated with Albums. We want to add an Album class and include a field with album information in each Song.
Translate the following data definition into Java. Again, you can put it in a new file or add it to your running file.
; An album is (make-album string string)
(define-struct album (artist genre))
Edit your Song class to include a field onAlbum with type Album.
Revise your examples of data to include the new field.
Everyone should get this far before splitting into discussion groups. Keep going until discussion time is called. Don’t worry if you only get to this point this week.
3.1 Code Review
Look at this sample attempt at adding the Album class and writing a couple of methods for it (as Java for experimenting or as PDF for marking up).
What errors do you see? (try doing this without running the code first)
What do you have to remember about programming in Java to avoid these mistakes?
What error messages does Java produce for these mistakes? (run the code to find out – the errors don’t always make sense, but this exercise is designed to help you recognize having seen these messages before, in case they arise again in your own work).
Your discussion group will review the errors and the lessons from them. Continue to work through eliminating the errors on your own at home if you are new to Java: this will help you get familiar with some common early programming gotchas in Java.
3.2 Writing Methods
Add a method overFourMin to your Song class. This method returns a boolean indicating whether the song is longer than 4 minutes.
Write a method inGenre on albums that consumes a String and produces a boolean indicating whether the album’s genre matches the given genre. To test two strings for equality in Java, you use the command <string1>.equals(<string2>), where <string1> and <string2> are replaced with the names or expressions that yield the strings to compare.
4 What to Turn in
Submit all .java files that you produced for this assignment to the Lab1 area via Turnin. If you don’t have a Turnin account or don’t find CS2102 under your available courses, ask your lab staff to create one for you.