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

What to do: 1. The class definition for Ternary in a text file named ternary.h t

ID: 3889237 • Letter: W

Question

What to do:
1. The class definition for Ternary in a text file named ternary.h that meets the specification
above. Give brief but convincing arguments, as comments, for your choice of class
member access restriction (i.e., public/private). Here is an example of that:

2. In a text file named ternary.cc, implement the constructor functions and the member
function insert. Here is an example of that:

Rational::Rational() {

numerator = 0;

denominator = 1;

}

Rational::Rational(int num, int den){

if (den == 0)

{

denominator = 1;

numerator = num;

cout << "Error: zero(0) as denominator is llegal; using one(1) "

<< "instead" <<endl;

}

else {

numerator = num;

denominator = den;

}

}

Rational Rational::plus(const Rational& r) const{

int c = r.numerator;

int d = r.denominator;

int a = numerator;

int b = denominator;

return Rational(a*d + b*c, b*d);

}

Rational Rational::minus(const Rational&r) const{

int c = r.numerator;

int d = r.denominator;

int a = numerator;

int b = denominator;

return Rational(a*d - b*c, b*d);

}

Rational Rational::times(const Rational&r) const{

int c = r.numerator;

int d = r.denominator;

int a = numerator;

int b = denominator;

return Rational(a*c, b*d);

}

Rational Rational::divide(const Rational&r) const{

int c = r.numerator;

int d = r.denominator;

int a = numerator;

int b = denominator;

return Rational(a*d, b*c);

}

void Rational::insert(ostream& sout){

if (denominator == 1){

sout << numerator;

return;

}

sout << numerator << '/' << denominator;

return;

}

void Rational::extract(istream& sin){

sin >> numerator >> denominator;

return;

}  

3. Write a test program in a file named test ternary.cc to test the implemented member
functions (see, for example, Rational class test program). For the implemented
constructors your test code should show the creation of Ternary objects with dierent
values (negative, positive) for both valid and invalid Ternary objects. Here is an example of that:

#include <iostream>

#include "rational.h"

using namespace std;

int main()
{
  
Rational p,q, r;
Rational();
  
cout << "p = ";
  
p.insert(cout);
  
cout << " q = " ;
  
q.insert(cout);

cout << " r = ";
  
r.insert(cout);

cout << endl;

Rational s(1,0);
  
cout << "s = ";
  
r.insert(cout) ;
  
cout << endl;
  
Rational t(1,1);
cout << "t = ";
  
t.insert(cout) ;
  
cout << endl;
  
cout << "Enter a rational number (numerator denominator): ";
  
p.extract(cin);
  
cout << endl;
  
cout << "Enter a rational number (numerator denominator): ";
  
q.extract(cin);

r = p.plus(q);
  
cout << "The sum of ";
  
p.insert(cout);
  
cout << " and " ;
  
q.insert(cout);
  
cout << " is ";
  
r.insert(cout);

cout << endl;

r = p.minus(q);
  
cout << "The difference of ";
  
p.insert(cout);
  
cout << " and " ;
  
q.insert(cout);
  
cout << " is ";
  
r.insert(cout);

cout << endl;

r = p.times(q);
  
cout << "The product of ";
  
p.insert(cout);
  
cout << " and " ;
  
q.insert(cout);
  
cout << " is ";
  
r.insert(cout);

cout << endl;


r = p.divide(q);

p.insert(cout);
  
cout << " divided by " ;
  
q.insert(cout);
  
cout << " is ";
  
r.insert(cout);

cout << endl;
  
return(0);

}

Note: I do not want you to send me those three files, just the implemented codes inside them, but of course you have to create the required files to make sure everything works properly. Also do not forget to breifly comment on functions etc.

In this assignment you will define, mplement, and test a C++ class called Ternary to represent and use whole numbers (positive or negative) in base-three number system In the decimal system or base-ten number system we have 10 symbols or numerals 0, 1.2····.9 representing values zero, one, two,., nine, respectively. In the ternary number system there are only three symbols 0, 1, 2 representing values zero, one, two, respectively. For a multi-digit whole number e.g., 122, its value in decimal system is computed as (122)1- 1102210210one undred twenty two. In the ternary number system s value ls (122h = 1+32 +2 * 31 + 2+30-1710. Your task is to design, mplement, and test a class named Ternary according to the following specification. A ternary number is represented by its equivalent decimal value in the class. Thus, for example, ternary umber 122)3 is actually stored as the decimal integer 17. Therefore, the only data member in class Ternary is an int object named decimal equivalent Code snippets and their intended meaning given below are used to ustrate the member functions that you need to include in class Ternary 1. Define two different constructors to create Ternary class objects in a consistent state. Ternary a b(122), c(241); // create objects of class type Meaning. Object a is created and the data member decimal equivalent is initial ized with value 0. Object b is a valid ternary number whose decimal value is 17; the data member decimal equivalent is initialized with value 17. Object c is an invalid ternary number and the constructor must give an error message and the data member decimal equivalent is initialized with value 0 2. Other member functions you need to have in class Ternary Ternary d (201) // d is created b-plus (d) ; Ternary class object a contains the sum of Ternary class objects b and b.minus (d) ; a Meaning. a Meaning. Ternary class object a contains the result of Ternary class object d subtracted from Ternary class object b a = b , times (d) ; Meaning. and d. Ternary class object a contains the product of Ternary class objects b a. insert (cout); Meaning. Ternary class object a is displayed on the terminal (standard output) c.extract(cin); Meaning. Ternary class object c is read from the keyboard (standard input)

Explanation / Answer

#include <bits/stdc++.h>

using namespace std;

class Ternary{

int num; // Num is your only private data member

public:

Ternary(){

num = 0;

}

Ternary(int n){ // Convert Base 3 number to Decimal number

int temp = 0, base = 1;

while(n){

int rem = n % 10;

if(rem > 2){ // If anytime rem > 2 number is Invalid

cout << "Invalid Ternary Number : " << rem <<endl;

return;

}

temp += (rem * base);

base *= 3;

n /= 10;

}

num = temp;

}

Ternary plus(const Ternary& r) const;

Ternary minus(const Ternary& r) const;

Ternary times(const Ternary& r) const;

void insert(ostream&);

void extract(istream&);

};

Ternary Ternary::plus(const Ternary& r) const{ // Add number and store it to another number

int a = r.num;

int b = num;

int res = 0 , sum = a+b;

while(sum){ // First convert sum to base 3

res = (res * 10) + sum%3;

sum /= 3;

}

return Ternary(res);

}

Ternary Ternary::minus(const Ternary& r) const{

int a = r.num;

int b = num;

int res = 0 , sub = b - a;

while(sub){ // Convert Subtraction to Base 3

res = (res * 10) + sub%3;

sub /= 3;

}

return Ternary(res);

}

Ternary Ternary::times(const Ternary& r) const{

int a = r.num;

int b = num;

int res = 0 , sub = b * a;

while(sub){ // Convert Times result to Base 3

res = (res * 10) + sub%3;

sub /= 3;

}

return Ternary(res);

}

void Ternary::insert(ostream& out){

out << num << endl;

return;

}

void Ternary::extract(istream& in){

in >> num;

return;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote