Homework 2

Skills: loops, substring, regular expressions, reading online documentation, step-wise refinement, designing methods, test plans, algorithm development, coding classes, standard structure of a while loop that gets input,

Classes: String, coding your own class

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 3: Thu by 10pm.
Note: the printed test plan for the palindrome program is due this week, (W2) Wed, before the start of lecture.

Write the following programs:

  1. Write a program called HexFun that will convert a very large binary number to hex using the following basic algorithm:

    1. Break the string into groups of 4, starting from the right-most digit (digit 0).
    2. For each group of 4, convert it to a hex digit.
    3. Concatentate the resulting hex digit with the in-progress result.
    4. Convert and store the next set of 4.

    Code and use the following methods:

    1. public static String convertToHex(String binaryNumber) - takes any size binary number, as a String, and returns the equivalent hexadecimal number, also represented by a String. This method should break the string into groups of 4, call convertHexDigit() with each one, and use concatenation to build the final resulting string. Note: you will use a loop and substring() for this part.
    2. public static String convertHexDigit(String binaryNumber) - takes an up-to 4 digit binary number and returns the equivalent hexadecimal digit (both the argument and result are Strings). You may implement this conversion in any way you want but note that the argument MUST be a String and the returned value MUST be a String.

    Your main() will prompt for a single binary number (as a string), call the convertToHex() method, and print the answer.

    Have your output follow this example dialog:

        Please enter a binary number: 1011
    
        The binary number 1011 is 0xB in hex.
    

  2. Write an application named Sum3 that is based on the program for Exercise 5.12 in your book.

    The Sum3 program should calculate the sum of all integers that are divisible by 3, from 1 to N. Design, code, and use a static method that takes N as an argument and uses the proper loop to return the calculated result.

    Your program should prompt the user for N, print the answer, and continue prompting until the user enters -1.

    Print the answer like this:

        Sum3 for the number 11 is: 18
    
    Note: Think about these questions: How is Sum3 different from 5.12 in the book? How is it the same?

  3. Complete the program for Exercise 5.20 from your book and have your main() prompt for the number of terms. Call this program PiEstimate. You may decide on your own if another method helps or not in solving this problem. If you decide to write it, make sure it is static or your main() cannot call it properly. If you write another method, make sure main() does all the prompting.

    Note: the Output is different from the book!

    Output: instead of printing the whole table of data, show only the first 5 values and then the last five values. At the end, add 2 lines of output, in the following format:

    ♦ If the pattern is NOT found, print:

     1. Searched for 3.14159, not found after N terms.
    Where N is the number of terms.

    ♦ If the pattern IS found, print:

     1. Searched for 3.14159, found after M terms.
    Where M is the number of terms when it first found the pattern.

    ♦ In all cases, end the output with this:

     2. Pi for N terms is result.
    Where N is the number of terms and result is the last value for Pi.

  4. Write a program called PalChecker that checks whether or not a given word is a palindrome. Your program should prompt the user for a single string and print the results using the expected format shown below.

    A palindrome is a string of characters that reads the same forwards or backwards, e.g. "radar" is a palindrome. The program should not care if the case of the letters differ, e.g. "Dad" and "dad" are both palindromes. The program should print out the string that is to be tested and then print out whether or not the string is a palindrome (see below for the expected format of the output). Note that punctuation and whitespace are ignored when checking for a palindrome.

    Design, code, and use the following methods:

    1. strippedString() - return a copy of the passed-in String with all the whitespace and punctuation stripped out. Use replaceAll() to create this new string. (Yes, look it up in the online documentation.)
    2. reverse() - return a copy of the passed-in String with the letters in reverse order. You must code the logic for this method by using substring, concatenation, and loop(s); you cannot use any other built-in methods for this one. Hint: how would you write the code to figure out how long a String is?

    Test Plan for Palindrome Program - note that you should add your own test cases. Please turn in a printed copy of your full test plan by the start of Wednesday's lecture.

  5. Write a program called PrintRightTriangle that is based on program 5.15 in your book. This program should prompt the user for the 3 values that define a right triangle:

    1. the width of the triangle (the widest line in the triangle).
    2. the string to use to print the triangle, so your code will not just print a triangle made of asterisks, but of any characters.
    3. the location of the right angle:

      • bl - bottom left (triangle (a) in your book)
      • tl - top left (triangle (b) in your book)
      • tr - top right (triangle (c) in your book)
      • br - bottom right (triangle (d) in your book)

    Your program should continue prompting and printing right triangles until the user presses enter when prompted for the width.

    To write this program, code a class called RightTriangle. It should have 3 attributes - can you figure out what they are called and what their datatypes are? Remember to make them private. Check your design with a TA by the end of Week 2.

    Write the following methods in your RightTriangle class:

  6. Complete the LineArt program that was started during the Week 2 Short Lab session by adding Part b of Program 4.1 (Fig 4.29) of your book.
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.