You will need to download the Console class into the directory where you are working.
Which of the following are valid variable names? Of the valid names, which would be good style?
Write down the values of the following expressions and their type.
Note: The percentage symbol, %, is the modulus operator. It produces the integer remainder. E.g. 12 % 5 is two.
Write a statement which will define an integer called sum and assign it a value of 30.
Place the statement from the above exercise in a program, multiply the variable sum by two and print it out.
What will the following code fragment print when run
int i = 5; System.out.println(i); i = 15; System.out.println(i); |
What will the following code fragment print when run
int sum = 5; sum = sum + 1; System.out.println(sum); sum = sum + 1; System.out.println(sum); sum = sum * 10; System.out.println(sum); |
What mistakes are there in the following code fragment? If this fragment was in a proper method definition, would it compile?
int i; System.out.println(i); int i = 15 System.out.println(i); |
Is there anything wrong with the following code?
double x = 15.0; x = x * 10; System.out.println(x); |
If not, what does it do?
Write a program which will read in three numbers and print the average value of the numbers. Try out the program. Does it produce the answers you expect?
A sample session with the user might look like
$ java Average Enter three numbers 5 10 15 The average is 10 |
Note you should Console.readInt() to read an int, and Console.readDouble() if you want to read a double. You will need three variables to hold the three numbers, and three assignment statements to place the numbers into the variables. You can then perform the calculation and print out the result. You could look at the Temperature program which may provide some inspiration.
Here are some comments about the process of creating the program.
Write a program that reads in a person's height in centimeters and converts it to light years. One light year is the distance that light travels in a year. The speed of light is 2.99793e8 meters per second.
Stick the following code in a method and see what happens.
System.out.print("1/2 = ");
System.out.println(1/2);
System.out.print("1/2.0 = ");
System.out.println(1/2.0);
System.out.print("1.0/3.0 = ");
System.out.println(1.0/3.0);
System.out.print("0.000000001 = ");
System.out.println(0.000000001);
double x = 0.1;
System.out.print("x + 0.1 = ");
System.out.println(x + 0.1);
System.out.print("x + 0.2 = ");
System.out.println(x + 0.2);
System.out.print("x + 0.7 = ");
System.out.println(x + 0.7);
|
Explain the results.
Well, how can you explain that! The computer wrong! Impossible! In fact, it's not impossible, it's down to the fact that the computer uses binary numbers and decimal numbers are not always accurately represented by binary decimals. In particular, 0.1 is a repeating binary number. Here is an explanation.
$ java Splitter Enter a five digit number: 16384 The number split is 1 6 3 8 4 |