Purpose:
To help setup your Eclipse environment, develop data definitions, and write simple methods over classes and recursive unions (interfaces).
Part 0: HW Submission Test
Try submitting something for Assignment #1 and see if it works. Don't worry, you'll be able to resubmit as many times as you want. Let your Lab TA know if it fails.
Part 1: Setting up Eclipse and the Tester
For the rest of the semester we'll be using the Eclipse IDE to develop our Java programs. There are many different ways to test programs written in Java/Eclipse, but for simplicity we'll be using a library that provides forms similar to check-expect in DrRacket.
Create a directory for your course files and Libraries (called JARs in Java) somewhere accessible. It's easiest to log in using your CCIS account (assuming you have one) and create a directory on your Z: drive under the "classes" directory, something like "cs2510".
Source Management
For those of you that are interested, most computer scientists (e.g., your instructor) use a source control/versioning system to manage projects that allow multiple users to access and modify them concurrently. Most popular are Subversion and GIT, both of which have plugins for Eclipse to manage your project from within the IDE.
Any time invested will certainly pay off when you never have to worry about your computer crashing, files getting lost, or screwing up the current version of your homework/project. If you need help setting up a source repository we'd be happy to pass on what we know. But we will not force you to use one for the purposes of this course.
Eclipse and the Tester Library
Once your course directory is setup, download/save the tester library JAR (Tester.jar) in the directory and create a separate "workspace" directory. Note: this is a custom version of the Notheastern Tester library... the full version/documentation is located here.
Start up Eclipse. When prompted, select your workdspace directory from before (e.g. Z:/classes/cs2510/workspace/. Feel free to check the "Use this as the default..." box.
Once Eclipse starts, close the annoying Welcome screen if it comes up.
First things First
There's a few settings we need to get out of the way before you start. If/when you work from your own computer you'll have to adjust these settings too, otherwise the graders might be angry... and you wouldn't like them when they're angry.
Select Preferences under the Window menu and change the following settings. You should also be able to follow along with your friendly Lab Leader.
Great... now let's get on to Java-like programming!
- Type "tab" in the search box at the upper-left to minimize the available selections.
- Select "Text Editors" on the left. Make sure the "Insert spaces for tabs" check-box is checked.
- Select "Formatter" on the left. Click the "Edit..." button, then choose the "Indentation" tab at the top. Change the "Tab policy" to "Spaces only".
- When you say OK it will force you to create a name for the profile... you can just say "Mine" or some other cool name.
Creating a Project
Select New > Java Project from the File menu. Enter the name "Lab-02" or something similar. Under Project Layout, make sure the "Create separate folders for sources and class files" radio button is selected, then click Finish.
Right-click your project in the Package Explorer pane and select "Build Path > Configure Build Path". Under "Java Build Path" on the left, select the Libraries tab, and click "Add External JARs...". Browse to where you saved the Tester.jar and click "OK".
Open your project in the project explorer, right-click on the "src folder, and select New > File. Depending on your Eclipse version, you may have to select Other, then choose General > File. Name it "Lab2.java" (or something else with a ".java" extension.
Add a simple Lab2Examples class to the file... put a few fields and initialize them, something like this:
class Lab2Examples{ int myInt = 45; double myDouble = 16.82; String myString = "Hello Java"; }Make sure there are no errors.Setup a Run Configuration
Highlight your project in the in the Package Explorer pane. In the Run menu select Run Configurations.... In the left pane select Java Application.
In the upper left corner click on the leftmost item (the icon with the plus in the corner). When you mouse over, it should show "New launch configuration".
Make up a name for this configuration - usually the same as the name of your project, lab, or assignment. The "Project" field should be your current project (e.g., "Lab-02"), and in the Main class field enter tester.Main (note the lowercase t and uppercase M).
Click on the (x)= Arguments tab and in the Program arguments text field enter "Lab2Examples". Later, if/when you define your own Examples classes, you will use the class' name to run your examples/tests.
At the bottom select "Apply" then "Run". You should see a few messages in the Console, and a display of a Lab2Examples class instance with the your fields/instance(s).
Part 2: Defining Classes and Unions
Exercise 1:
Remember all the Rockets we launched (and landed) last semester? Here's a Racket data definition for a simple Rocket:
;; A Rocket is: (make-rocket String Number Boolean) (define-struct rocket (name height landing?)) ;; Example Rockets: (define r1 (make-rocket "Apollo 11" 150 false)) (define r2 (make-rocket "Gemini 3" 25 true))
- Draw the class diagram for this data definition
- Create a new file in your Eclipse Lab project named Rocket.java and convert the data definition/class diagram into a Java class definition.
- Create a RocketExamples class, and make example instances of Rockets.
- Create a new Run Configuration and run it to display the examples.
Exercise 2:
Rather than just keeping track of the height of a Rocket, we might also need its horizontal coordinate. Let's use a Posn to keep track of both. Of course posn is built into DrRacket, so the following is mostly just for illustration.
;; A Posn is: (make-posn Number Number) (define-struct posn (x y)) ;; Example Posns: (define p1 (make-posn -30 150)) (define p2 (make-posn 50 25)) ;; A Rocket is: (make-rocket String Posn Boolean) (define-struct rocket (name loc landing?)) ;; Example Rockets: (define r1 (make-rocket "Apollo 11" p1 false)) (define r2 (make-rocket "Gemini 3" p2 true))
- Draw the class diagram for these data definitions
- Add the Posn class to your file and modify the Rocket class (and constructor) to match the Racket definitions.
- Create examples of Posns and adjust your Rocket Examples accordingly.
- Run the examples to make sure everything is correct.
Exercise 3:
The class of data that represents Rockets is not as interesting as it could be. For instance, the rocket that lifts the space shuttle is very different than the one that took Buzz to the moon. And a toy rocket might have a price associated, whereas you probably don't want to know how much the space shuttle rockets cost (especially if you pay taxes).
Create an interface to represent various kinds of rockets... call it IRocket (and so do you). The data class of IRocket will be made up of three different Java classes... feel free to embellish as you see fit.
- SelfFlying which includes the location and the version number of the auto-pilot software.
- DualEngine which includes the location, the power of each engine, and the name of the pilot.
- Toy which includes only the max-height and price of the rocket.
Notice that the names are distinct from your other classes... you can use the same java file, just include new examples in your RocketExamples class
- Draw a class diagram for the class hierarchy that represents these three types of Rockets
- Design data definitions (i.e., classes) for each of the definitions (including the interface)
- Add examples of each kind of IRocket to your RocketExamples class. Be sure to use IRocket as the type of the example fields.
Part 3: Recursive Unions
Exercise 4:
You remember our definition of list classes from Lectures (hopefully just a few minutes ago)? Well we want you to practice them, this time by making classes and interfaces that represent List of IRocket.
- Draw a class diagram for the interface and classes that represent lists of IRocket. Typically we will name them ILoR (interface for lists of rockets), MtR (Empty Rocket List), and ConsR (Cons Rocket Element).
- Turn your class diagram into interface/class defintions.
- Create examples of lists of IRockets. Make sure at least one of them has more than three rockets.