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
-
Attendance in lab is not dependent on whether you finish the lab. Feel free to work on the lab at your own pace to get the most out of the material. Solutions will always be posted by Monday evening and you can check your solutions with those posted.
-
Working in pairs or small groups in lab is encouraged. We can learn much from each other, so feel free to pair up and we can create break out rooms for you to work with your partner.
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:
- I = 1
- V = 5
- X = 10
- L = 50
- C = 100
- D = 500
- M = 1000
We can represent different numbers by stringing
together the symbols.
Example:
- IX = 9
- VI = 6
- XX = 20
Given that each symbol has an individual numeric value, we can convert from Roman Numeral to decimal using the following rules:
-
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.
-
If a single lower value is placed left of a higher value letter, it is subtracted from the total instead.
-
Only one single lower value can be subtracted from a higher value.
-
”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?
-
The
main
method contains code which tests theconvert
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
-
The
main
method also contains code to test theadd
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:
- What is a Roman Numeral?
- 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.
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:
-
Create a new file called
RomanNumeral.java
. -
Declare the
data members
of the class as private instance variables. -
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? -
Write a
toString
method that returns the Roman Numeral representation of the decimal number. -
Write an
equals
method that checks if two RomanNumerals are equivalent. -
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. -
Write a
main
method to test your class. A simple test could look as followsRomanNumeral 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:
-
Create two strings of roman numerals. Invoke the
add
andconvert
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. -
Create at least two instances of RomanNumeral objects, add them together and print out the results.
-
Create an
array
of Roman numeral strings. For each element of the array, print out the corresponding integer value. -
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:
- first name
- last name
- game name
- display name
and methods:
- constructor(s)
- toString method
- mutator methods (as needed)
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!