Assignment 3
Goals: Learn how to follow the design recipe for designing methods in Java.
As the part of the design recipe, learn how to design tests for the methods and how to run them.
Instructions
The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.
Make sure you follow the style guidelines for code indentation.
You will submit this assignment by the deadline using the Web-CAT submission system.
With each homework you will also submit your log file named pairxxx.txt where you replace xxx with your pair number.
On top of every file you submit you will have the names of both partners, and the pair number.
The .txt file will be the log of your work on this assignment. Each log entry will have data and time, who was present (one or both of the partners) and a short comment decribing what you were working on.
Due Date: Tuesday, January 29th, 11:59 pm.
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Problem 10.6 on page 105
Problem 11.2 on page 116
Problem 12.1 on page 129
Use the javalib libraries to draw a stick figure
Problem 14.7 on page 144
Problem 1
The following DrRacket data definition describes the contents of a web site:
;;A Web Page (WP) is (make-wp String String [Listof Item (ILoI)]) |
(define-struct wp (url title items)) |
|
;; An Item is one of |
;; -- Text |
;; -- Image |
;; -- Link |
|
;; A Text is (make=text String) |
(define-struct text (contents)) |
|
;; An Image is (make-image String int String) |
(define-struct image (file-name size file-type)) |
|
;; A Link is (make-link String WP) |
(define-struct link (name page)) |
We are giving you the names of the classes or interfaces you will probably need
—
For lists of items, the names of the two classes should be MtLoI for the empty list and ConsLoI for the nonempty list, and the name of the interface that represents the list should be ILoI.
Describe in English, on paper a web page with at least two of each kind of items and at least two levels of links. Submit this as a WPdesign.txt file
Draw a class diagram for the classes that represent this data definition. Name the file that contains the diagram Diagram. You may make it a .java file and show the diagram as a block comment using ’ASCII art’, as is done in lecture notes, you may draw the diagram by hand and save it as a pdf file, you may make it a .docx document, or save it as a jpeg image.
Define Java classes that represent web pages as defined above.
Design the data representation of the example you have defined earlier.
Design the example of the following web page:
WP: with title "My Pets" at url "mypets.org"
that contains the following items:
-- text "This is my cat"
-- image of the cat in the file "kitty" of size 400 and file type "png"
-- text "This is my dog"
-- image of the dog in the file "doggie" of size 430 and file type "jpeg"
-- link with the label "Here are Bob's pets" to the web page named "Bob's Pets"
WP: with the title "Bob's Pets" at url "bob-pets.org"
that contains the following items:
-- text "My gerbil"
-- image of the gerbil in the file "cutie" of size 500 and file type "png"
Design the method totalImageSize that computes the total size of all images in this web page and all web pages that are linked to it.
Design the method textLength that computer the number of letters in all text that appears on the web site starting at this web page. This includes the contents of the text, the names of the image files, the labels for links, and their titles of the web pages.
Design the method images that produces one String that has in it all names of images on this web site, given with their file types, and separated by comma and space.
So for the exmple above this String would start with
"doggie.jpeg, kittie.png"
Note: You can combine two Strings with a + operator, or by invoking the method concat (e.g. s1.concat(s2) produces a new String appending the String s2 to the String s1.)
Make sure the object that represents the the "My Pets" page is named myPetsWP
Name your Examples class ExamplesWP
Problem 2
Continue the work on the Mobiles from the lab 3.
Make an example of the following mobile:
|
|
|
------------+------
| |
------+------ |
| | |
10 | 40
red 10 green
blue
Name it calder.
Finish the work on designing the methods isBalanced, buildMobile and maxWidth from the lab and include it in your submission.
Design the method WorldImage drawAt(int x, int y) that produces a WorldImage that represents this mobile, hanging from a hook at the given location on the Canvas.
The following code can be used to show the Canvas and the mobile drawn on it:
import javalib.worldcanvas.WorldCanvas;
// test the drawings of the mobiles
class DrawingMobiles {
// an instance of the test class where we defined
// an instance of a mobile named 'calder'
ExamplesMobileSolution ems = new ExamplesMobileSolution();
// the Canvas where the mobile will be displayed
WorldCanvas canvas = new WorldCanvas(400, 400);
// test the drawing of the mobile
boolean showDrawing() {
return
canvas.drawImage(this.ems.calder.drawAt(200, 100)) &&
canvas.show();
}
public static void main(String[] arg){
DrawingMobiles dm = new DrawingMobiles();
dm.showDrawing();
}
}
You can run the code, but first you need to import the javalib.jar library.
Important: Do not include this file in your homework submission.
Here is a sample that shows how the library can be used to draw images. If you run the following program ExamplesImageDrawings.java it will produce the image shown here WorldImageSampler.png. You will also need to save the file fish.png, using the following instructions:
If you want your program to display an image (in the format .png, .jpeg, .bmp. .tiff or some other formats), you need to save the images in the same folder in Eclipse where your src and bin folders are - for the project that uses the images.
Detailed documentation of the javalib drawing and game libraries can be found at http://www.ccs.neu.edu/javalib/World/.
Make sure the object that represents the shown mobile is named calder, and the name of your methods are isBalanced, buildMobile, maxWidth, and mobileImage.
Name your Examples class ExamplesMobiles