This is the archived website of IC 312 from the Fall 2015 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 1: Remembering Java

  • Due before class on Friday, August 28
  • Submit using the submit program as 312 hw 01.

To help us remember Java, we'll do a little warmup homework. This was a practicum for the 2012 12-week IC211 exam. This should not be hard, but it might take some time to remember everything. There shouldn't be a lot of reason for this to be less than perfect.

You are going to implement some useful code for a bookstore, which they can use to keep track of inventory. A full submission for this practicum will contain 6 classes (ReadingMaterial, Book, Magazine, Bookstore, OutOfStockException, BookMain). Note BookMain is written for you.

Download warmup.tgz for test files and BookMain.java.

Unpack it with tar xzf warmup.tgz

ReadingMaterial

ReadingMaterial implements Comparable. Each reading material object has:
  • A title (String), a cost (double), and a quantity in stock (int).
  • A constructor that takes these three values, in that order.
  • Getters for title, cost, and quantity.
  • An abstract String toString().
  • A void buy() which decrements the quantity. If the quantity is already zero, this throws an OutOfStockException.
  • A int compareTo(Object o) which allows ReadingMaterials to be sorted by price (lowest to highest).

Book

Book extends ReadingMaterial. In addition to what it inherits from ReadingMaterial, it has:
  • An author (String).
  • A constructor, that takes as arguments the title, author, cost, and quantity, in that order.
  • A toString(), that returns "author, title: $cost".

Magazine

Magazine extends ReadingMaterial. In addition to what it inherits from ReadingMaterial, it has:
  • A constructor, that takes as arguments the title, cost, and quantity, in that order.
  • A toString(), that returns "title: $cost".

Bookstore

Bookstore contains all the ReadingMaterials we are hoping to sell. So, it has:
  • An array of ReadingMaterials
  • A constructor that takes as an argument an array of ReadingMaterials
  • A method void buy(String title) which finds a ReadingMaterial in its array with exactly the same title, and calls its buy() method. If no such ReadingMaterial exists, or if the item is out of stock, it throws an OutOfStockException.
  • A method void printAll() which prints out what is returned by each ReadingMaterial's toString().
  • A sort method which follows in its entirety:
    1
    2
    3
    
    public void sort() {
      Arrays.sort(arr); // replace "arr" with the name of your array
    }

OutOfStockException

behaves like a RuntimeException.

BookMain

is entirely written.

You have downloaded and unpacked a directory with BookMain.java and two test files, testFile.txt, and input.txt. If your code is run as follows, it should result in the following output:

taylor@albert:~/12wk/practicum$ java BookMain testFile.txt < input.txt 
The Economist: $0.99
Sports Illustrated: $1.50
David Foster Wallace, Infinite Jest: $15.99
Harry S Truman, Mr. Citizen: $21.99

What would you like to buy?
You are trying to buy Infinite Jest
What would you like to buy?
You are trying to buy Mr. Citizen
What would you like to buy?
You are trying to buy Mr. Citizen
Out of stock!
What would you like to buy?
You are trying to buy The Economist
Out of stock!
What would you like to buy?
You are trying to buy Nonexistant
Out of stock!
What would you like to buy?