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

Implement examples of naming variables, functions, and classes using Javascript

ID: 3838812 • Letter: I

Question

Implement examples of naming variables, functions, and classes using Javascript.

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.

MY DESCRIPTION:

This program I 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.

Explanation / Answer

1) //Complete working program In java language
// save this program using fraction.java

public class fraction {

   // Stores the reduced numerator and denominator of the fraction object.
   private int numerator;
   private int denominator;
  
   final private static double EPSILON = 0.0000001;
  

   // Creates a new fraction object with the same value as n/d.
   public fraction(int n, int d) {
       numerator = n;
   denominator = d;
   reduce();
   }

   // Creates a fraction object with a value of realval within .01.
   public fraction(double realval) {
       numerator = (int)(100*realval);
       denominator = 100;
       reduce();
   }
  
   // Returns the real value of the current object.
   public double getRealVal() {
       return (double)numerator/denominator;
   }

   // Reduces a current object to lowest terms.
   private void reduce() {
       int common = gcd(numerator, denominator);
    numerator /= common;
    denominator /= common;
   }

   // Returns the greatest common divisor of a and b.
   private static int gcd(int a, int b) {
   if (a == 0) return b;
   if (b == 0) return a;
   if (a > b) return gcd(b, a%b);
   return gcd(a, b%a);
   }
  
   // Returns a new fraction object that is the sum of the current object
   // and f.
   public fraction add(fraction f) {
      
       // Determine the numerator and denominator achieved with a common
       // denominator.
       int num = this.numerator*f.denominator + this.denominator*f.numerator;
       int den = this.denominator*f.denominator;
      
       // Return the new object - the constructor will reduce it.
       return new fraction(num, den);
   }
  
  
   public fraction add(int n) {
       int num = this.numerator+this.denominator*n;
       return new fraction(num, denominator);
   }
  
  
   // Returns a new fraction object that is the difference of the current object
   // and f.
   public fraction sub(fraction f) {
       int num = this.numerator*f.denominator - this.denominator*f.numerator;
       int den = this.denominator*f.denominator;
       return new fraction(num, den);
   }
  
   // This is an alternate implementation of the sub method above.
   public fraction sub2(fraction f) {
       fraction temp = new fraction(-1,1);
       return this.add(temp.multiply(f));
   }
  
   // Returns a new fraction object that is the product of the current object
   // and f.
   public fraction multiply(fraction f) {
       int num = this.numerator*f.numerator;
       int den = this.denominator*f.denominator;
       return new fraction(num, den);
   }
  
   // Returns a fraction object that is equal to the current object raised to
   // the exp power.
   public fraction power(int exp) {
       fraction ans = new fraction(1,1);
      
       // Multiply the current object exp times, storing the answer in ans.
       for (int i=0; i<exp; i++)
           ans = ans.multiply(this);
       return ans;
   }
  
   // Returns a new fraction object that is the reciprocal of the current
   // object.
   public fraction reciprocal() {
       return new fraction(denominator, numerator);
   }
  
   // Returns a new fraction object that is the current object divided by f.
   public fraction divide(fraction f) {
       fraction bottom = f.reciprocal();
       return this.multiply(bottom);
   }
  
  
   // Returns a String representation of the current object.
   public String toString() {
       return numerator+"/"+denominator;
   }
  
   public Object clone() {
       return new fraction(numerator, denominator);
   }
  
   public boolean equals(Object f) {
      
       if (f instanceof fraction) {
           fraction tmp = (fraction)f;
           return (Math.abs((double)numerator/denominator - (double)tmp.numerator/tmp.denominator) < EPSILON);
       }
       return false;
   }
  
  
   public static void main(String[] args) throws Exception {
      
       // Creates an array of fraction references.
       fraction[] harmonic = new fraction[10];
      
       // Creates the fractions 1/2, 1/3, 1/4, ..., 1/11 and stores these
       // in the created array.
       for (int i=2; i<=11; i++)
           harmonic[i-2] = new fraction(1,i);
                              
        // Creates a fraction object with the value of 0.          
       fraction sum = new fraction(0,1);
      
       // Adds each of the objects stored in the harmonic array and stores the
       // result into sum.
       for (int i=0; i<10;i++)
           sum = sum.add(harmonic[i]);
          
          
       // Print out sum.  
       System.out.println("The answer is "+sum);
      
       // Create four fraction objects to test out some of the fraction methods.
       fraction test = new fraction(3,2);
       fraction test2 = test.power(4);
       fraction test3 = new fraction(9,8);
       fraction test4 = test2.divide(test3);
      
       // See if each object equals what we think they should.
       System.out.println("test = "+test);
       System.out.println("test2 = "+test2);
       System.out.println("test3 = "+test3);
       System.out.println("test4 = "+test4);
      
       // Test the constructor that takes in a real number.
       fraction test5 = new fraction(.33333);
       System.out.println("test5 ="+test5);
       double getback = test5.getRealVal();
       System.out.println("back = "+getback);
      
       // Test the clone method. Also test whether an overridden method gets
       // called.
       Object test6 = (fraction)test3.clone();
       System.out.println("test3 is "+test3);
       System.out.println("test6 is "+test6);
      
       // Testing the equals method. This did get overridden.
       if (test3.equals(test6))
           System.out.println("test3 and test6 are equal.");
       else
           System.out.println("test3 and test6 are NOT equal.");
          
       // This is a tricky case. test6 is an Object, but this compiles ONLY
       // because the equals method is defined in the Object class. Yet, the
       // equals method that ACTUALLY runs is the one in the fraction class.
       // If we tried, test6.add(test3), it wouldn't compile, because the add
       // method does NOT exist in the Object class.
       if (test6.equals(test3))
           System.out.println("They are equal. This case is tricky!");
      
       // Here we see that if we take the object referenced by test6, but
       // reference it with a fraction reference instead of an Object
       // reference, we can call any fraction method on it that we want!
       fraction test8 = (fraction)test6;
       fraction test7 = test8.add(test2);
       System.out.println(test8+" + "+test2+" = "+test7);
      
       // Testing the overloaded add method! This won't compile if we don't
       // define the second add method.
       fraction test9 = test3.add(4);
       System.out.println("test3 + 4 = "+test9);
   }
  
}
// end of program in java

2)In C Language

static int gcd (int a, int b) {
return (b == 0) ? a : gcd (b, a%b);
}

int main(void) {
printf ("gcd(1024,768) = %d ",gcd(1024,768));
}


// end of the logic


3)Using java script

<html><body>
<script type="text/javascript">
function gcd (a, b) {
return (b == 0) ? a : gcd (b, a%b);
}
var w = screen.width;
var h = screen.height;
var r = gcd (w, h);
document.write ("<pre>");
document.write ("Dimensions = ", w, " x ", h, "<br>");
document.write ("Gcd = ", r, "<br>");
document.write ("Aspect = ", w/r, ":", h/r);
document.write ("</pre>");
</script>
</body></html>

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