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

Java introductory course What you need to do: In the same class that includes th

ID: 3758540 • Letter: J

Question

Java introductory course

What you need to do: In the same class that includes the main method write and test a method with the following method header: boolean static golenRatio (int n1, int n2) The method goldenRatio returns true if the quotient of the greater number divided by the smaller number - rounded to 3 decimal digits -equals 1.618 Otherwise it returns false. Test the method golden Ratio in main. That means: call it with a number pair that is a golden ratio and verity that it returns true. Then call it with a number pair that is NOT a golden ratio (e.g. 4 and 5) and verify that the answer is false. How to round a number x to 3 digits after the decimal point: x = Math.round(x * 1000) /1000d; // notice the d at the end; can you guess why we need it? More general as a method: How to round a number x to n digits after the decimal point: double round (double x, int n) { double factor = Math.pow(10, n); return Math.round(x * factor) / factor;}

Explanation / Answer

import java.util.*;
class ratio
{
   public static boolean goldenRatio(int n1, int n2)
   {
       double q,r;
       if(n1>n2)
       {
           q=(double)(n1/n2);
       }
       else
       {
           q=n2/n1;
       }
       r=round(q,3);
      
       if(r==1.618)
       {

           return true;
       }
       else
       {
           return false;
       }

      
   }

   public static double round(double x, int n)
   {
       double factor=Math.pow(10,n);
       return Math.round(x*factor)/factor;
   }

   public static void main(String args[])
   {
       boolean ans = goldenRatio(7,4);  
       if(ans)
       {
           System.out.println("Golden ratio");

       }
       else
       {
           System.out.println("Not Golden ratio");

       }
       ans = goldenRatio(7,4);  
       if(ans)
       {
           System.out.println("Golden ratio");

       }
       else
       {
           System.out.println("Not Golden ratio");

       }
   }
}
  

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