Question In basic Java What you need to do: In the same class that includes the
ID: 3769274 • Letter: Q
Question
Question
In basic Java 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 goldenRatio 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. Hint: 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? FYI: 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
class golenRatio_test
{
public static boolean golenRatio (int n1, int
n2)
{
double a;
if(n1>n2)
a=n1/n2;
else
a=n2/n1;
if(Math.round(a*1000)==1.618)
return true;
else
return false;
}
public static void main(String args[])
{
boolean b=golenRatio(4,5);
System.out.println(b);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.