CS 112
Spring 2024

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 lab3 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.

A couple of reminders

Working with Roman Numerals

Recall that a long time ago, numbers were represented with Roman Numerals. The value of each Roman Numeral symbol is as follows:

We can represent different numbers by stringing together the symbols. Example:

Given that each symbol has an individual numeric value, we can convert from Roman Numeral to decimal using the following rules:

  1. Reading left to right, symbols with higher values will generally appear first. In this case, 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 by studying a static class, RomanNumeralStatic which provides several static methods that allow us to perform operations on roman numerals. We refer to this class as a static class because we do not have to create an instance of this class to invoke the methods.

After we have reviewed this static API, we will then create an object based version of this same 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 of this class have been written. Further note that one of the methods has been declared to be private and not public. Why do you think this is the case?

  1. The main method contains code which tests the convert method of this class. This method takes in a string representing a Roman Numeral and returns its integer equivalent value.

    The signature of this method is:

    public static int convert(String romanNum)
    

    Following are a few examples of test calls that are provided:

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

    Running your program, you should see the following expected output:

    10
    99
    405
    
  2. The main method also contains code to test the add method of this class. 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)
    

    Following are a few examples of the test calls that are provided:

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

    Running your program, you should see the following expected output:

    20
    416
    100
    

Task 2: Roman Numeral (Object) Class

Classes made up of only static methods are useful, however, they do not 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 object version of this class.

To begin, consider the following:

Thinking through this will help us identify the data we need to store and the methods we need to write.

A Roman Numeral is a string which has a corresponding numeric value. In order to properly represent a Roman Numeral then, each object we create should contain exactly two fields or data members, namely 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 as private instance variables.

  3. Write a constructor that takes in a String which is expected to be a Roman Numeral. Initialize the data members of the class based on the string passed.

    To help you complete this method, consider what methods you have available to you in the RomanNumeralStatic class. Can one of those methods help here? If so, how would you call it?

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

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

  6. Write 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.

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

    RomanNumeral r1 = new RomanNumeral("X");
    RomanNumeral r2 = new RomanNumeral("IX");
    
    System.out.println( r1 );
    System.out.println( r2 );
    

    Expand your main method to test each of the methods you have written. Example:

    System.out.print( "Testing for equality: object are " );
    if ( !r1.equals(r2) )
       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( r1.add(ry) );
    

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 the code we have written in 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 their own class file.

Add code in the main method of this class to accomplish each of the following:

  1. Create two strings of roman numerals. Invoke the add and convert static methods of our static Roman Numeral class, passing the appropriate arguments to add the two roman numeral strings you created and then to see their decimal equivalents.

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

  3. Create an array of Roman numeral strings. For each element of the array, print out the corresponding integer value.

  4. Create an array of RomanNumeral objects. For each element of the array, print out the corresponding integer value.

Task 4: Preparing for MyArray

In lab2 Task 3 you were asked to write a class that implemented methods to process an array of integers. This is a possible solution for this program. As you can see, this is a program class made up of independent static methods, and we did not need to create an object of this class to call the methods. Therefore each method was passed as an argument the array it needed to process.

On ps3 problem 4 you are asked to write a program class which is used as a blue print to create an object containing its own array that can be manipulated by calling the instance methods of the class. To help you with this problem, convert the program written in lab2 into an instance class.

Challenge

Create a new program RomanNumeralGame.java that creates a game that allows you to play with roman numerals objects.

The game should begin by asking for the name of the player. The string input at the keyboard should be used to create an instance of class Name. This implies you need to createa class Name, the represents a persons Name.

For this game, this class simply needs the following attributes:

and methods:

The game is of your design (feel free to adlib) but, can be played something like this:

Sample Run #1:

Welcome to my Roman Numeral Game, what is your name? Christine Kanaris
Hello Christine, would you like to play (yes/no)? no
You are such a chicken Christine ...@&FHF!!!!

Sample Run #2:

Welcome to my Roman Numeral Game, what is your name? Sarah Rogers
Hello Sarah, would you like to play (yes/no)? maybe
Well, Sarah if you cannot follow simple instructions .... &D*&HS%!!!

Sample Run #3:

Welcome to my Roman Numeral Game, what is your name? Amaeli Strong
Hello Amaeli, would you like to play (yes/no)? Yes
You are brave Amaeli!!! 
What is your game name Amaeli? Super Girl
ok, Amaeli from now on you are known as Super Girl!!

Continue to enter roman numerals, when are done, enter quit.

Enter a roman numeral and be amazed: X
Oh that was easy! X has a decimal value of 10!

Enter a roman numeral and be amazed XX
ha Super Girl! Is that supposed to challeng me? XX has a decimal value of 20!

Enter a roman numeral and be amazed: XXVVXCCIIXXV
Thinking, Thinking, Thinking aha 235!!!

Enter a roman numeral and be amazed: 10
DAA!

Enter a roman numeral and be amazed: This!!!game!!!stinks!!
AUTOMATIC DESTRUCTION SEQUENCE ACTIVATED. YOUR DEVICE WILL 
IMPLODE IN TWO MINUTES!!!

Just kidding Super Girl...

Enter a roman numeral and be amazed: quit
Quitting after only 3 tries! loser!

You guys get the idea... Make it your own, make if fun. No one will see it, but you who write it!