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

1. Find the error in the following method definition: // This method has an erro

ID: 3528638 • Letter: 1

Question

1. Find the error in the following method definition: // This method has an error! public static void sayHello(); { System.out.println("Hello"); } 2. Find the error in the following method definition: // This method has an error! public static double timesTwo(double num) { double result = num * 2; } 3. Find the error in the following method definition: // This method has an error! public static int half(double num) { double result = num / 2.0; return result; } 4. Here is the code for the displayValue method, shown earlier in this chapter: public static void displayValue(int num) { System.out.println("The value is " + num); } For each of the following code segments, indicate all that will successfully compile. A) displayValue(100); B) displayValue(6.0); C) short s = 5; displayValue(s); D) long num = 1; displayValue(num); E) displayValue(6.2f); F) displayValue((int) 7.5); 5. A program contains the following method definition: public static int cube(int num) { return num * num * num; } Write a statement that passes the value 4 to this method and assigns its return value to a variable named result . 6. A program contains the following method: public static void display(int arg1, double arg2, char arg3) { System.out.println("The values are " + arg1 + ", " + arg2 + ", and " + arg3); } Write a statement that calls this method and passes the following variables as arguments: char initial = 'T'; int age = 25; double income = 50000.00; 7. Write a method named timesTen. The method should accept a double argument, and return a double value that is ten times the value of the argument. static () { num * 10.0; } 8. Write a method named getName that prompts the user to enter his or her first name, and then returns the user

Explanation / Answer

//please rate


1. Find the error in the following method definition:

// This method has an error! Semicolon kills loop

public static void sayHello()//semicolon do not belong here as it will kill loop before entering it, removed

{

System.out.println("Hello");

}



2. Find the error in the following method definition:

// This method has an error! Return a double is needed


public static double timesTwo(double num) //note the return type is double

{

double result = num * 2;

return result; //needs to RETURN a double here.

}



3. Find the error in the following method definition:

// This method has an error! Wrong return type!

public static double half(double num) { //changed to return type double

double result = num / 2.0;

return result;

}


4. Here is the code for the displayValue method, shown earlier in this chapter:

public static void displayValue(int num) {

System.out.println("The value is " + num);

}

For each of the following code segments, indicate all that will successfully compile.

A) displayValue(100); //SUCCESS

B) displayValue(6.0);//fail


C) short s = 5;//SUCCESS

displayValue(s);

D) long num = 1;//fail

displayValue(num);


E) displayValue(6.2f);//fail

F) displayValue((int) 7.5);//SUCCESS

5.

result=cube(4);


6.

display(age,income,initial);

7.

public static double timesTen(double num) {

return num * 10.0;

}

8.

public static String getName(Scanner input){

System.out.print("Enter first name: ");

return input.next();

}