Week #9 Discussion Details
Project Assignment 10 - Extra Credit
Due to our other section of CIS 3022 not being able to cover all of the material on Project Assignment
10 by the end of lecture Wednesday, October 28, 2009, recursion and UML diagrams will
not be tested on Exam II. Thus, Project Assignment 10 is now optional and will be an
Extra Credit project assignment. I recommend preparing for the material on
Exam II before working on this assignment. We will be picking up with the material related to this
assignment after Exam II.
Collection: class HashSet
The
class HashSet
provides an object type that stores a collection of elements. The elements are stored in no specific
order, compared to an array or ArrayList where the elements are stored in an indexed
sequence. When two iterators of the HashSet are created, there is no guarantee the order of the
elements of the set will be returned the same sequence. The object type of the elements is defined
within the declaration of the collection. Review the Java API documentation of the
class HashSet
to see the structure of the class and the methods provided.
Modifier: static
When a property or method is static, there is only one instance of the property or method for all
instances of the classification. When static, all references to a property see/access/modify the same
property. When static, a method can only use static properties and any variables declared within the
method itself, all references must be within a static context. A static property or method is also
referred to as non-instance or class property or method. The property or method can be accessed by
listing the name of the class followed by a period and the name of the desired property or method.
Examples of this are the usages of static within the
class Math.
The properties of the
class Math,
Math.PI and Math.E relate to the entire classification of mathematics and
not a single instance. The same is true for the methods within the
class Math,
for example Math.abs(int a) and Math.sqrt(double a) also relate to the entire
classification of mathematics and not a single instance.
A non-static property or method is also referred to as an instance property or method. Instance
properties and methods must be accessed through an instance of the classification. Recall an
instance of a classification is created by declaring a variable of the desired class type and then
calling the class constructor to create a specific object instance. Until now, we have focused our
attention on using only instance properties and methods.
Modifier: final
When a variable is declared to be final, the initial value assigned to it is the only value that
can ever be assigned to the variable. It is the final value of the variable.
Control Structures: do while Loops
We have introduced the do while loop which assists our program in repeating the execution of a
set of statements. Here is the
general structure
of the do while loop.
Exercises
Please note, it is likely you will not have the time to complete all of these exercises during your
discussion. They are given to you so that you can review and practice using our recent topics. Use
them as a study aid during the rest of the week to supplement the home work given in the project
assignments.
- Create a HashSet that stores Integer values. Allow the user to add values to the HashSet. Also,
allow the user to print all the values currently within the HashSet. Observe whether or not the
values are printed in the order they were entered.
- Create a method that will copy the values from the HashSet of the previous question to an array.
- Here are .class files for: the
class Number
and the
class RomanNumberalSymbol.
These are solutions to the following problems. They provide a menu system allowing for you to test
multiple options and see different results. Note, you will need both files downloaded into the
same directory and they are .class files. As we have discussed, you can execute the program from
the command prompt/terminal window by entering: java Number
- Create a method that will receive a int value and return a String composed of the String
representations of each digit within the int. List the String values from left to right in the
same order as the digits. For example, if 1234 is passed to the method, "one two three four" is
returned by the method. Also, if 428 is passed to the method, "four two eight" is returned by the
method.
- Create the class RomanNumeralSymbol. The class will represent Roman Numerals. A Roman Numeral is
represented by a symbol: I, V, X, L, C, D, & M, and a corresponding decimal value.
through 10. The corresponding value for each Roman Numeral Symbol is: I = 1, V = 5, X = 10, L = 50,
C = 100, D = 500, M = 1000.
- Create a method that will convert an int value into its Roman Numeral equivalent. In your
solution, only solve numbers up to the decimal value 4000. More history about and examples of
Roman Numerals can be found
here and
here.
Recall these definitions related to Roman Numerals.
- There is no Roman Numeral Symbol that represents 0 by itself.
- Some numbers are represented by a single symbol while others are a combination of symbols.
- The Roman Numerals counting from 1 to 10 are: I = 1, II = 2, III = 3, IV = 4, V = 5,
VI = 6, VII = 7, VIII = 8, IX = 9, and X.
- When listed in decreasing or equivalent order, the values of the combined Roman Numerals are
summed. The same Roman Numeral Symbol (those starting with the digit 1: I, X, C, and M) may
be listed in sequence up to three times in a row. These multiples can be combined with each
other or with the remaining symbols (those starting with the digit 5: V, L, and D) Consider:
- I by istelf: I = 1, II = 2, and III = 3
- I and V combined: VI = 6, VII = 7, VIII = 8
- I and X combined: XI = 11, XII = 12, XIII = 13
- I and X combined with multiples of both: XXII = 22, XXXIII = 33
- I, V, and X combined: XVI = 16, XVII = 17, XVIII = 18
- When listed in partially increasing order, the increased subset pair has the smaller value
decrementing the larger value. Consider:
- I and V, the V will be decreased by the value of I when I is listed before V, yielding IV = 4
- I and X: IX = 9
- X and L: XL = 40
- X and C: XC = 90
- The definitions can then be combined to create:
- I, V and X combinations: XIV = 14, XIX = 19, XXIV = 24, XXIX = 29
<<<< Prev
Next >>>>