Lab 3
Goals: The goals of this lab are to learn how to follow the design recipe for designing methods in Java.
Of course, as the part of the design recipe, we will learn how to design tests for our methods and how to run them.
For a simple tutorial on how to design the tests using the tester library look at the wiki pages on the JavalibTester github site.
1 Using the Web-CAT homework submission system
For the rest of the semester we will use the Web-CAT homework submission and grading system.
In the lab today we will make sure every student can submit their work individually, we will then make sure every pari can submit the homework that is due tonight.
Follow the link to our Web-CAT server and log in.
Find Assignment 1 and upload the file Shapes.java that we had worked on during the last lab.
Check that the file has been submitted correctly.
2 Methods for simple classes
Last week we started designing methods for the class Person that referred to the person’s Address. We have designed the method sameCityState that told us whether this person lived in the same city (and state) as the given Person.
Design the following additional methods for the classes Person and Address you have defined during the previous lab:
Design the method sameName that determines whether two persons have the same name.
Design the method moveToCity that produces a new Person who has moved to a new city within the same state.
Design the method moveTo that produces a new Person who has moved to a new city in a new state.
Design the method moveInWith that produces a new Person who has moved in with the given Person - so they are now in the same city and state.
3 Simple drawings
In this simple exercise you will se how you can design images from geometric shapes as well as image files and some text.
Download the file javalib.jar and add it to your project.
Start a new project MyDrawing and download the file ExamplesDrawings.java and import it into the project.
Download the file Images.zip, unzip it and save the images in the same folder in your Eclipse has the src folder for your MyDrawing project.
Run the program the same way as you would any other program.
Look at the program and add to it a new image. Include in your image at least three geometric shapes, one image (use those that were given to you; later you may explore other options), and some text.
For a desctiprion of how to design images see the Javalib World tutorial pages.
4 Methods for complex data
In class we have designed a data representation for mobiles as follows:
+---------+ |
| IMobile |<---------------+ |
+---------+ | |
+---------+ | |
| | |
/ \ | |
--- | |
| | |
--------------------- | |
| | | |
+--------------+ +---------------+ | |
| Simple | | Complex | | |
+--------------+ +---------------+ | |
| int length | | int length | | |
| int weight | | int leftside | | |
| IColor color | | int rightside | | |
+--------------+ | IMobile left |----+ |
| IMobile right |----+ |
+---------------+ |
Convert this data definition into Java interfaces and classes and make several examples of mobiles. Include in your examples at least three mobiles, the first two as shown and the thid one more complex that what is shown here:
Simple mobile |
|
10
blue
Complex mobile |
|
|
------------+-----
| |
------+------ |
| | |
10 | 40
red 10 green
blue
Design the method totalWeight for the mobiles that computes the total weight of the modile. We assume that the weight of the struts from which the mobile hangs is negligible, as is the weight of the string on which the disks hang.
Design the method totalHeight that computes the height of the mobile. Assume that the height of the disks is the same as their weight (i.e. a disk of weight 10 has height 10).
Design the method isBalanced that determines whether a mobile is balanced.
A simple mobile is always balanced.
A complex mobile is balanced if every mobile that hangs from it is balanced, and the weight of the left side multiplied by the length of the right strut equals the weight of the right side multiplied by the length of the left strut.
(So if I hang weight 3 on the left and weight 5 on the right, then the length on the left should be 5 and the lengh on the right should be 3 —
or both could be the same multiple of these numbers, e.g. 20 on the right and 12 on the left.) Design the method buildMobile that combines this balanced mobile with the given balanced mobile and produces a new mobile as follows:
The method is also given the desired length of the string on which the new mobile will hang, ad the strut that will be used to hang the two mobiles on. Your job is to make sure that the length of the strut is divided into the left and righ side in such way that the resulting mobile will also be balanced.