Homework: Review I


Here are some review questions to give you extra practice with what you have learned so far.

  1. 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.

  2. (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:
  3. (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:
    1. The sum of all even numbers between 2 and 100 (inclusive).
    2. The sum of all squares between 1 and 100 (inclusive).
    3. All powers of 2 from 20 up to 220 (inclusive).
    4. The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with ab.
    5. 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.)
    6. 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.)
  4. (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.
    1. Computing the larger of two integers
    2. Computing the smallest of three real numbers
    3. Checking whether an integer is a prime number, returning true if it is and false otherwise
    4. Checking whether a string of characters is contained inside another string of characters
    5. Computing the balance of an account with a given initial balance, an annual interest rate, and a number of years of earning interest
    6. Printing the balance of an account with a given initial balance and an annual interest rate over a given number of years
    7. Printing the calendar for a given month and year
    8. Computing the weekday for a given day, month, and year (as a string such as "Monday")
    9. Generating a random integer between 1 and n
  5. (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?
  6. (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.
    1. returning true if the arguments are all the same
    2. returning true if the arguments are all different
    3. returning true if the arguments are sorted with the smallest one coming first

Additional Questions

  1. (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:
    1. The smallest and largest of the inputs.
    2. The number of even and odd inputs.
    3. Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19.
    4. 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.
  2. (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.
  3. (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.
    1. returning the smallest of the arguments
    2. returning the average of the arguments