8.3

#### Assignment 1: Designing complex data

Goals: Practice designing the representation of complex data.

##### 1.1Instructions

This homework should be done with your partners (that you will work with starting from Lab 1). You may begin working on the assignment on your own before lab, and then combine work with your partner after lab.

Be very, very careful with naming! The solution files expect your submissions to be named a certain way, so that they can define their own Examples class with which to test your code. Therefore, whenever the assignment specifies:
• the names of classes,

• the names and types of the fields within classes,

• the names, types and order of the arguments to the constructor, or

• filenames,

...be sure that your submission uses exactly those names. Additionally, make sure you follow the course style guidelines. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, interfaces begin with a capital I, names of fields and methods start with a lower case letter), and having spaces before curly braces.

You will submit this assignment by the deadlines using the course handin server. Follow A Complete Guide to the Handin Server for information on how to use the handin server. You may submit as many times as you wish. Be aware of the fact that close to the deadline the server may slow down to handle many submissions, so try to finish early. 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 you work on.

The four submissions will be organized as follows:

• Homework 1 Problem 1: The RetailStore.java file

• Homework 1 Problem 2: The FriendGroup.java file

• Homework 1 Problem 3: The ExamplesGame.java file

• Homework 1 Problem 4: The BouncingBalls.java file

Due Date: Tuesday, January 25th, 9:00 pm

##### Practice Problems

Work out these problems from How to Design Classes 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 14.7 on page 140

##### Problem 1

Design a class RetailStore to represent the following information about stores

Everywhere in this assignment that you see italic, fixed-width text, it is intended to be the name of a field, identifier, class name or interface name you must define...but you likely must modify that name a bit to conform to our Java naming conventions: hyphenated-names are written in camelCase, and interface names begin with an uppercase I.

Everywhere that you see fixed-width text, it is exactly the name you must use.

• company: the name of the company that runs the store, as a String

• city: where the store is located, as a String

• type: the type of goods they sell, as a String

• squareFootage: the size of the store, as an int

• startingWage: the starting wage of an employee, as a double

• isHiring: a boolean representing whether or not the store is hiring

Make at least three examples of instances of this class, in the class ExamplesRetailStore. Two of the examples should be objects named gap and wollys and should represent the following two retail stores:

• A 10,000 sqft. Gap in San Francisco that sells clothing and is hiring new employees at $16.50/hr • A 2,250 sqft. Wollastons in Boston that sells groceries and is not hiring but starts pay at$15.00/hr

##### Problem 2

Here is a data definition in DrRacket:

 ;; A FriendGroup is one of: ;; -- Person ;; -- FriendOf ;; A Person is a (make-person String) (define-struct person [name]) ;; A FriendOf is a (make-friend-of FriendGroup String) (define-struct friend-of [connection name])
• Draw the class diagram that represents this data definition. You may draw this as ASCII-art and include it in your submission, if you wish. Or you can just draw it on paper and not submit it. Regardless, we 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, converted into Java style standards. Make sure that the constructor arguments are given in the same order as shown.

• Include in your examples the following friend groups

"Cameron" has a friend group of "Carter".

"Sarah" has a friend group of "James", "Ben", and "Louis".

The order of friends in a person’s friend group doesn’t matter. But friends are definitely distinct from the primary person in the friend group, and should not be conflated.

Make sure the two sample friend groups given above are named labs and largeGroup.

Name your file FriendGroup.java. Name the class that holds the examples of your data ExamplesFriendGroup.

##### Problem 3

We’ve been asked to help build a new deck-building game, Drydock. To start, we’re designing representations for the resources a player can have and the actions they can take during their turn. A player can have three kinds of resources: Captain, Crewmember, and Ship.

A Captain has a name and a number of successful battles (as an integer).

A Crewmember has a name, a description of their role on the ship, and an integer wealth value representing how many gold coins they have to their name.

A Ship has some description of its purpose and a flag hostile denoting whether it will plunder other ships unprompted.

As the game is under construction, the player can only perform two kinds of actions right now: they can Purchase a resource from the common pool, or they can Barter to swap a resource in their hand with one from the discard pile.

To purchase an item, the player must pay an associated cost, which must be a positive integer. They then receive the purchased resource item.

Every swap action has a sold resource and an acquired resource. The value of the acquired resource must be no more than 2 greater than the value of the sold resource. A captain is worth its battles, a crewmember is worth their wealth, and a ship is either worth 50 or 100, based on its hostility (hostile ships obtain more loot and are therefore more expensive).

• Define six examples of resources, including:
• jackSparrow: name "Jack Sparrow", battles 89

• hectorBarbossa: name "Hector Barbossa", description "first mate", wealth 52

• flyingDutchman: purpose "sail the oceans forever", hostile

The others can be whatever you wish.

• Define four types of actions, two of each kind.

Name your action examples purchase1, barter2, etc., and your examples class ExamplesGame. You haven’t learned yet how to check the described consistency requirements in Java, but make sure your examples follow them.

##### Problem 4

Complete the Bouncing Balls portion of the lab and hand it in. Hint: think carefully about exactly what causes a ball to bounce...and test your code well to figure out any subtle cases. The methods which were added to Ball earlier in the lab should also exist in BouncingBall.