Assignment 3: Abstracting over Data Definitions; Designing Games Solution

$35.00 $30.80


Goals: Learn to use the Sting class, practice working with lists. Learn to design abstract classes and class hierarchies. Design an interactive graphics-based game: Using libraries.

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
  • java – the data definitions and examples for Problem 1
  • java – the data definitions and examples for Problem 2
  • java – the data definitions and examples for Problem 3

all combined into one file.

Due Date: Monday, October 7th, 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.

  • Problems 18.1 – 18.4 on page 225
  • Problem 18.5 on page 229
  • Problem 18.6 on page 234
  • Problem 19.4 on page 263

Problem 1: Understanding the String class

For this problem start with the file that defines a list of Strings.

Note: The following method defined for the class String may be useful:

// does this String come before the given String lexicographically?
// produce value < 0   — if this String comes before that String
// produce value zero  — if this String and that String are the same
// produce value > 0   — if this String comes after that String
int compareTo(String that)
  • Design the method isSortedthat determines whether the list is sorted in alphabetical order.

Hint: You may need a helper method. You may want remember the accumulator style functions we have seen in Scheme.

  • Design the method mergethat consumes two sorted lists of <tt>String</tt>s and produces a sorted list of Strings that contains all items in both original lists (including duplicates).

Problem 2: Abstracting over Data Definitions

A file in a computer can contain either a text, or an image, or an audio recording. Every file has a name and the owner of the file. There is additional information for each kind of file as shown in the program

Download the file and work out the following problems:

  • Make one more example of data for each of the three classes and add the tests for the method sizethat is already defined.
  • Design the method downloadTimethat determines how many seconds does it take to download the file at the given download rate.

The rate is given in bytes per second.

  • Design the method sameOwnerthat determines whether the owner of this file is the same as the owner of the given file.

Save the work you have done. Copy the file and continue. (Do not submit this part of your work.)

Possibly start a new project and import the file into it. Alternatively, save the a copy of the file you have been working on in another folder.

Hand in the result of this part of the assignment only.

  • Look at the code and identify all places where the code repeats —

Lift the common fields to an abstract class AFile.

Make sure you include a constructor in the abstract class, and change the constructors in the derived classes accordingly. Run the program and make sure all test cases work as before.

  • For each method that is defined in all three classes decide to which category it belongs:
    • The method bodies in the different classes are all different, and so the method has to be declared as abstractin the abstract
    • The method bodies are the same in all classes and it can be implemented concretely in the abstract
    • The method bodies are the same for two of the classes, but are different in one class — therefore we can define the common body in the abstractclass and override it in only one derived class.

Now, lift the methods that can be lifted and run all tests again.

Note: You can lift the method sameOwner only if you change its contract. Do so — make sure you adjust the test cases accordingly.

Problem 2: Using Libraries: Designing Interactive Games

Design a simple interactive graphics-based game that extends the World class in the javalib.funworld library and performs some actions on each tick, and responds to some user’s key events.

The game should have at least one collection of objects in it.

It should have some random behavior.

It should not be overly complicated, but have options for later additions.

For your submission besides the code for your game and all of the necessary examples and tests, you should include:

  • a copy of the class diagram
  • a one page text document that explaing how to play the game for the game user
  • a one (or more) page text document that describes the responsibilities of each class in your program

Additional instructions for how to organize and submit your work will be available early next week.