CSC108 --- Assignment 3


Solve the following problems. Remember to put throws Exception on any method that needs it. You may assume that all input (from a user or a file) is valid and of an appropriate type.

We expect this assignment to take 10-15 hours to complete. If you and your partner are taking more time than that, please contact your instructor or a TA --- we may be able to help with suggestions for different work habits.

If you write one of these a day, you'll be done with plenty of time to check things over!



  1. Write the class GuessDivisors that allows a user to play a number-guessing game. The constructor takes a positive integer to be guessed. Instance method play first prompts the user with
    Guess a positive integer.
    	
    and then displays a series of prompts, which are one of the following two, as appropriate (no leading or trailing spaces). If the guess is too low:
    Your guess divides my number, leaving a remainder of N.  Try again.
    	
    If the guess is too high:
    My number divides your guess, leaving a remainder of M.  Try again.
    	
    (where N and M are replaced the remainder after division). These prompts are produced using JOptionPane.showInputDialog

    If the user guesses the number exactly, tell them so:

    You got it!
    	
    ...using JOptionPane.showMessageDialog. You'll need to give this method null as the first argument, and the message you prompt the user with as the second.

    Copy and paste these messages; don't type them. Our automarking will expect these exact messages.

    Also, write import javax.swing.*; not import javax.swing.JOptionPane; to accommodate a nuance in our automarking.


  2. Write a class BracketGuessDivisors that does the same thing that GuessDivisors did, but with prompts that include the range so far (i is the highest guess below the answer, and j is the smallest guess above the answer). If the guess is too small:
    Your guess divides my number, leaving a remainder of N.
    The number is in the range i..j (exclusive).
          
    If the guess is too big:
    My number divides your guess, leaving a remainder of M.
    The number is in the range i..j (exclusive),
          
    Of course, you have to replace i and j with numbers, and give careful thought to what values they should have. After the first guess (unless it was correct) only i or j will be know, so your message should replace i..j with either i.. or ..j as appropriate.

    When the user finishes, tell them (using JOptionPane.showMessageDialog):

    You got it!
    You guessed X numbers smaller than my number
    and Y numbers larger than my number.
    
    ... where X and Y are replaced by the appropriate numbers. A really lucky guesser could have both X and Y equal to zero.
  3. Write a class StringTrivia that has the following static methods. Do not declare any static or instance variables.
    /**
     * Return whether the number of vowels is greater than the number of
     * consonants in s.  Consider vowels to be the characters contained in
     * the String "aeiouyAEIOUY", and consonants to be the alphabetic
     * characters that are NOT in that String.
     * Precondition: s != null
     */
    public static boolean vowelsBeatConsonants(String s)
          
    /**
     * Return whether s can be formed from t by deleting (but not
     * re-ordering) zero or more of t's characters.  For example,
     * "abc" can be formed from "RaTbbNcQ" by deleting R, T, b, N,
     * and Q.  Notice that if s is the empty String ("") this true
     * for EVERY t!
     * Precondition: s != null && t != null
     */
    public static boolean isSubsequence(String s, String t)
      // Hint: Thoroughly check out indexOf in API
          
    /**
     * Return the average length of the lines in file f.
     * Precondition: f != null and the file is not empty.
     */
    public static double averageStringLength(String f)
          
    /**
     * Return the number of non-overlapping pairs of adjacent characters in s.
     * For example "ooo" contains just a single non-overlapping adjacent pair,
     * whereas "bookkeeper" contains three non-overlapping adjacent pairs.
     * Precondition: s!= null
     */
    public static int doubleLetters(String s)
          

  4. Write a class NumberInfo that has the following static methods. For all files, you may assume 1 input per line and no leading spaces.
    /**
     * Read a sequence of doubles from file f (one per line), and return
     * the one with a fractional part that is closest to zero.
     * Precondition: f != null and the file is not empty
     */
    public static double getClosestFraction(String f)
          
    /**
     * Read a sequence of doubles from a file f (one per line), and return
     * the average of the largest and smallest entries.
     * Precondition: f != null and the file is not empty
     */
    public static double midPoint(String f)
          
    /**
     * Read a sequence of ints from file f, and return the sum of the
     * numbers that are multiples of 3.
     * precondition: f != null
     */
    public static int getThreeSum(String f)
          
    /**
     * Read a sequence of ints from file f, and return the sum of the
     * numbers that are multiples of 3, minus the sum of those
     * that are not.
     * Precondition: f != null
     */
    public static int getThreeSumDifference(String f)
          
    /**
     * Read a sequence of ints from file f, and return whether
     * numbers that are multiples of 3 occur more than half
     * as often as those that are not.
     * Precondition: f != null
     */
    public static boolean hasManyThreeProducts(String f)
        

  5. Write a class TabularStuff that has the following method. Do not declare any static or instance variables. You may want to nest one loop inside another.
    /**
     * Produce (n/4)+1 lines of output, consisting of
     * n n-2 n-4 n-6 ... n/2
     * n-2 n-4 n-6 ... n/2
     * ...
     * n/2+2 n/2
     * n/2
     * Precondition: n >= 0 && n a multiple of 4
     */
    public static void printTriangle(int n)
          

    Submit all *.java for your five classes, plus GuessDivisors.html and BracketGuessDivisors.html

Danny Heap
Last modified: Thu Mar 13 16:52:25 EST 2003