Programing Excercises - Chapter 0 Solutions
0.1 The package java.lang includes the Integer class. In that class, what is the definition of MAX_VALUE ? The number 0x7fffffff is in hexadecimal, that is, base 16, notation. To indicate hexadecimal notation, start with “0x”, followed by the hexadecimal value. In hexadecimal, there are sixteen digits: 0, 1, 2, . . ., 9, a, b, c, d, e, and f. The digits 0 through 9 have the same value as in decimal, and the letters ‘a’ through ‘f’ have the following decimal values:
a 10
b 11
c 12
d 13
e 14
f 15
The decimal representation of hexadecimal 7ffffff is
7 ∗ 167 + 15 ∗ 166 + 15 ∗ 165 + 15 ∗ 164 + 15 ∗ 163 + 15 ∗ 162 + 15 ∗ 161 + 15
= 2147483647
Similarly, the decimal value of MIN_VALUE is −2147483648. Hypothesize the decimal value of each of the following:
Integer.MAX_VALUE + 1
Math.abs (Integer.MIN_VALUE)
Test your hypotheses with a small program that calls the System.out.println method. Get the solution of 0.1
0.2 Suppose we have the following:
int a = 37,
b = 5;
System.out.println (a - a / b * b - a % b);
Hypothesize what the output will be. Test your hypothesis by executing a small program that includes that code.
Can you find another pair of positive int values for a and b that will produce different output? Explain. Get the solution of 0.2
0.3 Hypothesize the output from the following:
System.out.println (1 / 0);
System.out.println (1.0 / 0);
Test your hypotheses by executing a small program that includes that code. Get the solution of 0.3
0.4 In the String class, read the specification for the indexOf method that takes a String parameter. Then hypothesize the output from the following System.out.println ("The snow is now on the ground.".indexOf ("now")); Test your hypothesis by executing a small program that includes that code. Get the solution of 0.4
0.5 In the String class, read the specification for the indexOf method that takes a String parameter and an int parameter. Then hypothesize the output from the following
System.out.println ("The snow is now on the ground.".indexOf ("now", 8));
Test your hypothesis by executing a small program that includes that code. Get the solution of 0.5
0.6 Write and run a small program in which an input string is read in and the output is the original string with each occurrence of the word “is” replaced by “was”. No replacement should be made for an embedded occurrence, such as in “this” or “isthmus”. Get the solution of 0.6
0.7 Write and run a small program in which an input string is read in and the output is the original string with each occurrence of the word “is” replaced by “is not”. No replacement should be made for an embedded occurrence, such as in “this” or “isthmus”. Get the solution of 0.7
0.8 Write and run a small program in which the end user enters three lines of input. The first line contains a string, the second line contains a substring to be replaced, and the third line contains the replacement substring. The output is the string in the first line with each occurrence of the substring in the second line replaced with the substring in the third line. No replacement should be made for an embedded occurrence, in the first line of the substring in the second line. Get the solution of 0.8
0.9 Study the following method:
public void mystery (int n)
{
System.out.print ("For n = " + n);
while (n > 1)
if (n % 2 == 0)
n = n / 2;
else
n = 3 * n + 1;
System.out.println (", the loop terminated.");
} // method mystery
Trace the execution of this method when n = 7. In the same class as the method mystery, develop a main method and a run method to show that the while statement in the method mystery successfully terminates for any positive integer n less than 100. Cultural note: It can be shown that this method terminates for all int values greater than 0, but it is an open question whether the method terminates for all integer values greater than 0. Get the solution of 0.9