Homework 3

Skills: designing, coding, and using static methods, arrays, reading online documentation, step-wise refinement, test plans, algorithm development,

Classes: String, Math, Random

Please make sure to include your name, Andrew ID, and the honor statement in all your code, using the format given in class.

Due Week 4: Thu by 10pm.
Note: a printed test plan for the WordCount program is due this week Wed by the start of lecture.

Write the following programs:

  1. Update the program called GuessTheNumber based on Exercises 6.30 and 6.31 on p283 of your book and started as the guided program for the Tuesday short lab.

    1. Refactor the code to use a method called guess() that takes the maxRange as its only argument, generates the number to guess, allows the user to guess the number (as coded during the lab), and returns the number of tries it took. I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.

    2. Write a method called log2() that takes an integer and returns the log base 2, also as a int. The method should use this formula, where n is the passed-in value:
      Math.round(Math.log10(n) / Math.log10(2.))

      I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.

      When determining if your main() should print "You know the secret!" or "You should be able to do better!", use the result of log2(maxRange) instead of a hard-coded 10.

    3. After making the other changes, update it to choose to play the game again. When prompting if they want to play again, please accept any form of yes:
      • blank string
      • y (in any case)
      • yes (in any case)
      • yep (in any case)

      Anything else should be considered "no". To help with the prompting design, code, and use these methods:

      1. create a method that checks if a string is blank. "Blank" includes an empty string or any number of spaces or tabs (hint: use replaceAll()).
      2. create a method that checks if a string means yes (as defined above).

      I strongly recommend having a TA or your instructor review your method signatures before writing the code. Office hours or a private Piazza message is a good way to have them reviewed.

    Things to think about:

  2. Write a application called WordCount that reads in a line of data, continuing until the user presses Enter, up to a maximum of 10 lines. Note: this is a partially-filled array, so make sure you write the code properly. Once all the lines have been read in, do the following for each one:

  3. Write a program called CoinToss that uses the Coin class to flip a coin N times, where N is passed in as a command-line argument, and prints out the percentage of heads and tails.

    Sample output:

    Design, code, and use a method called formatPercent() in CoinToss that takes a number and a total and returns the string to print as the percent. For instance, if called with 112 and 200, it returns the string 56%; if called with 48 and 100, it returns the string 48%.

    I strongly recommend having a TA or your instructor review your method signature before writing the code. Office hours or a private Piazza message is a good way to have it reviewed.

    Start the Coin class with this code snippet:

    import java.util.*;
    
    public class Coin
    {
    private static Random r = new Random();
    private CoinSide sideUp;
    
    public Coin()
    {
        sideUp = CoinSide.HEADS;
    }
    public boolean isHeads()
    {
    }
    public boolean isTails()
    {
    }
    public void flip()
    {
        // Generate a random number, either 0 or 1, and use it to set sideUp to HEADS or TAILS
    }
    public String toString()
    {
        return ("Coins side up is: " + sideUp);
    }
    }
    

    Update the Coin class to add the code for the CoinSide enumeration. It should have HEADS and TAILS as the enumeration values and the methods that have not been finished.

  4. Write a program called Primes that uses an array of 1,001 elements to determine and print the prime numbers between 1 and 1,000 (ignore 0). A prime number is one that is not evenly divisible by any number other than 1 and itself.

    Use the following algorithm (called the Seive of Eratosthenes).

    1. Start by assuming all the numbers are prime by creating the array with all the elements set to true. (Hint: what is the dataype for your array?)

    2. Starting with subscript 2 (1 is prime), loop through the rest of the array and check for true.

    3. If the value is true, have inner loop that goes through the rest of the array and checks if the subscript is a multiple of the first subscript. If it is, set that one to false.

    When you are done, only those indices that are prime will stil be true.

    For instance, for the subscript 2, all elements beyond 2 that are multiple of 2 (i.e. 4, 6, 8, 10, ...) will be set to false, indicating that they are not prime. For the subscript 3, the subscripts 6, 9, 18, 21, ... will be set to false. Do not use any additional classes - use a local variable in main() for your array.

  5. Write a program called ConnectTheDots that prompts the user for x and y coordinates as x,y. Your program will continue prompting until the user presses Enter. After getting the input, you code will connect the dots.

    To complete this project, start with this code ConnectTheDots.java and update it based on the instructions in the comments. Only write the code that has this in the comment:

    YOU WRITE THE CODE!

Make sure your name, Andrew ID, and the Academic Integrity statement are in all of your submitted code. Follow the instructions for submitting your homework 2 code.