Question

In: Computer Science

JAVA Answer the following questions as briefly (but completely) as possible: How do you simplify the...

JAVA

Answer the following questions as briefly (but completely) as possible:

  1. How do you simplify the max method in the following, using the conditional operator?
     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:     }
  2. Write method headers (just the declaration, not the bodies) for the following methods:
    1. Compute a sales commission, given the sales amount and the commission rate.
    2. Display the calendar for a month, given the month and year.
    3. Compute a square root of a number.
    4. Test whether a number is even, and returning true if it is.
    5. Display a message a specified number of times.
    6. Compute the monthly payment, given the loan amount, number of years, and annual interest rate.
    7. Find the corresponding uppercase letter, given a lowercase letter.
  3. Identify and correct the errors in the following program:
     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: }
  4. What is pass-by-value? Show the results of the following two programs.
    1.  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: }
    2.  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: }
  5. What is wrong with the following class?
    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. Write an expression that obtains a random integer between 34 and 55, inclusive.
    2. Write an expression that obtains a random integer between 0 and 999, inclusive.
    3. Write an expression that obtains a random number between 5.5 (inclusive) and 55.5 (exclusive).
    4. Write an expression that obtains a random lowercase (English) letter.
  6. How many times is the factorial method in the following invoked, for the call factorial(6)?
     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: }
  7. Suppose that the class F is defined as shown below. Let f be an instance of F. Which of the following statements are syntactically correct?
    1: public class F {
    2:   int i;
    3:   static String s;
    4:   void iMethod () {
    5:   }
    6:   static void sMethod () {
    7:   }
    8: }
    1. System.out.println(f.i);
    2. System.out.println(f.s);
    3. f.iMethod();
    4. f.sMethod();
    5. System.out.println(F.i);
    6. System.out.println(F.s);
    7. F.iMethod();
    8. F.sMethod();
  8. Add the static keyword in the place of ?, if appropriate, in the code below.
     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: }
  9. Can each of the following statements be compiled?
    1. Integer i = new Integer("23");
    2. Integer i = new Integer(23);
    3. Integer i = Integer.valueOf("23");
    4. Integer i = Integer.parseInt("23", 8);
    5. Double d = new Double();
    6. Double d = Double.valueOf("23.45");
    7. int i = (Integer.valueOf("23")).intValue();
    8. double d = (Double.valueOf("23.4")).doubleValue();
    9. int i = (Double.valueOf("23.4")).intValue();
    10. String s = (Double.valueOf("23.4")).toString();
    1. How do you convert an integer into a string?
    2. How do you convert a numeric string into an integer?
    3. How do you convert a double number into a string?
    4. How do you convert a numeric string into a double value?

Solutions

Expert Solution

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.


Related Solutions

Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
GRADED PROBLEM SET #8 Answer each of the following questions completely. When possible to answer using...
GRADED PROBLEM SET #8 Answer each of the following questions completely. When possible to answer using a complete sentence and offering explanation, please do so. There are a total of 20 points possible in the assignment. Resveratrol, an ingredient in red wine and grapes, has been shown to promote weight loss in rodents. One study investigates whether the same phenomenon holds true in primates. The grey mouse lemur, a primate, demonstrates seasonal spontaneous obesity in preparation for winter, doubling its...
How do you simplify ratios?
How do you simplify ratios? sample problem: Simplify  70:56
Please answer questions in the order listed. You do not need to be completely correct, but...
Please answer questions in the order listed. You do not need to be completely correct, but I do need to see an honest effort. For this post, YOU MUST SHOW YOUR WORK, on any question requiring math. This is to make it so that I can see where exactly where you mis-stepped in your calculation or logic, and/or so that your classmates can learn from you. 5.) MicroServe needs $100,000 to upgrade its warehouse. Dayna, the CEO of MicroServe, thinks...
Answer the following questions briefly. You do not have to repeat everything in the book. Just...
Answer the following questions briefly. You do not have to repeat everything in the book. Just highlight the key points and identify distinction in various approaches. a) How many levels of virtualization can one consider? Comment on their advantages, shortcoming, and limitation. b) What are the typical systems that you know of that have been implemented at each level in the past? c) What are the difference between full virtualization and paravirtualization? Explain at the advantages, and shortcomings, and limitation...
1. Simplify each of the following sets as much as possible. a) ((? ∪ ??)? ∩...
1. Simplify each of the following sets as much as possible. a) ((? ∪ ??)? ∩ (? ∩ ∅?))? b) (ℤ ∩ ℚ+)? ∩ ℤ? 2.  Determine the cardinality of each set: a) ((ℂ − ℝ) ∩ ℕ) ∪ (ℤ+ − ℕ) b) ?(ℤ?? ∩ ℤ??)
Read the following scenario, and answer the question as completely as possible. A driver accidentally backs...
Read the following scenario, and answer the question as completely as possible. A driver accidentally backs his car into a man's leg above the knee. The man falls to the ground, yelling in pain; he then becomes confused, and has cool, clammy, and pale skin. What should you do?
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
How do I upload the Dataset in order for you to answer the following questions? Generate...
How do I upload the Dataset in order for you to answer the following questions? Generate a bivariate display in SPSS or PSPP for the DEGREE variable with each of the other attitude variables (you will have two tables). Then answer the questions below. Q3a: The majority of those with a Graduate degree reported what level of confidence in the military? Report the category for conarmy and the associated percentage. Category _______________ Percentage_____________
Question (1) Answer each of the following questions briefly. These questions are based on the following...
Question (1) Answer each of the following questions briefly. These questions are based on the following relational schema: Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pcttime: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) (a) (5 points) Give an example of a foreign key constraint that involves the Dept relation. What are the options for enforcing this constraint when a user attempts to delete a Dept tuple? (b) (5 points) Write the SQL statements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT