CSE1OOF Class Implementation Assessment 2

You will, using object-oriented principles, design a class from a problem description, document that design with a UML diagram, and implement it as a working Java program. Solving it will help you find the answer to life, the universe, and everything! (It’s BQ, of course.)

Specifically, you are tasked with implementing a class that will convert numbers to and from base-26 (Hexaicosadecimal).

 

The Problem

You’ll all be familiar with our normal base-10 (decimal) number system:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

You should also be familiar with base-2 (binary), in which there are only two values:

0, 1

There are several other number systems that are important to computer scientists, including base-8 (octal):

And base-16 (hexadecimal):

0, 1, 2, 3, 4, 5, 6, 7

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f

I’ve got a typewriter on which, unfortunately, all the number keys are broken. But I still want to do maths! Luckily, all the letter keys work, so I’ve got a handy set of 26 symbols laying around. So I’m in luck: All I have to do is write out my numbers in base-26 (hexaicosadecimal)!

 

a 0 h 7 o 14 v 21
b 1 i 8 p 15 w 22
c 2 j 9 q 16 x 23
d 3 k 10 r 17 y 24
e 4 l 11 s 18 z 25
f 5 m 12 t 19    
g 6 n 13 u 20    

Now, I’ve gotten pretty good at thinking in hexaicosadecimal, but I keep getting bad grades in maths because nobody else can understand it! Your job is to help me out by writing a Java program that can convert numbers from base-10 to base-26 and vice versa.

Part I: Create your “test harness” (10 marks)

We’ll get to maths in a minute. First, you should create the main() function that you can use to test your program. Create a new class called HexiacosadecimalNumber. Within that class, create your main() function. The main function should behave as follows:

First, it should ask the user to indicate what mode it should be in. If the user types ‘h’ or ‘H’, the program should ask them to input a hexaicosadecimal number (that is, a String). The program should then read that number into a variable, and print it back to the command line (for now). If the user types ‘d’ or ‘D’, the program should ask them to input a decimal number (that is, a double). The program should then read that number into a variable, and print it back to the command line (for now). If the user types ‘q’ or ‘Q’, the program should exit. If the user types anything else, the program should display the message “INVALID INPUT”, and then prompt the user again. The program should then ask the user again what mode it should be in, and repeat the process until the user types ‘q’ or ‘Q’ at the prompt.

To accomplish this, you will need to use a Scanner object in combination with a loop containing an if-else block.

Part II: Add your attributes and methods (10 marks)

Your HexaicosadecimalNumber class should know how to express a number in both hexaicosadecimal and decimal. To do this, it should have two attributes: stringRepresentation and doubleRepresentation. (What types should these variables be, and what should their access modifiers be?)

Your HexaicosadecimalNumber class should have two constructors, both of which will populate the two attributes appropriately. One will take as input a String (representing a number in hexaicosadecimal), and the other will take as input a double. Both methods have to populate both attributes.

You might now be thinking, “How do I populate both attributes when I only have one representation as input to the constructor?” You will need two helper functions: hexaicosadecimalStringToDouble(String in) and doubleToHexaicosadecimalString(double in). You should call these functions in your constructors as needed.

You should also override the toString() method of HexaicosadecimalNumber. This is a special method that belongs to Java’s Object() class, from which all other classes – including HexaicosadecimalNumber – are derived. The signature of the toString() method is

public String toString()

 As you can see from the method signature, this function is public, takes no arguments, and returns a String. This function is called automatically when an object of that class is printed, for example by System.out.println().The string should be in the following format:

stringRepresentation (doubleRepresentation)

That is, if your hexaicosadecimal number object is named temp and contains the value abcde (for which the equivalent decimal representation is 19010.0; check this later!), the result of calling

System.out.println(temp)

 should be

 

abcde (19010.0)

 For the two “helper” functions - hexaicosadecimalStringToDouble(String in) and doubleToHexaicosadecimalString(double in) – just have them return “dummy” values for now. (I.e. return 0.0 from the first function, and “NOT IMPLEMENTED” from the second.) At this point, you should have a complete working program (that doesn’t do much). Compile and run your program. If you encounter errors, fix them before continuing.

Part III: Convert from a natural hexaicosadecimal number to a double (15 marks)

Your next step is to convert a “natural” (that is, whole and zero or positive) hexaicosadecimal number to its equivalent decimal representation. (Where should this code go? Hint: You shouldn’t create any more methods than you already have.) We’ll start with just natural numbers for now to keep things simple, but eventually we’ll be dealing with hexaicosadecimal numbers that are floating point – like floating.point – and/or negative – like -negative.

The algorithm for doing this might look something like:

accumulator = 0

for each character in the input String from left to right

baseValue = the integer equivalent of the current character

accumulator += baseValue * 26length of the whole String – (index of the current character + 1)

return accumulator

 This is relatively straightforward using appropriate methods from the String class and the Math class, but computing “the integer equivalent of a character” can be a little bit tricky if you’ve never seen it before.

One thing to note is that characters in Java use the Unicode standard, which in turn contains the ASCII standard (a table of ASCII characters can be found here: https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html)

Another thing to note is that characters in Java are equivalent to their character code. (That is,

char c = ‘a’; is exactly the same as char c = 97.)

A third thing to note is that the characters in ASCII are arranged in order. (That is, a = 97, b = 98, c = 99…z = 122).

A fourth thing to note is that while ‘a’ (97) and ‘A’ (65) have different character codes, you already know a convenient function that can ensure that your hexaicosadecimal string is all lowercase (or all uppercase, if you prefer), regardless of how it was typed in.

If you take all these points together, you should be able to extract a single character from the input string and convert it to its equivalent hexaicosadecimal value (That is, a/A = 0, b/B = 1, c/C

= 2, … z/Z = 25) simply. (By “simply” I mean one or two short lines of code; if you find yourself wanting to use more than this, perhaps go back to the drawing board.)

Part IV: Convert from a floating point hexaicosadecimal number to a double (20 marks)

The next step is to expand our hexaicosadecimal converter to take not just natural numbers, but floating point (a/k/a rational) numbers. The good news is that you’re already halfway done, with your work from the previous section!

What you need to do is wrap your code from the previous section in an if-else block. IF (the input string does not contain a decimal point), do exactly what you did before. IF (the input string DOES contain a decimal point), do what you did before, except where your code had “the length of the whole string”, you’ll instead need “the length of the part of the string that comes before the decimal point.” That will get you the whole number part of the number. Then you’ll need a second loop that looks just at the part of the string that is to the right of the decimal point, and adds those values into the accumulator. (Hint: Negative exponents correspond to dividing instead of multiplying. That is, 10-1 = 0.1 and 26-1 = 0.03846, 10-2 = 0.01 and 26-1 = 0.001479, and so on.)

Part V: Convert from a NEGATIVE WHOLE NUMBER OR FLOATING POINT hexaicosadecimal number to a double (5 marks)

Extend your solution from Part VI to enable conversion of NEGATIVE (that is, beginning with “-“) hexaicosadecimal numbers to doubles.

Expert's Answer

Chat with our Experts

Want to contact us directly? No Problem. We are always here for you

Professional

Online Tutoring Services

17,148

Orders Delivered

4.9/5

5 Star Rating

748

PhD Experts

 

Amazing Features

Plagiarism Free

Top Quality

Best Price

On-Time Delivery

100% Money Back

24 x 7 Support

Ask a New Question
*
*
*
*
*

TOP

  Connect on WHATSAPP: +61-416-195006, Uninterrupted Access 24x7, 100% Confidential

X