Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CPSCI 121 Object Oriented Programming Spring 2017 Program #10 due Thursday, 4/20

ID: 3820112 • Letter: C

Question

CPSCI 121 Object Oriented Programming Spring 2017 Program #10 due Thursday, 4/20 by 11:00 pm on Titanium Write a class, called Number, that will hold one integer value in the range of 0-999. Write your code in three files; the class declaration, the class definitions, and a main program that will test all of the class functions. When finished, compress these 3 files cpp files and 1.h file) into a zipped file and submit the zip file. Since you are writing the main and the class, the exact function names are up to you. The data field must be private, but all functions will be public. Write operators as member functions when possible. Class Number specifications: Private data members: Value field an integer in the range of 0-999 Public member functions: default constructor: Sets the number to be 0 constructor receiving a value: Checks the parameter value to make sure that it is in the right range. Ifit is in the right range, store it to the private data member. Store a value of 0 or end the program if the parameter value is bad. copy constructor: Receive one Number object as a parameter and make the new object a copy of that parameter object. Set function: This function receives an integer value. If the parameter value is in the range of0-999, store it to the private data member. Store a value of 0 or end the program if the parameter value is bad Get function: Return a copy of the private data field. Print function Writes out the Number object's private data value in word form. (ex. 938 would be written out as nine hundred thirty eight 512 would be written out as five hundred twelve Operators Receives a Number parameter and retums another Number that has the sum of the two objects' private data (ex. A B will retum an object with the sum of A's value and B's value as its private data value). Same as but subtract instead of add. Receives a Number object and returns true if the left hand operand's value is greater than the right hand operand's value. Otherwise it returns false. Receives a Number object and returns true ifthe left hand operand's value is less than or equal to the right hand operand's value. Otherwise it returns false. prefix Should add one to the private data field and retum a copy of the changed object. ++-postfix Should add one to the private data field and return a copy

Explanation / Answer

Header File

class Number;
class Number
{
private:
int a;
public:
Number();
Number(int m);
Number(Number &m);
void Set(int m);
int Get();
void Print(char *num);
Number operator + (Number &m);
Number operator - (Number &m);
int operator > (Number &m);
int operator <= (Number &m);
Number& operator++();
Number operator++(int);
void disp();
};

Program1

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

Number::Number()
{
a=0;
}

Number::Number(int m)
{
if(m>=0 && m<=999)
a=m;
else
{
a=0;
exit(0);
}
}

Number::Number(Number &m)
{
a=m.a;
}

void Number::Set(int m)
{
if(m>=0 && m<=999)
{
a=m;
}
else
{
a=0;
exit(0);
}
}

int Number::Get()
{
return a;
}

void Number::Print(char *num)
{
int len = strlen(num);

if (len == 0) {
   cout<<"empty string ";
   return;
}
if (len > 4) {
   cout<<"Length more than 4 is not supported ";
   return;
}

char *single_digits[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char *two_digits[] = {"", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char *tens_multiple[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
char *tens_power[] = {"hundred", "thousand"};

cout<<" "<<num;

if (len == 1) {
   cout<<" "<<single_digits[*num - '0'];
   return;
}

while (*num != '') {

   if (len >= 3) {
   if (*num -'0' != 0) {
       cout<<single_digits[*num - '0'];
       cout<<tens_power[len-3]; // here len can be 3 or 4
   }
   --len;
   }

   else {
       if (*num == '1') {
       int sum = *num - '0' + *(num + 1)- '0';
       cout<<" "<<two_digits[sum];
       return;
   }

   else if (*num == '2' && *(num + 1) == '0') {
       cout<<"twenty ";
       return;
   }

   else {
       int i = *num - '0';
       cout<<tens_multiple[i];
       ++num;
       if (*num != '0')
       cout<<single_digits[*num - '0'];
   }
   }
   ++num;
}
}

Number Number::operator + (Number &m)
{
m.a=m.a+a;
return m;
}

Number Number::operator - (Number &m)
{
m.a=m.a-a;
return m;
}

int Number::operator > (Number &m)
{
if(m.a>a)
return 1;
else
return 0;
}

int Number::operator <= (Number &m)
{
if(m.a<=a)
return 1;
else
return 0;
}

Number& Number::operator++()
{
a++;
return *this;
}

Number Number::operator++(int)
{
Number temp = *this;
++*this;
return temp;
}

void Number::disp()
{
char str[4];
itoa(Get(),str,4);
Print(str);
}

Program2

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
clrscr();
Number ob1,ob2(124),ob3,ob4;
ob4.Set(111);
ob3=ob2;
ob1++;
++ob2;
ob3+ob4;
ob3-ob4;

ob1.disp();
ob2.disp();
ob3.disp();
ob4.disp();
cout<<" "<<ob3>ob4;
cout<<" "<<ob3<=ob4;
getch();
}