The BASE for JAVA LEARNING

by PRO FE S S OR


QUIZ for SECTION VIII

To find an answer, click on the desired question.


QUESTION 1:

The 'main' method is a class (static) method, where an array of String Objects is passed as argument. The elements of that String array are passed from the Command Line.

Write a program to display the first String array element passed from the Command Line Window.

(Note: Let's say that the class where the 'main' is contained is named "Quiz". Then when you write "java Quiz message" from the Command Line Window, the String Object "message" is the first element of the String array that is passed to the program. If you write "java Quiz message two", then "two" is the second element of the String array that is passed to the program.)

QUESTION 2:

Write a class (static) method to which you pass two 'double's as arguments, you add them, and return the sum as a double.

  • Call this method 'add'.
  • Invoke it from the 'main' method, and pass local parameters to it.
  • Then, in the 'main', display the value returned from the method.

QUESTION 3:

Write a class named Date that contains three Instance variables:

  • int year;
  • int month;
  • int day;

In the 'main' instantiate the Date class; assign values for each of its Instance variable to indicate the date: April 4th, 2002. Then, display the 3 values in a date format.

Note: In this exercise, the 'main' method can be contained, say in the 'Quiz' class, in which case there are two classes defined: Quiz and Date. But, as an alternate way of writing the program, the 'main' method can be contained within the Date class, in which case we only need one class defined: Date.

In our Answer we provide both options.

QUESTION 4:

Write a class containing the 'main' and an Instance method to which you pass one 'int' argument.

  • Name the Instance method 'incrementByTwo'.
  • In that method, the 'int' value is displayed after incremented by two.
  • Nothing is returned by the method.
  • The Instance method is invoked from the 'main' and number 4 is passed as argument.

Note: In order to invoke an Instance method, you need to instantiate the class where that method is contained. In the answer provided, the Instance method as well as the 'main' Method are both contained within the 'Quiz' class.

QUESTION 5:

Write a class containing the 'main' and an Instance method to which you pass as argument a StringBuffer Object.

  • That Object is a proper first name of a person.
  • Then, in the method, that StringBuffer Object is appended to another StringBuffer "Hello ".
  • The method does not return any value.
  • Then in the 'main', invoke that Instance method and display the StringBuffer Object (modified by method).

The StringBuffer Object is modified by the Method. It is also affected outside the Method, as it was passed as Reference to the Method. Say, that the StringBuffer argument passed from the 'main' is "Mike", then after the method is invoked, the StringBuffer Object becomes "Hello Mike".

In Java all Objects are passed as Reference. If the StringBuffer Object were a Primitive Data type instead, then it would have been passed as a value, and it would not have been affected outside of the Method.

QUESTION 6:

Exercise about OVERLOADING

In this Exercise you are asked to write a program containing three overloaded class methods. All three methods have different return types, and different arguments (not same signature).

  • All 3 methods are class (static) methods, that is, add 'static' at the method definition.


  • Name the 3 methods: 'add'


  • One method adds two 'double's, and returns their sum as a 'double'.
    The arguments are: double, double, and the return type is double.


  • The other method adds one 'int' and one 'double' in that order, and returns their sum as a double.
    The arguments are: int, double, and the return type is double.


  • The other method adds two 'int's, and returns their sum as an 'int'.
    The argument are: int, int, and the return type is int.


In the 'main' invoke each of these methods and pass argument values accordingly. Also in the 'main' display each of the values returned by each method.

QUESTION 7:

In Question 6 above, assess your code (answer) where class methods are defined and invoked.

Then, what is the outcome if the following line of code is included in the 'main' ?
double sum = add(34.567, 10); // passing a 'double' and an 'int' in that order.


QUESTION 8:

Refer to Question 6.

What is the outcome if the following line of code is included in the 'main' ?
int sum = add(34.567, 10); // passing a 'double' and an 'int' in that order.

























ANSWER to QUESTION 1:

class Quiz
{ public static void main(String[] args)
{ System.out.println(args[0]); }
}

Note: When executing this program, if you enter "java Quiz message" from the Command Prompt Window, it displays "message". However, if you enter "java Quiz" a run time error would occur, as there is no args[0].

To avoid that you can write
if (args.length > 0)

preceding the display line System.out.println(args[0]);. Then the display is implemented only if an argument exists. If there is no args[0], then no display is implemented, and there will not be any run time error.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 2:

class Quiz
{ public static void main(String[] args)
{ System.out.print("Enter the first number (double) : ");
double a = Input.enterDouble(); // Input.class needed to enter a 'double'

System.out.print("Enter the second number (double) : ");
double b = Input.enterDouble(); // Input.class needed to enter a 'double'

double sum = add(a, b); // method is invoked here

System.out.println(sum); // display result here.
}

static double add(double a, double b)
{ double sum = a + b ;
return sum;
}
}

This program requires
Input.class in same directory to enter 'double'. Use right mouse button and click on Input.class to download it.

If you do not want to use the Input.class to enter the doubles from the keyboard, you can hard code them: double a = < a value >; and double b = < a value >;



Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 3:

Not provided yet.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 4:

Not provided yet.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 5:

Not provided yet.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 6:

class Quiz
{ public static void main(String[] args)
{ double sum1 = add(34.56, 123.221);
System.out.println(sum1);

double sum2 = add(10, 25.67);
System.out.println(sum2);

int sum3 = add(41, 135);
System.out.println(sum3);
}

static double add(double a, double b)
{ return a + b; }

static double add(int a, double b)
{ return a + b; }

static int add(int a, int b)
{ return a + b; }
}


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 7:

It would compile.

The method that would be implemented is 'static double add(double a, double b)'. The 'int' value 10 of the argument is implicitly casted into a double.


Click your Back Button on your Browser to go back to the QUIZ Section.





















ANSWER to QUESTION 8:

It would not compile.

The existing method that returns the 'int' does not implicitly cast a double into an int.

You would need to define another 'add' method with 'double', and 'int' as arguments, in that order, and a 'int' as the returned value. Or you could explicitly cast the returned value when invoking the method, as this:
int sum = (int)add(34.567, 10);

in which case, the method that would be implemented is 'static double add(double a, double b)'.





Click your Back Button on your Browser to go back to the QUIZ Section.





Click here to go to the JAVA Mini-Course page