Here are some review questions to give you extra practice with what you have learned so far.
- Complete the following Additional Activity for the Hailstone
Series lab.
Implement a new static method declared as follows:
Note that you cannot assume the user will provide a number; the user can type pretty much anything. So your method should read the input as a String (use SimpleReader.nextLine()), then make sure that the input is an integer number (use FormatChecker.canParseInt()), then convert the string to an integer (use Integer.parseInt()), and finally check that the integer is positive./** * Repeatedly asks the user for a positive integer until the user enters * one. Returns the positive integer. * * @param in * the input stream * @param out * the output stream * @return a positive integer entered by the user */ private static int getPositiveInteger(SimpleReader in, SimpleWriter out) {...}
- (This is modified from Review Exercise R4.19 at the
end of Chapter 4 of Java
for Everyone.) Using the kind of tracing tables discussed in Writing
and Tracing Loops, provide tracing tables for these loops:
-
int n = 1; int i = 2; while (i < 5) { n = n + i; i = i + 1; }
-
int i = 2; double n = 1.0 / 2.0; while (i <= 5) { n = n + 1.0 / i; i = i + 1; }
-
double x = 1.0; double y = 1.0; while (x < 1.8) { y = y / 2.0; x = x + y; }
-
int x = 3; int y = 4; while (y > 0) { x = x + 1; y = y - 1; }
-
- (This is from Programming Exercise P4.1 at the end of
Chapter 4 of Java
for Everyone.) Write program fragments (i.e., you do not need
to write complete programs) with loops that compute:
- The sum of all even numbers between 2 and 100 (inclusive).
- The sum of all squares between 1 and 100 (inclusive).
- All powers of 2 from 20 up to 220 (inclusive).
- The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.
- The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)
- The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)
- (This is from Review Exercise R5.2 at the end of Chapter 5
of Java
for Everyone.) Write method headers for methods with the
following descriptions.
- Computing the larger of two integers
- Computing the smallest of three real numbers
- Checking whether an integer is a prime number, returning true if it is and false otherwise
- Checking whether a string of characters is contained inside another string of characters
- Computing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest
- Printing the balance of an account with a given initial balance and an annual interest rate over a given number of years
- Printing the calendar for a given month and year
- Computing the weekday for a given day, month, and year (as a string such as "Monday")
- Generating a random integer between 1 and n
- (This is from Review Exercise R5.14 at the end of Chapter 5
of Java
for Everyone.) Consider the following method that is intended
to swap the values of two integers:
Why doesn’t the falseSwap method swap the contents of x and y?public static void falseSwap(int a, int b) { int temp = a; a = b; b = temp; } public static void main(String[] args) { int x = 3; int y = 4; falseSwap(x, y); System.out.println(x + " " + y); }
- (This is modified from Programming Exercise P5.2 at
the end of Chapter 5 of Java
for Everyone.) Write the following methods and provide a
program to test them.
returning true if the arguments are all the sameboolean allTheSame(int x, int y, int z)
returning true if the arguments are all differentboolean allDifferent(int x, int y, int z)
returning true if the arguments are sorted with the smallest one coming firstboolean sorted(int x, int y, int z)
Additional Questions
- (This is from Programming Exercise P4.2 at the end of
Chapter 4 of Java
for Everyone.) Write program fragments (i.e., you do not need
to write complete programs) that read a sequence of integers from
a given SimpleReader in and print:
- The smallest and largest of the inputs.
- The number of even and odd inputs.
- Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19.
- All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the program should print 3 5 6.
- (This is from Review Exercise R5.5 at the end of Chapter 5
of Java
for Everyone.) Consider these methods:
Without actually compiling and running a program, determine the results of the following method calls.public static double f(double x) { return g(x) + Math.sqrt(h(x)); } public static double g(double x) { return 4.0 * h(x); } public static double h(double x) { return x * x + k(x) - 1.0; } public static double k(double x) { return 2.0 * (x + 1.0); }
double x1 = f(2.0);
double x2 = g(h(2.0));
double x3 = k(g(2.0) + h(2.0));
double x4 = f(0.0) + f(1.0) + f(2.0);
double x5 = f(-1.0) + g(-1.0) + h(-1.0) + k(-1.0);
- (This is from Programming Exercise P5.1 at the end of
Chapter 5 of Java
for Everyone.) Write the following methods and provide a
program to test them.
returning the smallest of the argumentsdouble smallest(double x, double y, double z)
returning the average of the argumentsdouble average(double x, double y, double z)