Question (Java): Write the correct method signature for the following code. Indi
ID: 3709182 • Letter: Q
Question
Question (Java): Write the correct method signature for the following code. Indicate what is printed at the end of each question:
1. ?
if ( r < 2 ) g *= 4.0; // g = g * 4.0;
return r + r + r * r + 1; // what is this written as a math expr?
} public class less {
public static main(String[] args) {
int x = 2; double y = 3.0;
x = mathForFun(x, y * 2.0);
System.out.println("x = " + x + "y = " + y );
}
}
Prints: ________
2. ?
float[] taolf = new float[N];
for(int ii = 0; ii < N; ii++)
taolf[ii] = val;
return taolf;
}
public class y {
public static main(String[] args) {
float set = 2.0 * Math.PI;
final int sz = 16;
float[] x = initArray(set, sz)
System.out.println("x[4] = " + x[4]);
}
}
Prints: ________
Explanation / Answer
Complete program of Question 1:
public class less
{
//it is return type of main() method is void
public static void main(String[] args)
{
int x = 2; double y = 3.0;
x = mathForFun(x, y * 2.0);
System.out.println("x = " + x + "y = " + y );
}
//method signature and it is a static method
private static int mathForFun(int r, double g)
{
if ( r < 2 ) g *= 4.0; // g = g * 4.0;
return r + r + r * r + 1;// what is this written as a math expr?
}
}
what is this written as a math expr? r + r + r * r + 1;
Answer: r + r + (r * r )+ 1;
Output: x = 9 y = 3.0
Complete program of Question 2:
public class y
{
//main method return type should be void
public static void main(String[] args)
{
//cast it to float
float set = (float) (2.0 * Math.PI);
final int sz = 16;
float[] x = initArray(set, sz);
System.out.println("x[4] = " + x[4]);
}
//method signature and implementation
private static float[] initArray(float val, int N)
{
float[] taolf = new float[N];
for(int ii = 0; ii < N; ii++)
taolf[ii] = val;
return taolf;
}
}
Output:x[4] = 6.2831855