Assignment 2
Goals: Review the design recipes, work with inexact numbers, review the design of loops using the accumulator style.
1 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. We will be practicing its use during the lab next week.
With each homework you will also submit your log file named pairxx.txt where you replace xx with your pair number.
On top of both files you will have four lines of comments as follows:
// assignment 2 |
// pair xxx |
// partner1-last-name partner1-first-name |
// partner1-username |
// partner2-last-name partner2-firt-name |
// partner2-username |
(In the text file you do not need the two slashes)
Your submission sould consist of the following files:
pairxxx.txt – your log file
IPhoneChain.java – the data definitions for Problem 1
interface name should be IPhoneChain, the class names should be Empty and Link
ExamplesPhoneChain.java – the examples of data for Problem 1
the instance that defines the sample tree for Jen should be named jenLink
IPizza.java – the data definitions for Problem 2
the interface should be named IPizza, the class names should be Plain and Fancy
ExamplesPizza.java – the examples of data for Problem 2
the two required examples of pizzas should be named oreder1 and oreder2
Person.java – the data definitions for Problem 3
class names should be Person and Address
ExamplesPerson.java – the examples of data for Problem 3
the instances of people defined here should be the same as their names, but starting with a lower-case letter, for examples tim, jan
Combine all files into one HW2.zip file and submit that file.
Due Date: Tuesday, January 22nd, 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 2.4 on page 17
Problem 3.1 on page 25
Problem 4.4 on page 33
Problem 5.3 on page 44
Problem 5.9 on page 51
Problem 10.2 on page 97
Problem 10.5 on page 105
Problem 14.1 on page 140
Problem 14.7 on page 144
Problem 1
Finish working out the problem from Lab 2 that deals with a phone chain for a soccer team.
Name the file you submit IPhoneChain.java and the file and the class that holds the examples of phone chain data ExamplesPhoneChain.java.
At the end of your ExamplesPhoneChain class definition include the following lines:
// making sure the code contains at least one test |
boolean testAll(Tester t) { |
ExamplesPhoneChain epc = new ExamplesPhoneChain(); |
return t.checkExpect(epc.jenLink, epc.jenLink); |
} |
This is only needed for our first two submissions, because they do not contain any tests.
Problem 2
Here is a data definition in DrRacket:
;; An IPizza is one of |
;; -- (make-plain String String) |
;; -- (make-fancy IPizza String) |
|
(define-struct (plain crust cheese)) |
(define-struct (fancy base topping)) |
Draw the class diagram that represents this data definition.
Convert this data definition into Java.
Include in your examples the following two piza orders:
– a pizza with thin crust, mozarella cheese, and two toppings: mushrooms and olives
– a deep dish pizza with mixed cheese, pepperoni, and onions
Make sure the two sample orders given above are named order1 and order2.
Name your file IPizza.java and the file and the class that holds the examples of pizza data ExamplesPizza.java
At the end of your ExamplesPizza class definition include the following lines:
// making sure the code contains at least one test |
boolean testAll(Tester t) { |
ExamplesPizza ep = new ExamplesPizza(); |
return t.checkExpect(ep.order1, ep.order1); |
} |
This is only needed for our first two submissions, because they do not contain any tests.
Problem 3
Designing methods.
Complete and hand in the Part 7 of Lab 2 that asks you to design your first method(s).
Design the method sameStateOnly that determines whether two people are from the same state but do not live in the same city.
Hint: Remember, one task, one method rule, and use it.
Make sure the objects that represent the four sample persons are named ann, pat, kim, and dan, and the name of your method is sameCity.
Name your file Person.java and the file and class that holds the examples of peron and address data ExamplesPerson.java