+1 678 648 4277 
Table Of Contents
  • Creating a Complex Type Char

Creating a Complex Type Char

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 Class Methods
C++ Java Description
Char() Char() Default constructor that sets the data section of the class to null (binary 0)
Char(char c) Char(char c) Overloaded constructor that takes a primitive char as an argument. It should set the data section of the class to the argument.
Char(int c) Char(int c) Overloaded 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(const Char &c) Char(final Char c) Overloaded 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); Char(String c) Overloaded 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.

Required Mutators
Your class should have the following mutators:
C++ Java Description
void equals(const Char &c); void equals(final Char c) Sets the data section to the data section of the argument
void equals(char c); void equals(char c); Sets the data section to the primitive argument
void equals(int c) void equals(int c); Sets the data section of the class  as a character from the int argument.
finally, you should have the following accessor functions
Required Accessor Methods
C++ Java Description
char toChar() const; char toChar(); Returns the data section of the class as a char.
inttoInt() const; inttoInt(); Returns the data section of the class as an int.
string toString(); String toString(); Returns the data section of the class as a string
string toHexString(); String toHexString(); Returns the data section of the class as a hexadecimal valued string
string operator + (char c); String add(char c); Concatenates the data section of the class with the parameter and returns the two characters as a string.
string operator + (const Char &c) 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.
Using Type Char to Create Big Decimal Class
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 container 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
Note: If you are using C++ feel free to use the + operator instead of the add function.
I am going to let you off the hook for the multiply and divide but it should be easy to implement.
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
Make a change to your BigDecimal class to add the following functions (methods)
• intwholeNumber() - 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 Input Output
You are to 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 Notepad and make sure that you have one number per line.
Creating an Exception Class
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 get Message method to display the error message you set. Here is a code example that shows main and the output:
publicstaticvoidmain(String[]args)
{
       Char ch=newChar('A');
       Char c =newChar('B');
System.out.println(ch.add(c));
System.out.println(ch.toChar()+" In Hex: "+ch.toHexString());
System.out.println(ch.toChar()+" In Decimal "+ch.toInt());
try
{
System.out.println("Try to set 140 as character");
ch.equals(140);
System.out.println(ch.toChar());
}
catch(CharExceptionce)
{
System.out.println(ce.getMessage);
}
       Char x =new Char(c);
System.out.println(x.toString());
}
AB
A In Hex: 41
A In Decimal 65
Try to set 140 as character
Invalid Character
B
Press any key to continue . . .
C++
Use public inheritance to derive your CharException class from exception. Override the virtual function what (const char *what()) so that it returns the message "Invalid Character". Use the what function to display the error message. Below is and image of main and it's output to give you an idea of how the class should work:FinallyCreate 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 exception if more than one decimal is being set.At this point I am not going to give you many specifics on how to implement this. I will only say that you are to use inheritance. No matter how you implement this class please have good reasoning for doing so. It is time for all of us to start using what we have learned to formulate our own ideas.Important:For this assignment I expect that you will use good coding practices. This means that whenever possible you should create one code path. Having code duplications will be frowned upon and may result in a point reduction in your grade.
intmain()
{
 Char ch('A');
 Char c('B');
 cout<
 cout<
 cout<
 try
 {
  cout<<"Try to set 140 as character"<
  ch.equals(140);
  cout<
 }
 catch (CharException*ce)
 {
  cout<what() <
 }
 return0;
}
AB
A In Hex: 41
A In Decimal 65
Try to set 140 as character
Invalid Character
Press any key to continue . . .
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 exception if more than one decimal is being set.
At this point I am not going to give you many specifics on how to implement this. I will only say that you are to use inheritance. No matter how you implement this class please have good reasoning for doing so. It is time for all of us to start using what we have learned to formulate our own ideas.
Important:
For this assignment I expect that you will use good coding practices. This means that whenever possible you should create one code path. Having code duplications will be frowned upon and may result in a point reduction in your grade.
Code Solution
#include "BigDecimal.h"
#include "BigDecimalException.h"
#include
#include
#include
#include
usingstd::string;
usingstd::stringstream;
BigDecimal::BigDecimal()
{
 data.push_back(new Char('0'));
 data.push_back(new Char('.'));
 data.push_back(new Char('0'));
}
BigDecimal::BigDecimal(string value)
{
 if (!isValidValue(value))
  throwBigDecimalException("Invalid value");
 for (inti = 0; i< (int)value.size(); i++)
  data.push_back(new Char(value[i]));
}
BigDecimal::~BigDecimal()
{
 clear();
}
voidBigDecimal::equals(char ch)
{
 if (ch< '0' || ch> '9')
  throwBigDecimalException("Invalid digit");
 clear();
 data.push_back(new Char(ch));
}
voidBigDecimal::equals(string value)
{
 if (!isValidValue(value))
  throwBigDecimalException("Invalid value");
 clear();
 for (inti = 0; i< (int)value.size(); i++)
  data.push_back(new Char(value[i]));
}
doubleBigDecimal::toDouble()
{
 stringstreamss(toString());
 double value;
 ss>> value;
 return value;
}
Char BigDecimal::at(int index)
{
 return *data[index];
}
stringBigDecimal::toString()
{
 stringstreamss;
 for (inti = 0; i< (int)data.size(); i++)
  ss<< data[i]->toChar();
 returnss.str();
}
BigDecimalBigDecimal::operator + (BigDecimal other)
{
 // Get the integer section of the numbers
 stringstream ss1(toString());
 stringthisInts;
 getline(ss1, thisInts, '.');
 stringthisDecimals;
 getline(ss1, thisDecimals);
 stringstream ss2(other.toString());
 stringthatInts;
 getline(ss2, thatInts, '.');
 stringthatDecimals;
 getline(ss2, thatDecimals);
 // Make the integers equal in length
 while (thisInts.size() != thatInts.size())
 {
  if (thisInts.size()
   thisInts = "0" + thisInts;
  else
   thatInts = "0" + thatInts;
 }
 // Make the decimals equal in length
 while (thisDecimals.size() != thatDecimals.size())
 {
  if (thisDecimals.size()
   thisDecimals += "0";
  else
   thatDecimals += "0";
 }
 // Solve the decimals first
 string sum = "";
 int carry = 0;
 for (inti = (int)thisDecimals.size() - 1; i>= 0; i--)
 {
  intthisOperand = thisDecimals[i] - '0';
  intthatOperand = thisDecimals[i] - '0';
  int sum = thisOperand + thatOperand + carry;
  if (sum >= 10)
  {
   carry = sum / 10;
   sum %= 10;
  }
  sum = (char)(sum + '0') + sum;
 }
 // Solve the integers
 sum = "." + sum;
 for (inti = (int)thisInts.size() - 1; i>= 0; i--)
 {
  intthisOperand = thisInts[i] - '0';
  intthatOperand = thisInts[i] - '0';
  int sum = thisOperand + thatOperand + carry;
  if (sum >= 10)
  {
   carry = sum / 10;
   sum %= 10;
  }
  sum = (char)(sum + '0') + sum;
 }
 if (carry > 0)
  sum = ((char)carry) + sum;
 returnBigDecimal(sum);
}
// Perform subtraction
BigDecimalBigDecimal::operator - (BigDecimal other)
{
 // Get the integer section of the numbers
 stringstream ss1(toString());
 stringthisInts;
 getline(ss1, thisInts, '.');
 stringthisDecimals;
 getline(ss1, thisDecimals);
 stringstream ss2(other.toString());
 stringthatInts;
 getline(ss2, thatInts, '.');
 stringthatDecimals;
 getline(ss2, thatDecimals);
 // Make the integers equal in length
 while (thisInts.size() != thatInts.size())
 {
  if (thisInts.size()
   thisInts = "0" + thisInts;
  else
   thatInts = "0" + thatInts;
 }
 // Make the decimals equal in length
 while (thisDecimals.size() != thatDecimals.size())
 {
  if (thisDecimals.size()
   thisDecimals += "0";
  else
   thatDecimals += "0";
 }
 // Solve the decimals first
 string difference = "";
 bool borrow = false;
 for (inti = (int)thisDecimals.size() - 1; i>= 0; i--)
 {
  intthisOperand = thisDecimals[i] - '0';
  intthatOperand = thisDecimals[i] - '0';
  if (borrow)
  {
   thisOperand -= 10;
   borrow = false;
  }
  if (thisOperand
  {
   thisOperand += 10;
   borrow = true;
  }
  int result = thisOperand - thatOperand;
  difference = (char)(result + '0') + difference;
 }
 // Solve the integers
 difference = "." + difference;
 for (inti = (int)thisInts.size() - 1; i>= 0; i--)
 {
  intthisOperand = thisInts[i] - '0';
  intthatOperand = thisInts[i] - '0';
  if (borrow)
  {
   thisOperand -= 10;
   borrow = false;
  }
  if (thisOperand
  {
   thisOperand += 10;
   borrow = true;
  }
  int result = thisOperand - thatOperand;
  difference = (char)(result + '0') + difference;
 }
 if (borrow)
  throwBigDecimalException("BigDecimal does not support negative values.");
 returnBigDecimal(difference);
}
// Get the integer section of the number
intBigDecimal::wholeNumber()
{
 stringstreamss(toString());
 string line;
 getline(ss, line, '.');
 stringstream ss1(line);
 int value;
 ss1 >> value;
 return value;
}
// Get the fraction section of the number
doubleBigDecimal::fraction()
{
 stringstreamss(toString());
 string line;
 getline(ss, line, '.');
 getline(ss, line);
 stringstream ss1("0." + line);
 double value;
 ss1 >> value;
 return value;
}
// Clear the vector
voidBigDecimal::clear()
{
 for (inti = 0; i< (int)data.size(); i++)
  delete data[i];
 data.clear();
}
// Check if value can be converted to big decimal
boolBigDecimal::isValidValue(string value)
{
 intnumDots = 0;
 for (inti = 0; i< (int)value.size(); i++)
 {
  if (value[i] == '.')
  {
   numDots++;
   continue;
  }
  if (value[i] < '0' || value[i] > '9')
   return false;
 }
 if (numDots> 1)
  return false;
 return true;
}