1 Lab Objectives
2 Setting up your Java environment
2.1 A Basic Java Program
2.2 Configuring the tester library
3 Nested Class References
3.1 Code Review
3.2 Writing Methods
4 What to Turn in

Lab 1

1 Lab Objectives

  1. Get you up and running with a Java environment

  2. Help you configure the check-expect testing library in your environment

  3. Practice a simple data definition conversion

  4. 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. If you are on a Mac, note that you need to download/use the "Jar File", not the "Mac OS X App". See the software page for instructions on how to use the Jar file, as well as general instructions to configure your environment.

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.

  1. 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))

  2. Edit your Song class to include a field onAlbum with type Album.

  3. Revise your examples of data to include the new field.

Everyone should get at least this far during lab. Don’t worry if you only get to this point this week, but look at the code review queestions in the next section on your own.

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).

We encourage you to break into small groups and review both the errors and the lessons you learned 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

  1. Add a method overFourMin to your Song class. This method returns a boolean indicating whether the song is longer than 4 minutes.

  2. 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 assignment in InstructAssist. If you don’t have an account in InstructAssist, ask your lab staff for help.