Question: Implement examples of naming variables, functions, and classes using 3 different languages. Your program must include examples of: 1. global variable(s) 2. function(s) - show

Implement examples of naming variables, functions, and classes using 3 different languages. Your program must include examples of: 1. global variable(s) 2. function(s) - show how local, nonlocal, and global variables can work together 3. class(es) - a. some fields. b. some methods Your program must use all of the above examples. You must write your program in 3 different languages. One language must have a complete working and tested implementation, while the other two should be as close to working as you can make them. You must choose one language from the following three groups: Group 1: 1. Java 2. C++ 3. C Group 2: 1. python 2. javascript Group 3: any other language not in group 1 or group 2 You are encouraged, but not required, to choose a functional programming language for your third language.

3. Ruby

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

What I so far: This program in C++ should tell me the

amount of pie left (in fraction)

after a certain amount has been taken off.

Use a fraction that has numerator and denominator

DESIGN:

I will have a class called Fraction

contains integers numerator and numerator

that will call a function gcd

Another function called simplify that lowers

the fraction into simplest form by calling the

gcd to determine the gcd of the denominator.

a function called add fraction subtracts the amount

taken off and return the answer as the remainder of pie I have left

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

C++ code:

using namespace std; // Required Class class Fraction { private: int numerator; int denominator; double pieTaken; double remainPie; public: //public methods Fraction(); double simplify(); int gcd(int num, int denom); void setValue(int num, int denom);

}; // Constructor to assign default values to the data members Fraction::Fraction(){ pieTaken = 0.0; remainPie = 100.0; } // simplify() function to lower the fraction into simplest form double Fraction:: simplify(){ double frac = 0.0; int divisor = gcd(numerator, denominator); numerator = numerator / divisor; denominator = denominator / divisor; frac = (double)numerator / (double)denominator; return frac; } // gcd() function to calculate the greatest common divisor using EUCLIDEAN ALGORITHM int Fraction::gcd(int num, int denom){ int greatDiv; // To store the greatest common divisor int rem; if (num == 0) greatDiv = denom; else if (denom == 0) greatDiv = num; else{ rem = num % denom; greatDiv = gcd(denom, rem); } return greatDiv; } // setValue() function to assign user given values to numerator and denominator and calculate remainPie. void Fraction:: setValue(int num, int denom){ numerator = num; denominator = denom; pieTaken = simplify(); cout << " Portion of the Pie taken out: " << pieTaken; cout << endl; if (pieTaken > remainPie){ cout << "You are trying to take our more than what is available!"; exit(0); } else{ remainPie = remainPie - pieTaken;

cout << "Remaining Pie: " << remainPie; cout << endl; }

}

int main() { bool isValid = true; int num, denom; Fraction frac; do{ // Get values for numerator and denominator cout << " Enter the numerator value of fraction: "; cin >> num; cout << "Enter the denominator value of fraction: "; cin >> denom; if (num < 0 || denom < 0){ // If either value is negative isValid = false; cout << " The numerator and denominator must be some positive values. Please try again!"; cout << endl; } if (denom == 0){ // If denominator is 0 isValid = false; cout << " Denominator cannot be 0! Please try again!"; cout << endl; } cout << endl; }while(isValid);

frac.setValue(num, denom); cout << endl; return 0;

}

Please help review this and do it in Javascript and Ruby for me. Thanks

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!