### Homework 2 - CS170 Section 002

• I have made an answer sheet for you to do this homework:

(You can go to the MathCS lab, login to one of the machines and print it out. Pick up your print outs by the lab rep (at the entrance of the MathCS lab). You can also print in the library, but that will cost you some money).

You must use the answer sheet - grading paper homeworks in random format has proven to be quite difficult.

• Question 1 (40 pts)

• Assume that the following variables are defined:

 ``` double a = 11.0, b = 4.0; int i = 11, j = 4; ```

Using the given values above in the variables a, b, i and j, for each of the following Java println statements, state:

• what value is printed and
• what is the type of the value?

(Note: you can answer these questions using the Java compiler, and if you do this, you will miss out on a chance to learn. Also you will be asked to do similar problems like these on the midterm and you will not have access to a Java compiler.

But I do recommend you write these statement inside a Java program after you have do the homework. The Java program can check your answers --- if you have errors, try to find out why !!! So: use the Java compiler to help you learn Java programming !)

 System.out.println( a - b * 2 - j );     value =                 type=             System.out.println( a / b );             value =                 type=             System.out.println( a / i );             value =                 type=             System.out.println( i / j );             value =                 type=             System.out.println( 1.0 * i / j );       value =                 type=             System.out.println( b - j / a - i );     value =                 type=             System.out.println( - - i );             value =                 type=             System.out.println( i - - - j );         value =                 type=             System.out.println( a - i / j - - b );    value =                 type=             System.out.println( (i - 1) / j + + a );   value =                 type=

Note: Java has the the unary + operator that returns the same value (identity operation).
E.g.: +3 = 3.

• Question 2 (10 pts)

• In the Java docs linked above, look up in the top left corner of the page for the following packages:

 java.lang                                             java.util java.io.

For each package, write down the first sentence of the package description, and report how many classes it contains. (Just count classes, not interfaces, exceptions, or whatever else.)

 Package java.awt.color: Provides classes for color spaces. Package has 5 class (ColorSpace, ICC_ColorSpace, ICC_Profile, ICC_ProfileGray and ICC_ProfileRGB --- you don't need to list them, I did it to show you what to look for to find the correct answer)

• Question 3 (30 pts)

• Write a Java program that reading a double typed number and print its square (i.e., multiply the number with itself).

```
public class Question3
{
public static void main( String[] args )
{

}
}
```

Make sure you import the necessary classes in your program.

• Question 4 (20 pts)

• Here is a URL for the documentation of the java.lang.Math class: click here

 List all the methods named max (give all the details). (5 pts) Why are there more than one method ? (5 pts) You are given 3 int typed variables named x, y and z. The expression: ``` Math.max( x , y ) ``` will return the maximum (= largest) of the 2 values stored in x and y. Question: (10 pts) Using the max method, write one single Java expression whose value is the maximum of x, y and z Hint: you can nest functions, e.g.: ``` The square root of the square root of 2 = Math.sqrt( Math.sqrt(2.0) ); ```