CS 112
Spring 2019

Old version

This is the CS 112 site as it appeared on May 8, 2019.

Lab 3: Understanding Static Classes vs. Object Classes

Using folders

We strongly encourage you to create a separate folder for each lab and each problem set, so that you can more easily keep track of your work. For example, you could create a folder called lab2 for your work on this lab, and put all of the files for this lab in that folder. If you are working on a lab machine, you will need to create the folder on the Z: drive, so that it won’t be lost when you log out.

Reminder on Attendance Policy

Working with Roman Numerals

Recall that a long time ago, numbers were represented via Roman Numerals. The values of each symbols are as follows

To convert from Roman Numeral to digits we must consider the following rules

  1. Reading left to right symbols with higher values will generally appear first. The overall value is given by taking the sum of values from left to right.

  2. If a single lower value is placed left of a higher value letter, it is subtracted from the total instead.

  3. Only one single lower value can be subtracted from a higher value

  4. ”I” can only be subtract from ”V” and ”X”. ”X” can only subtract from ”L” and ”C”, ”C” can only subtract from ”D” and ”M’, and ”V”, ”L”, and ”D” can never subtract.

In this lab we will begin with defining a static class named RomanNumberalStatic. This class will provide several static methods that will allow us to perform operations on, you guess it, roman numerals. Then we will create an object based version of this class so we can compare and contrast the differences between them.

Task 1: Static Roman Numeral Class

Begin by downloading RomanNumeralStatic.java.

Notice that several methods have been written. Further note that one of the methods have been declared to be private and not public. Why do you think this is the case?

Add code to the main method to test the convert method that takes in a Roman Numeral string and converts it to its integer equivalent value. The signature of this method is:

public static int convert(String romanNum)

Here are a few examples of statements that can be added to the main method to test the convert method on several Roman Numeral strings.

System.out.println(convert("X"));
System.out.println(convert("LXXXXIX"));
System.out.println(convert("CDV"));

The following should be the expected output:

10
99
405

Add code to the main method to test the add method. This method accepts two Roman Numerals (as String arguments) and returns the sum of the inputs as an integer. The signature of this method is:

public static int add(String romanNum1, String romanNum2)

Here are a few examples of statements that can be added to the main method to test the add method.

System.out.println(add("X", "X"));
System.out.println(add("XI", "CDV"));
System.out.println(add("LXXXXIX", "I"));

The expected outputs should be the following:

20
416
100

Task 2: Roman Numeral (Object) Class

Classes made up of only static methods are great, however, they don’t take full advantage of the Object Oriented Paradigm (OOP) that Java is based on. To illustrate the difference, let’s convert our static Roman NumeralStatic API into an actual object class.

What should the attributes of this class be? What are the behaviours that this class should implement? Thinking through this will help us identify the data we need to store and the methods we need to write.

What is a roman numeral? It is a string which represents a Roman Numeral and it has a corresponding decimal value. In order to properly represent a Roman Numeral, each object we create should contain exactly two values or data members, the string that represents the Roman Numeral, and the decimal value associated with it.

Once you have discussed the features of Roman Numberal objects, build your class as follows:

  1. Create a new file called RomanNumeral.java.

  2. Declare the data members of the class.

  3. Create the constructor that takes in a String which is expected to be a Roman Numeral and initialize the data members of the class.

  4. Create a toString method that returns the Roman Numeral representation of the decimal number

  5. Create an equals method that checks if two RomanNumerals are equivalent

  6. Create an add method that takes in another RomanNumeral object, and returns an integer that is the sum of the invoked RomanNumeral, and the other RomanNumeral

Write a main method to test your class. A simple test could look as follows

RomanNumeral x = new RomanNumeral("X");
RomanNumeral y = new RomanNumeral("IX");

System.out.println( x );
System.out.println( y );

Expand your main method to test your other methods.

System.out.print( "Testing for equality: object are " );
if ( !x.equals(y) )
   System.out.print( "not " );

System.out.println( "equal!" );

// Invoke the add method to add two roman numeral objects
// Note the decimal output
System.out.println( x.add(y) );

Task 3: Interacting with Objects and Static APIs

Create a new program TestRomanNumerals.java. This program only needs a main method that we are going to use to write and test our code from our two previous classes.

Much of the code that we will write in this method, we have already written in the main methods local to each class. However, depending on whether or not we are using the static API or creating instance objects, the methods may not be called the same way as they were in the main method of it’s own file. Add code in the main method to accomplish each of the following:

  1. Invoke the add and convert static methods of our static Roman Numeral class.

  2. Create at least two instances of RomanNumeral objects, add them together and print out the results.

  3. Create a list of Roman numeral strings, print out the corresponding list of integers.

  4. Create a list of RomanNumeral objects and print out the corresponding list of integers.

Part 4: Practice Submission (optional)

For the final part of this lab, we are going to review the homework submission process and perform a sample submission of the Java package you have been working on in lab today. Follow the instructions as per your Lab instructor.

Good luck on problem set 2!