Reference no: EM132080031
You need help to write this Java code.
Step 1: The Char type Create a complex type called Char that models the primitive type char. In your class, you are going to want to have the following constructors:
Char(): Default constructor that sets the data section of the class to null (binary 0)
Char(char c): Overload constructor that takes a primitive char as an argument. It should set the data section of the class to the argument.
Char(int c): Overload constructor that takes a primitive int as a parameter. The data section of the class should be set as a character from the argument.
Char(final Char c): Overload constructor that takes the complex Char type as a parameter. The data section of the class should be set with the data section of the argument.
Char(String c): Overload constructor that takes a string type as a parameter. The data section of the class should be set to the first character in the string.
The class should have the following mutators:
void equals(final Char c): sets the data section to the data section of the argument. void equals (char c): sets the data section to the primitive argument.
void equals(int c): Sets the data section of the class as a character from the int argument.
Finally, it should have the following accessor functions:
char toChar( ): Returns the data section of the class as a char.
int toint( ): Returns the data section of the class as an int.
String toString( ): Returns the data section of the class as a string.
String to HexString( ): Returns the data section of the class as a hexadecimal valued string.
String add(char c): Concatenates the data section of the class with the parameter and returns the two characters as a string.
String add(final Char c): Concatenates the data section of the class with the data section of the parameter and returns the two characters as a string.
Step 2: Big Decimal
In Step 1 you created a class called Char that would do a few things with a char primitive type. In this assignment we are going to use the Char class to create another class called BigDecimal. What this means is that you are going to store digit characters in your Char class and then add new instances of the Char class to create a BigDecimal.
The BigDecimal class is going to need a way to hold all of the Char classes that you add to it so you might want to consider using a vector. I realize that the vector class in Java is somewhat going away so if you are using Java you might want to consider using the ArrayList. From this point forward I will refer to ArrayList and vector as a container. This way I don't have to keep typing both.
Your BigDecimal class should have the following constructors
BigDecimal() - default constructor should simply set your contianer to three Char objects that contain the values '0' '.' '0'
BigDecimal(String value) - This constructor will parse the string taking each digit, putting it into a new Char and adding the Char to the container.
Your BigDecimal class should also contain the following mutators
void equals(char ch) - A char that contains a digit
void equals(String value) - This does the same as the overloaded constructor that takes a string BigDecimal add(BigDecimal) - Adds the values together and returns the result as a Big Decimal
BigDecimal sub(BigDecimal) - Subtracts the two values and returns the result as a BigDecimal
Accessors
double toDouble() - Returns the value stored in the container as a double
String toString() - Returns the value store in the container as a string
Char at(int index ) - Returns the value at the particular index as a Char
Caveat:
If you are using C++ your container must hold pointers to the Char type. This means when you push something back you need to do the following:
v.push_back(new Char('A'));
Java you will use references but the principle is the same. Simpy add a new Char to whatever container you are using.
al.add(new Char('A'));
Other requirement Before adding anything to your container you must determine if the value is actually a digit. If it is not then simply stop processing characters at that point. You are also to make sure that not more than one decimal is placed. Basically I am asking you to have a container that has a well formed number
Step 3: File IO
Make a change to your BigDecimal class to add the following functions (methods)
int wholeNumber() - Returns only the whole number portion of the decimal number as an int
double fraction() - Returns the fractional portion of the number as double
File IO:
Attched at this link is a text file called Numbers.txt (29.06367626098403, 42.77383737710885, 18.091887775083933, 26.00580851301579, 27.373385841524737, 22.950838262579364, 47.85003022494251, 16.75963522818282, 25.391047068576892, 41.621203024474184, 38.900829007904846, 14.965562131729882, 47.35966664937554, 34.6930055816334, 21.34083393290269, 24.625973137349234, 36.55015550385545, 28.61793131915871, 47.03503184700058, 22.547129989077735, 10.354255998374677, 40.52960355378197, 15.75798738892007, 41.98802541051023, 21.99447227641024, 12.71895402779139, 49.55661770604053, 26.943287240700332, 21.95282854310205, 17.761331235258112, 34.002798218501866, 47.84746733126361, 27.415554160228197, 46.11786972785617, 13.324341005546945, 23.120903887553567, 39.47614755169381, 49.79141807347378, 10.9731646951434, 30.860109966679794, 15.534548830877451, 43.62518709231083, 28.009830889595854, 21.108498174931487, 28.2754866585608, 40.090046809711126, 48.75388489987637, 11.526037728040004, 42.804356199727316, 13.91424071187037, 16.469792068115883, 27.909261045927728, 39.96432932482341, 39.667083736782445, 11.428556219500905, 15.895850033803791, 26.553672610564945, 18.416038099956552, 29.505409962862505, 21.320833577401142, 42.107587943537894, 48.8343505932619, 21.54430566771293, 34.07427119598432, 37.10419388483666, 23.64427640874803, 12.040217405085665, 13.943061062971594, 18.8995846758003, 44.038699943040534, 10.260600603046823, 46.80237313058899, 19.39219791843417, 22.731791971681012, 17.74766240052677, 35.654991664020585, 32.362062478869966, 30.547148289363765, 43.21339135470416, 13.399660410165598, 18.62578371032339, 11.959677922579303, 19.159640468250792, 32.842365048917856, 23.59345678949312, 11.76529688361792, 34.125837416351125, 44.347266133413854, 34.50899676465603, 18.060331516647715, 30.28722659860124, 12.65683182133535, 23.600692449266532, 15.97031384953457, 28.196863056514214, 31.793519156761526, 38.564105527709465, 22.0756827947849, 28.784868846667454, 10.588169721472148.
Read in each of these numbers and store them in your BigDecimal class. Basically you are going to need a BigDecimal class for each number that is in the file. Store each BigDecimal in some kind of container like a vector or an ArrayList.
Once you have all of the numbers loaded use a loop to write out all of the numbers into two separate text files. One file called wholeNumbers.txt will hold the whole number portion of the number. The other file call fraction.txt will hold the fractional portion of the number. Make sure you include the decimal point.
Open each of the files in Note Pad and make sure that you have one number per line.
Step 4: Exceptions
Create an exception class called CharException to be thrown in the equals function that takes an int as a parameter. You should check the range of the int to make sure it is a valid readable character. Basically if the parameter is less than 32 or greater than 127 you want to throw a CharException with the message "Invalid Character".
Java: Use inheritance to extend your class from Exception. Create a constructor that takes the message to be set. You are going to have to call the super constructor to make sure the message is set properly. Use the getMessage method to display the error message you set.
Finally:
Create a BigDecimalException class that is derived from the CharException class. You should throw this exception anytime that a character is being set that is not a valid character (a digit or a decimal). You should also throw this excpetion if more than one decimal is being set. Use inheritance.