1 Lab Objectives
2 Setting up your Java environment
2.1 A Basic Java Program
2.2 Configuring the tester library
2.3 Adding Test Cases/ Check Expects
3 Nested Classes
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.

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.

2.3 Adding Test Cases/Check Expects

Add a method overFourMin to your Song class. This method returns a boolean indicating whether the song is longer than 4 minutes. Add test cases to your Examples class. Run your program. You should get a report about whether your test cases passed.

3 Nested Classes

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.

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

  5. Run your tests for inGenre within the Examples class.

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.