In: Computer Science
JAVA
Answer the following questions as briefly (but completely) as possible:
1: /** Return the max of two numbers */ 2: public static int max ( int num1, int num2 ) { 3: int result; 4: 5: if ( num1 > num2 ) 6: result = num1; 7: else 8: result = num2; 9: return result; 10: }
1: public class Test { 2: public static method1(int n, m) { 3: n += m; 4: method2 (3. 4); 5: } 6: 7: public static int method2(int n) { 8: if ( n > 0 ) return 1; 9: else if (n == 0) return 0; 10: else if (n < 0) return -1; 11: } 12: }
1: public class Test { 2: public static void main ( String [] args ) { 3: int max = 0; 4: max(1, 2, max); 5: System.out.println(max); 6: } 7: 8: public static void max ( int value1, int value2, int max ) { 9: if ( value1 > value2 ) 10: max = value1; 11: else 12: max = value2; 13: } 14: }
1: public class Test { 2: public static void main ( String [] args ) { 3: int i = 1; 4: while ( i <= 6 ) { 5: method1( i, 2 ); 6: i++; 7: } 8: } 9: 10: public static void method1 ( int i, int num ) { 11: for ( int j = 1; j <= i; j++ ) { 12: System.out.print( num + " " ); 13: num *= 2; 14: } 15: System.out.println(); 16: } 17: }
1: public class Test { 2: public static void method ( int x ) { 3: } 4: public static int method ( int y ) { 5: return y; 6: } 7: }
1: import java.util.Scanner; 2: 3: public class ComputeFactorial { 4: public static void main ( String [] args ) { 5: Scanner input = new Scanner( System.in ); 6: System.out.print( "Enter a nonnegative integer: " ); 7: int n = input.nextInt(); 8: 9: // Display factorial 10: System.out.println( "Factorial of " + n + " is " + factorial(n) ); 11: } 12: 13: /** Return the factorial for the specified number */ 14: public static long factorial ( int n ) { 15: if ( n == 0 ) // Base case 16: return 1; 17: else 18: return n * factorial( n - 1 ); // Recursive call 19: } 20: }
1: public class F { 2: int i; 3: static String s; 4: void iMethod () { 5: } 6: static void sMethod () { 7: } 8: }
1: public class Test { 2: private int count; 3: public ? void main ( String [] args ) { 4: ... 5: } 6: public ? int getCount () { 7: return count; 8: } 9: public ? int factorial ( int n ) { 10: int result = 1; 11: for ( int i = 1; i <= n; i++ ) 12: result *= i; 13: return result; 14: } 15: }
Max method using conditional operators:
Conditional operator is a ternary operator where it checks the condition before question mark. If the condition is true it will set the result value as num1, else it will set the value of result as num2.
public static int max(int num1,int num2){
int result = (num1>num2)?num1:num2;
return result
}
Method headers:
1. public double computeSalesCommision(double amount,double commission_rate){
}
2. public void displayCalender(String month,int year){
}
3. public double squareRoot(int num){
}
4. public Boolean isEven(int num){
}
5. public displayMsg(String msg, int n){
}
6. public double monthlyPayment(double loan_amount, int no_of_years,double rates){
}
7. public char correspondingUpperCase(char c){
}
Errors in the code:
1. In this program, the method declaration itself is incorrect. Datatype is required before parameter m and the return type is not mentioned in the declaration. On line 4, the argument passed to method2 is in incorrect format as there is space between 3 and 4 which is not required.
The correct code is:
public class Test { public static void method1(int n, int 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; } }
Pass-by-Value:
In pass-by-value there is two independent variable swith same value i.e the caller and callee both have different variables with same value. The changes made to the parameter by the callee will not affect the value of actual variable in the caller method.
Results:
1. Answer: 0
Here the pass-by-value concept is used where the changes made by the max() method will not affect the value of max variable.
2. Answer:
2
2 4 2 4 8 2 4 8 16 2 4 8 16 32 2 4 8 16 32 64 Here the next number is the multiplication of the current number and 2. This loop iterates for 6 times.
In the class Test both the method has a same signature for method(). When we call the method(), Java compiler will get confused as there is two method with same signature. This can be solved using overloading or overriding concept.