8.2



public class Punctuation {

	public static void main(String[] args) {

		int count = 0;

		final int STOP = 10;

		while (count <= STOP) {

			System.out.println(count);

			count++;

		}

	}

}



8.5



System.out.println(' ' * 2.0); // what does this output? why?

This outputs 64.0

This is because the ASCII code for space (' ') is 32; and 32 * 2 = 64.  Java casts the char to an int.



System.out.println(true + 29); // what does this output? why?

Bad type.



System.out.println("Hi\nthere!") // what does this output?



Hi

there!



System.out.println((char)97); // (type) is cast operator; output?

This outputs:



a



Java casts 97 to 'a' as it corresponds to ASCII code 97.





8.7

String s1 = null;

This is ok; any object variable can be assigned null.



String s2 = "";

This is ok; the string of length zero is assigned to s2.



System.out.println( s1.length() );

This is invalid as s1 is null and does not reference a String object.  Null pointer error.



System.out.println( s2.length() );

This is valid; prints 0.



System.out.println("abc" + "123");

This is valid.  The + operator concatenates strings, and this line outputs:



abc123



s2[1] = 'd';

Invalid.  Strings are not arrays in Java; they're objects.



String a = "abcd";

Valid.  Assigns "abcd" to variable a.  For strings, no need to use the new String() constructor.



String b = new String("abcd");

Valid, has the same effect as the statement above.



System.out.println( a == b );

Valid; will print false since a is not stored in the same memory location as b.



System.out.println( a.equals(b) );

Valid; will print true since a and b have the same characters in them.



String start = "";

start = start + 1;

Invalid; you can't concatenate a string and an int this way.  use "1" instead.



start = start + "\n";

Valid concatenation.



start = start + 2;

Invalid, same reason as above.



System.out.println(start);



8.9

The program outputs the numbers 0 to 9.  This is a perfectly legal program since only the lowercase "int" is a reserved word.  Java differentiates between uppercase and lowercase.  And of course, don't use names like these; use more descriptive variable names that aren't spelled the same as java reserved words!



Write a program that checks if 13 is even or odd.



class CheckOdd13 {

	public static void main(String[] args) {

		if (13 % 2 == 0)

			System.out.println("13 is even");

		else

			System.out.println("13 is odd");

	}

}





Why does System.out.println(3/4) output zero?

3/4 evaluates to 0.75; but when cast to an int automatically, it's rounded down to 0.



Write a program that generates a random integer between 1 and 100, inclusive. Hint:

Math.random() returns a double in the interval [0, 1).



class GenRand {

	public static void main(String[] args) {

		int num = ((int) Math.random()*100) + 1;

	}

}





Write a program that converts all of the lowercase letters to uppercase without using an

array or strings. Hint: for(int count=start;count<=stop;count++).



class ConvLCase {

	public static void main(String[] args) {

		int start = 97; //a

		int stop = 122; //z

		for (int count = start; count<=stop; count++)

			System.out.println((char) count-32); //convert uppercase to lowercase

	}

}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                