Answer the following questions as briefly (but completely) as possible:
max
method in the following,
using the conditional operator?
/** Return the max of two numbers */ public static int max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; }
public class Test { public static method1(int n, m) { n += m; method2 (3. 4); } public static int method2(int n) { if ( n > 0 ) return 1; else if (n == 0) return 0; else if (n < 0) return -1; } }
public class Test { public static void main ( String [] args ) { int max = 0; max(1, 2, max); System.out.println(max); } public static void max ( int value1, int value2, int max ) { if ( value1 > value2 ) max = value1; else max = value2; } }
public class Test { public static void main ( String [] args ) { int i = 1; while ( i <= 6 ) { method1( i, 2 ); i++; } } public static void method1 ( int i, int num ) { for ( int j = 1; j <= i; j++ ) { System.out.print( num + " " ); num *= 2; } System.out.println(); } }
public class Test { public static void method ( int x ) { } public static int method ( int y ) { return y; } }
factorial
method in the following invoked,
for the call factorial(6)
?
import java.util.Scanner; public class ComputeFactorial { public static void main ( String [] args ) { Scanner input = new Scanner( System.in ); System.out.print( "Enter a nonnegative integer: " ); int n = input.nextInt(); // Display factorial System.out.println( "Factorial of " + n + " is " + factorial(n) ); } /** Return the factorial for the specified number */ public static long factorial ( int n ) { if ( n == 0 ) // Base case return 1; else return n * factorial( n - 1 ); // Recursive call } }
F
is defined as shown below.
Let f
be an instance of F
.
Which of the following statements are syntactically correct?
public class F { int i; static String s; void iMethod () { } static void sMethod () { } }
System.out.println(f.i);
System.out.println(f.s);
f.iMethod();
f.sMethod();
System.out.println(F.i);
System.out.println(F.s);
F.iMethod();
F.sMethod();
static
keyword in the place of
?
, if appropriate, in the code below.
public class Test { private int count; public?
void main ( String [] args ) { ... } public?
int getCount () { return count; } public?
int factorial ( int n ) { int result = 1; for ( int i = 1; i <= n; i++ ) result *= i; return result; } }
Integer i = new Integer("23");
Integer i = new Integer(23);
Integer i = Integer.valueOf("23");
Integer i = Integer.parseInt("23", 8);
Double d = new Double();
Double d = Double.valueOf("23.45");
int i = (Integer.valueOf("23")).intValue();
double d = (Double.valueOf("23.4")).doubleValue();
int i = (Double.valueOf("23.4")).intValue();
String s = (Double.valueOf("23.4")).toString();
Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission). If possible use the “text” and not the “HTML” mode of your email program. Please use the subject similar to “Java Programming I Homework Assignment #3 Submission”, so I can tell which emails are submitted homework assignments.
Homework assignments will not be returned. Please do not send as attachments. Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.
Confused? Send questions about the homework assignment to (homework questions). Please use a subject similar to “Java Programming I Homework Assignment #3 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).