ICS 211 Homework H02: OOP, Class Hierarchies and Interfaces.

Purpose

Object Oriented Programming (OOP) is a very useful tool for problem solving. We can break down a problem into its components, model them as classes and instances to solve the problem. We are also going to explore different ways of comparing instances.

We are going to enhance our understanding of OOP, class hierarchies and interfaces by creating a simple Java class hierarchy of Beers and a Brewery. The Brewery implements an interface and follows the Factory and Singleton design patterns. In addition to the Beers and Brewery we are going to develop two different Comparators for comparing beers.

This assignment will give you practice using Eclipse to create and test Java classes. Hopefully, you have set up your Eclipse environment to make this assignment easier to complete.

Tasks

1. Create a package named edu.ics211.h02

This is where we will put all our classes for homework 2.

2. Create an enum BeerType

From the Enum Types (The Java(TM) Tutorials). “An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.”

Our BeerType enum defines the valid types of beer for this assignment, INDIA_PALE_ALE, PILSNER, and BOHEMIAN_PILSNER.

Select the edu.ics211.h02 package and right mouse click. Choose New -> Enum

New Enum

Name the Enum BeerType.

BeerType enum

Then copy the contents of BeerType.java into the file.

3. Create the base abstract class Beer that implements Comparable<Beer>

Choose New -> Class.

New Beer Class

Make sure you add the Comparable<Beer> interface and click the abstract modifier before you click finish.

Here’s a screen shot of my Beer class the eclipse created. Notice the compareTo method was automatically created.

Beer Class

Modify the base abstract class Beer so that it has:

The Beer class must be abstract. You cannot create an instance of a generic beer. You can only create instances of the subclasses which are concrete.

Here’s a screen shot of my Beer class in Eclipse. Beer class

Notice I’ve added the getters and setters, hashCode, equals, toString methods and there are no Problems in my project. That means that there are no compile or checkstyle errors.

4. Create the class Pilsner that extends Beer

Class Pilsner extends Beer has:

Once you are done, your Pilsner class should be able to pass these tests PilsnerTest. To check this save the PilsnerTest.java file in the same directory as your Pilsner.java file. The you can run the tests by opening the file and choosing Run -> Run As -> JUnit Test. You should see something like:

Passing Pilsner Tests

The green bar on the left indicates all JUnit tests passed.

5. Create two other Beer subclasses, BohemianPilsner and IndiaPaleAle

Class BohemianPilsner extends Pilsner,

Your BohemianPilsner class should pass BohemianPilsnerTests.

Class IndiaPaleAle extends Beer,

You should create JUnit tests for your IndiaPaleAle class.

6. Create the IBrewery interface

The IBrewery interface defines the methods all Breweries must implement. Choose New -> Interface.

New Interface

Name the interface IBrewery

IBrewery

Copy the contents of IBrewery.java into the new interface.

7. Create the ManoaBrewing class

Create a ManoaBrewing singleton class that can create Pilsners, BohemianPilsners and IndiaPaleAles. Singleton classes have a static method getInstance() that returns the one and only one instance of the class. The class must implement the IBrewery interface.

Here’s a screen shot of my ManoaBrewing class.

ManoaBrewing

Notice there are no Problems in my project. That means that there are no compile or checkstyle errors.

Implement the IBrewery methods. The brewBeer method should check the BeerType then brew a random beer of that type. For the other brew methods if the ibu or abv are not correct the methods should throw an IllegalArgumentException.

At this point you can test your code with ManoaBrewingTest.java. We are going to use the JUnit tests to evaluate your homework for correctness.

8. (Optional) Create some Comparators

Next week’s homework is sorting a flight of beer (actually an array). You will need to create two comparators to do the sorting so you can get a head start on next week’s homework by:

You can test your comparators using ComparatorTest.java.

Grading Rubric

CriterionExcellent (100%)Satisfactory (75%)Borderline (50%)Unsatisfactory (25%)Poor (0)
Adherence to standards - 2 points
Does it conform to standards in every detail?
No errors. Minor details of the assignment are violated, or poor choices are made where the assignment is unclear. Significant details of the assignment or the underlying program intent are violated, but the program still fulfills essential functions. Significant details of the assignment or the underlying program intent are violated, but the program still fulfills some essential functions. Misses the point of the assignment.
Breakdown (modular design) - 1 point
Does it demonstrate good modular design?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.
Correctness of code - 4 points
Does it work? Does it pass JUnit?
Passes all tests. Works for typical input, may fail for minor special cases. Fails for typical input, for a minor reason. Fails for typical input, for a major reason. No.
Documentation, and style - 2 points
Is it clear and maintainable? Does it pass CheckStyle?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.
Efficiency of code - 1 point
Does it use the Java features well?
No errors. 1-3 minor errors. > 3 minor errors OR 1 major error. 2 major errors > 2 major error.

Turning in the Assignment

The assignment is due on Friday at 11:55pm. You may turn it in early.