Programming Assignment II - Due: Wednesday July 5, 2000
Object Oriented Programming
This short assignment is designed to give you experience with the concepts
of class and instance variables, class and instance methods, and
constructors, and how they work together.
Evariste decides that in order to gain more understanding of Object Oriented Programming he is going to write a small
class called Binary. He will have five instance variables in his class.
| original |
of type String |
| quadra |
of type String |
| octal |
of type String |
| hexadecimal |
of type String |
| decimal |
of type int |
He will have four instance methods in his class.
| toQuadra |
accepts no parameters and returns type String |
| toOctal |
accepts no parameters and returns type String |
| toDecimal |
accepts no parameters and returns type int |
| toHexadecimal |
accepts no parameters and returns type String |
| toString |
returns the String equivalent of this class (we will discuss this topic Tuesday and
Wednesday) |
He will have one class method called equiv that is overloaded.
The method will accept two, three or four parameters. It will return type String.
The constructor for Evariste's class will accept one parameter of type String.
The purpose of Evariste's class is to accept a binary number written as a string. Each of the instance methods will be
used to convert that binary number into the appropriate number system. When the constructor is called, its job will be to
give a value to original, and then call each of the four conversion instance methods to give values to the instance
variables, quadra, octal, decimal, and hexadecimal.
The method equiv will be used to accept two, three, or four bits (characters) and convert that group of bits to the
appropriate equivalent. For example, if the bits we send to one implementation of the method are ``1'',``1'', and ``1'',
the
method would recognize that since we are sending it three bits, we want the octal equivalent, so it would return the
String ``7''.
The method toString will be used to prepare a formatted result. The output this method will produce will be
| Binary |
QuadraDecimal |
Octal |
Decimal |
Hexadecimal |
| 1010 |
22 |
12 |
10 |
A |
Evariste has created a new number system called the quadradecimal number system which has only the digits 0, 1, 2, and 3.
He has decided that the easiest way in which to convert a binary number to other systems is to use the methods available
in the String class.
He decides the easiest way to proceed with converting a binary number to a quadradecimal is with the following algorithm.
Assume that there is a binary number stored in the String original.
1. Let l = the length of the string original.
2. Let n = the integer part of the quotient l/2.
(He decides on this because like the conversion
from binary to octal and hexadecimal he must
take groups of bits. Since the base of the
quadradecimal system, 4, is 22, he must take
groups of two binary digits and convert them to
their quadradecimal equivalent).
He knows now that there are n groups of 2 bits
in original. Now he must determine is there are
any bits left over.
3. Let r = the remainder in the division l/2.
If r is 0 he knows that there are exactly n
groups of 2 bits in original and no more. If r
is 1 he knows that there are n groups of 2 bits
in original and 1 left over. If there is one left
over he must accomodate for this.
4. if (r == 1)
get quadradecimal equivalent of ``0" and
the first character in original and store the
equivalent in an output string.
Now that he has accounted for the first character
if there is one left over, he can now go through
the rest of the string taking groups of two bits
and converting them to their quadradecimal equivalent.
If r was 1, then he will start breaking up the
string into groups of 2 beginning with position 1.
If r was 0, then he will start breaking up the
string into groups of 2 beginning with position 0.
5. for (initialize counter to r; as long as counter
is less than length of original; increment counter by 2) {
Get quadradecimal equivalent of two characters of original beginning
at position counter.
concatenate the equivalent into the output string.
}
6. Return the output string.
Evariste decides he can carry out the conversion from binary to octal and hexadecimal in the same way.
He decides the conversion from binary to decimal is even easier.
Your task is to write the class Binary and have it carry out the proposed tasks. The class you write should contain a
main method in which you instantiate the class, and make a call to its toString method to produce the formatted output.