Question: Can someone help me change my program written in java to C++? I have the source code ready, but I am having difficulties changing it

Can someone help me change my program written in java to C++? I have the source code ready, but I am having difficulties changing it to C++. PLEASE RUN THE PROGRAM AFTER YOU FINISH CONVERTING TO C++

Here are the instructions listed below followed by the required output and the source code to be updated.

Can someone help me change my program written in java to C++?

I have the source code ready, but I am having difficulties changing

it to C++. PLEASE RUN THE PROGRAM AFTER YOU FINISH CONVERTING TO

C++ Here are the instructions listed below followed by the required output

and the source code to be updated. SOURCE CODE TO BE UPDATED

FROM JAVA TO C++ Main.java --------------------------------------------------------- public class Main{ public static void

main(String[] args) { FractionUtility.menuHw5(); } } ----------------------------------------------------------- FractionUtility.java --------------------------------------------- import java.util.Scanner; classFractionUtility { public static Fraction add( Fraction l, Fraction r) { return

new Fraction(l.getNum() * r.getDenom() + l.getDenom() * r.getNum(), l.getDenom() * r.getDenom()); }

SOURCE CODE TO BE UPDATED

FROM JAVA TO C++

Main.java

---------------------------------------------------------

public class Main{

public static void main(String[] args) {

FractionUtility.menuHw5();

}

}

-----------------------------------------------------------

FractionUtility.java

---------------------------------------------

import java.util.Scanner;

class FractionUtility {

public static Fraction add(

Fraction l, Fraction r) {

return new Fraction(l.getNum() * r.getDenom()

+ l.getDenom() * r.getNum(), l.getDenom() * r.getDenom());

}

public static Fraction subtract(

Fraction l, Fraction r) {

return new Fraction(l.getNum() * r.getDenom()

- l.getDenom() * r.getNum(), l.getDenom() * r.getDenom());

}

public static Fraction multiply(

Fraction l, Fraction r) {

return new Fraction(l.getNum() * r.getNum(),

l.getDenom() * r.getDenom());

}

public static Fraction divide(

Fraction l, Fraction r) {

return new Fraction(l.getNum() * r.getDenom(),

l.getDenom() * r.getNum());

}

public static void print(Fraction f) {

System.out.println(" num: " + f.getNum() +

" denom: " + f.getDenom());

}

public static void init(Fraction[] fAry) {

int option;

int subOption;

int n;

int d;

Scanner scanner = new Scanner(System.in);

do {

System.out.print(" ***************** " +

"* 1. Initialize * " +

"* 2. Update * " +

"* 3. Cancel * " +

"***************** " +

"Enter your option (1, 2 or 3): ");

option = scanner.nextInt();

switch (option) {

case 1:

System.out.print(" Initializing Left Fraction -- " +

"Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print("Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[0] = new Fraction(n, d);

System.out.println(" Left Fraction --");

fAry[0].print();

System.out.print(" Initializing Right Fraction --" +

" Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print("Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[1] = new Fraction(n, d);

System.out.println(" Right Fraction --");

fAry[1].print();

System.out.println(" Finished Initializing --");

break;

case 2:

if (fAry[0] != null) {

do {

System.out.print(

" ***************************** " +

"* 1. Update Left Fraction * " +

"* 2. Update Right Fraction * " +

"* 3. Update Both * " +

"* 4. Cancel * " +

"***************************** " +

"Enter your option (1, 2, 3 or 4): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

System.out.print(

" Updating Left Fraction -- " +

"Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print(

"Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[0].update(n, d);

System.out.println(

" Left Fraction --");

fAry[0].print();

System.out.println(

" Finished Updating --");

break;

case 2:

System.out.print(

" Updating Right Fraction -- " +

"Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print(

"Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[1].update(n, d);

System.out.println(

" Right Fraction --");

fAry[1].print();

System.out.println(

" Finished Updating --");

break;

case 3:

System.out.print(

" Updating Left Fraction -- " +

"Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print(

"Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[0].update(n, d);

System.out.println(

" Left Fraction --");

fAry[0].print();

System.out.print(

" Updating Right Fraction -- " +

"Enter the Numerator: ");

n = scanner.nextInt();

do {

System.out.print(

"Enter the Denominator" +

"(cannot be 0): ");

d = scanner.nextInt();

} while (d == 0);

fAry[1].update(n, d);

System.out.println(

" Right Fraction --");

fAry[1].print();

System.out.println(

" Finished Updating --");

break;

case 4:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 4);

}

else {

System.out.println(

" NO FRACTION... PLEASE INITIALIZE");

}

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (option != 3);

}

public static void menuHw5() {

int option;

int subOption;

Fraction left = null;

Fraction right = null;

Fraction result = null;

Fraction[] fAry = new Fraction[2];

Scanner scanner = new Scanner(System.in);

do {

System.out.print(" ********************* " +

"* MENU - Hw #4 * " +

"* 1. Initializing * " +

"* 2. Adding * " +

"* 3. Subtracting * " +

"* 4. Multiplying * " +

"* 5. Dividing * " +

"* 6. Printing * " +

"* 7. Quit * " +

"********************* " +

"Enter your option (use integer value only): ");

option = scanner.nextInt();

switch (option) {

case 1:

System.out.println(" INITIALIZING Option -- ");

fAry[0] = left;

fAry[1] = right;

FractionUtility.init(fAry);

left = fAry[0];

right = fAry[1];

result = null;

break;

case 2:

if (left != null) {

System.out.println(" ADDING Option -- ");

do {

System.out.print(

" ******************************** " +

"* ADDING MENU * " +

"* (leftOp, rightOp, result) * " +

"* 1. add() - Member * " +

"* 2. add() - Stand Alone * " +

"* 3. Return to previous MENU * " +

"******************************** " +

"Enter your option (1, 2 or 3): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

System.out.println(

" Calling member add() --");

result = left.add(right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 2:

System.out.println(

" Calling stand-alone add() --");

result = add(left, right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 3);

}

else

System.out.println(" NO FRACTION --");

break;

case 3:

if (left != null) {

System.out.println(" SUBTRACTING Option -- ");

do {

System.out.print(

" ********************************* " +

"* SUBTRACTING MENU * " +

"* (leftOp, rightOp, result) * " +

"* 1. subtract() - Member * " +

"* 2. subtract() - Stand Alone * " +

"* 3. Return to previous MENU * " +

"********************************* " +

"Enter your option (1, 2 or 3): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

System.out.println(

" Calling member subtract() --");

result = left.subtract(right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 2:

System.out.println(

" Calling stand-alone" +

" subtract() --");

result = subtract(left, right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 3);

}

else

System.out.println(" NO FRACTION --");

break;

case 4:

if (left != null) {

System.out.println(" MULTIPLYING Option -- ");

do {

System.out.print(

" ******************************** " +

"* MULTIPLYING MENU * " +

"* (leftOp, rightOp, result) * " +

"* 1. multiply() - Member * " +

"* 2. multiply() - Stand Alone * " +

"* 3. Return to previous MENU * " +

"********************************* " +

"Enter your option (1, 2 or 3): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

System.out.println(

" Calling member multiply() --");

result = left.multiply(right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 2:

System.out.println(

" Calling stand-alone" +

" multiply() --");

result = multiply(left, right);

System.out.println(

" Result Fraction --");

result.print();

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 3);

}

else

System.out.println(" NO FRACTION --");

break;

case 5:

if (left != null) {

System.out.println(" DIVIDING Option -- ");

do {

System.out.print(

" ******************************** " +

"* DIVIDING MENU * " +

"* (leftOp, rightOp, result) * " +

"* 1. divide() - Member * " +

"* 2. divide() - Stand Alone * " +

"* 3. Return to previous MENU * " +

"******************************** " +

"Enter your option (1, 2 or 3): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

if (right.getNum() != 0) {

System.out.println(

" Calling member" +

" divide() --");

result = left.divide(right);

System.out.println(

" Result Fraction --");

result.print();

}

else

System.out.println(

" CANNOT DIVIDE!!" +

"The right operand is 0!!");

break;

case 2:

if (right.getNum() != 0) {

System.out.println(

" Calling stand-alone " +

"divide() --");

result = divide(left, right);

System.out.println(

" Result Fraction --");

result.print();

}

else

System.out.println(

" CANNOT DIVIDE!!" +

"The right operand is 0!!");

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 3);

}

else

System.out.println(" NO FRACTION --");

break;

case 6:

if (left != null) {

System.out.println(" PRINTING Option -- ");

do {

System.out.print(

" ******************************** " +

"* PRINTING MENU * " +

"* (leftOp, rightOp, result) * " +

"* 1. print() - Member * " +

"* 2. print() - Stand Alone * " +

"* 3. Return to previous MENU * " +

"******************************** " +

"Enter your option (1, 2 or 3): ");

subOption = scanner.nextInt();

switch (subOption) {

case 1:

System.out.println(

" Calling member print() --");

System.out.println(

" Left Fraction --");

left.print();

System.out.println(

" Right Fraction --");

right.print();

System.out.println(

" Result Fraction --");

if (result != null)

result.print();

else

System.out.println(" null");

break;

case 2:

System.out.println(

" Calling stand-alone" +

" print() --");

System.out.println(

" Left Fraction --");

print(left);

System.out.println(

" Right Fraction --");

print(right);

System.out.println(

" Result Fraction --");

if (result != null)

print(result);

else

System.out.println(" null");

break;

case 3:

break;

default:

System.out.println(" Wrong option!");

}

} while (subOption != 3);

}

else

System.out.println(" NO FRACTION --");

break;

case 7:

System.out.println(" Having fun!");

break;

default:

System.out.println(" Wrong option!");

}

} while (option != 7);

}

}

-----------------------------------------------------------------------------------------------

Fraction.java

-----------------------------------------------

class Fraction {

private int num;

private int denom;

public Fraction() {

num = 0;

denom = 1;

}

public Fraction(int n, int d) {

int gcd;

if (n != 0) {

num = n;

denom = d;

gcd = getGCD();

if (d

num = -n / gcd;

denom = -d / gcd;

}

else {

num = n / gcd;

denom = d / gcd;

}

}

else {

num = 0;

denom = 1;

}

}

public Fraction add(Fraction f) {

return new Fraction(num * f.denom + denom * f.num,

denom * f.denom);

}

public Fraction subtract(Fraction f) {

return new Fraction(num * f.denom - denom * f.num,

denom * f.denom);

}

public Fraction multiply(Fraction f) {

return new Fraction(num * f.num, denom * f.denom);

}

public Fraction divide(Fraction f) {

return new Fraction(num * f.denom, denom * f.num);

}

public int getNum() {

return num;

}

public int getDenom() {

return denom;

}

public void setNum(int n) {

int gcd;

if (n != 0) {

num = n;

gcd = getGCD();

num /= gcd;

denom /= gcd;

}

else {

num = 0;

denom = 1;

}

}

public void setDenom(int d) {

int gcd;

if (num != 0) {

denom = d;

gcd = getGCD();

if (denom

num = -num / gcd;

denom = -denom / gcd;

}

else {

num = num / gcd;

denom = denom / gcd;

}

}

else {

denom = 1;

}

}

public void print() {

System.out.println(" num: " + num + " denom: " + denom);

}

public void update(int n, int d) {

int gcd;

if (n != 0) {

num = n;

denom = d;

gcd = getGCD();

if (d

num = -n / gcd;

denom = -d / gcd;

}

else {

num = n / gcd;

denom = d / gcd;

}

}

else {

num = 0;

denom = 1;

}

}

public int[] getCommonDigit() {

int[] returnAry;

int[] numDigitAry;

int[] denomDigitAry;

int tmp;

int size = 0;

tmp = num

numDigitAry = new int[10];

do {

numDigitAry[tmp % 10] = 1;

tmp /= 10;

} while (tmp != 0);

tmp = denom;

denomDigitAry = new int[10];

do {

denomDigitAry[tmp % 10] = 1;

tmp /= 10;

} while (tmp != 0);

for (int i = 0; i

if (numDigitAry[i] == 1 && denomDigitAry[i] == 1) {

size++;

}

}

returnAry = new int[size];

if (size != 0) {

for (int i = 0, j = 0; i

if (numDigitAry[i] == 1 && denomDigitAry[i] == 1) {

returnAry[j] = i;

j++;

}

}

}

return returnAry;

}

protected final int getGCD(int a, int b) {

if (b != 0)

return getGCD(b, a % b);

else

return a;

}

protected final int getGCD() {

int a = num

int b = denom

int tmp;

while (b != 0) {

tmp = a % b;

a = b;

b = tmp;

}

return a;

}

}

Follow the instruction to Update the fraction class you already have for your C++ assignment and get the output given at the end of the instructions. 1. All class constructors for your Fraction class have to handle the initialization appropriately There must be as least 2 constructors of (i) Default; and (ii) Copy Each constructor should print the confirmation (e.g., "Calling Fraction (", or "Calling Fraction (const Fraction&)"). 2. Provide a destructor with a confirmation when removing the object (i.e., "Calling "Fraction (0"). 3. Provide get/set member functions for each private member data. 5. A member function print ) that will print the current Fraction object I. Provide the following member functions, a. A function add to add a Fraction object; and b. A function subtract (to subtract a Fraction object; and C. A function multiply ( to multiply a Fraction object; and d. A function divide () to divide a Fraction object; and II. Provide the following member operator functions, e. A function operator to assign a Fraction object. f. A function operator+ to add a Fraction object; and g. A function operator to subtract a Fraction object; and h. A function operatorto multiply a Fraction object; and i. A function operator/() to divide a Fraction object; and III. Provide the following stand-alone functions, .A function init () to set up or update the 2 required Fraction objects. k. A function add to add 2 Fraction objects; and I. A function subtract () to subtract 2 Fraction objects; and m. A function multiply ( to multiply 2 Fraction objects; and n. A function divide () to divide 2 Fraction objects; and o. A function print 0 to print the required Fraction objects; and p. An appropriate menu () function to produce the required output as displayed below

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!