Assignment 1: Designing complex data
Goals: Practice designing the representation of complex data.
1 Instructions
The names of the classes must be exactly the same as specified. The same is the case for the names and types of the fields within the class, as well as the order in which they are defined and listed as the constructor arguments. This allows us to design our own Examples class that tests your program.
Make sure you follow the style guidelines that WebCAT enforces. For now the most important ones are: using spaces instead of tabs, indenting by 4 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.
You will submit this assignment by the deadline using the Web-CAT submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the WebCAT system may slow down to handle many submissions - so try to finish early.
With each homework you will also submit your log file named pair-user1-user2.txt where you replace user1 and user2 with the usernames of the two partners.
On top of both files you will have five lines of comments as follows:
// assignment 1 |
// partner1-last-name partner1-first-name |
// partner1-username |
// partner2-last-name partner2-first-name |
// partner2-username |
(In the text file you do not need the two slashes)
Your submission sould consist of the following files:
pair-user1-user2.txt – your log file
Person.java – the data definitions and examples for Problem 1
IPizza.java – the data definitions and examples for Problem 2
ExamplesTravel.java – the data definitions and examples for Problem 3
There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with the feedback for each problem wou work on.
The three submissions will be organized as follows:
Submission HW1P1: The log file and the Person.java file in one .zip file
Submission HW1P2: The IPizza.java file
Submission HW1P3: The ExamplesTravel.java file
Final instructions are now posted.
Due Date: Tuesday, January 14th, 10: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 34
Problem 5.3 on page 43
Problem 10.6 on page 102
Problem 11.2 on page 113
Problem 12.1 on page 125
Problem 14.7 on page 140
Problem 1
We are designing the data collection for the US Census Bureau. For each person we need to collect the following information:
name to be represented as a String
yob the year of birth given as a four digit number
state of residence - given as the standard two letter abbreviation
citizen a boolean value, true, if the person is a US citizen
Design the class Person that represents the information about each person for the census.
Make at least three examples of instances of this class, in the class ExamplesPerson. Two of the examples should be objects named jackie and golda and should represent the following two people:
Jackie Robinson, born in 1920, resides in NY, and is a citizen
Golda Meir, born in 1930, resides in MA, is not a citizen
Problem 2
Here is a data definition in DrRacket:
;; A IPizza is one of |
;; -- Plain |
;; -- Fancy |
|
;; A Plain is |
;; -- (make-plain String String) |
(define-struct (plain crust cheese)) |
|
;; A Fancy is |
;; -- (make-fancy IPizza String) |
(define-struct (fancy base topping)) |
Draw the class diagram that represents this data definition. If you generates this as so-called ASCI-art, you can include it in your submission. If you just draw it on a paper, you do not need to submit it. We just think it will help you in visualizing how the data is organized.
Convert this data definition into Java. Make sure you use the same names for data types and for the fields, as are used in the DrRacket data definitions. Also, make sure that the constructor arguments are given in the same order as shown.
Include in your examples the following two pizza orders:
– a pizza with "thin crust", "mozarella" cheese, and two toppings: "onions" and "anchovies"
– a "deep dish" pizza with "mixed" cheese, with topping of "pepperoni", "peppers", and "mushrooms"
Make sure the two sample orders given above are named order1 and order2.
Name your file Pizza.java and the file and the class that holds the examples of pizza data ExamplesPizza
Problem 3
There are three ways one can travel from one city to another: by car, by train, or by plane.
For each city, we keep track of its name, state, the name of the train station and the name of the airport. If the city does not have an airport, or a train station, the name is the empty String.
One leg of a trip consists of the start and end city, and the distance between them. There are three different kinds of legs one can define:
CarLeg travelled by car
PlaneLeg travelled by a plane - here the start and end are cities that have a train station.
TrainLeg travelled by a train —
here the start and the end cities must have airports.
Define the classes that represent the three different legs of a trip and define examples in the class ExamplesTravel as follows:
- Define six cities, five of them to be:
NYC in the state NY with Penn train station and JFK airport
Boston, MA with South train station and Logan airport
Chicago, IL with Union train station and O’Hare airport
Columbus, OH with CNH airport
Erie, PA with Union train station
Define six legs of travel, two of each kind.