Friday, December 2, 2011

Exam #3 programming problems

I strongly suggest that each student develop a Java solution to the following problems in preparation for the coding portion of the first exam. While I do not promise that the practical part of the exam will be *exactly* the same as any one of these problems, the actual test question will at least be very similar to several of these practice problems.

Watch this post - more to come!

  1. You are to complete an exercise similar to 8.10, p 298. The class name must be QuadraticEquation. This class will have no main and will be tested against my own driver program. Any input/output must be done at the command line.

    The private data members must be:

       doubles:
          a   
    // coefficient of x^2
          b   
    // coefficient of x
          c   
    // constant
          d   
    // discriminant

    These are the required function headers:

       private helper functions:
          private double calculateDiscriminant()

       constructor:
          public QuadraticEquation(double coA,
             double coB, double coC)

       non-static public functions:
          public double getRoot1()
          public double getRoot2()
          public boolean displayRoots()

       accessor functions:
          public double getA()
          public double getB()
          public double getB()
          public double getDiscriminant()


    Sample Output
    Here is a screenshot of my code running.

  2. Quadratic Equation driver output


  3. Morse Code is the representation of individual characters and numerals as a series of dots and dashes. Write a program which reads in an integer from the user and converts it to an array of individual digits which are then printed out on the console using their Morse codes.

    You should use the following constant String array in your code:

       public static final String MORSE_CODE[] = {
          "-----",
    // zero
          ".----",
    // one
          "..---",
    // two
          "...--",
    // three
          "....-",
    // four
          ".....",
    // five
          "-....",
    // six
          "--...",
    // seven
          "---..",
    // eight
          "----." };
    // nine

    These are the required function headers:

       public void printMorseCode()
       public static MorseCodeNos[]
          convertToMorseCode(int n)
       public static void
          printArray(MorseCodeNos[] mcn)


    An object of type MorseCodeNos has a single private data member, morse, which is of type String.

    Sample Output
    Here is a screenshot of my code running.

  4. Morse Code Nos output


  5. Write a program which reads in the user's name as "first mi last" then creates from that string a phone book style entry, "last, first mi". You must write two methods.

    These are the required function headers:

       public static String getFullName()
       public static String
          assemblePhoneBookEntry(String full)


    You will need to make use of the StringBuilder class and at least two of the manipulation methods of the String class.

    Sample Output
    Here is a screenshot of my code running.

  6. Phone Book output


  7. Write a program, StringStats, which prompts the user to enter a string and then reports back to the user statistics about their string. Your program should have one method to get the user's input string and one method that can be used to determine how many spaces, or how many lowercase characters, or how many uppercase characters, or how many digit characters are in the string. This class uses several methods from the Character class: isSpaceChar, isUpperCase, isLowerCase, isDigit.

    These are the required function headers:

       public static String getLine()
       public static int countItem(String s,
          char item)


  8. Sample Output
    Here is a screenshot of my code running.

    String Stats output


  9. Write a program, Vowels, which prompts the user to enter a string and then reports back to the user how many vowels (upper-and lowercase) are in their string. Your program should have one method to get the user's input string and one method that can be used to determine how many vowel characters are in the string.

    These are the required function headers:

       public static String getLine()
       public static int countVowels(String s)


    Sample Output
    Here is a screenshot of my code running.


  10. Vowels output


  11. Write a program, SimplePunctuation, which prompts the user to enter a string and then reports back to the user how many punctuation marks are in their string. Your program should have one method to get the user's input string and one method that can be used to determine how many punctuation marks are in the string. For the sake of simplicity, you may limit your detection to the punctuation marks in the set { , . ? !}

    These are the required function headers:

       public static String getLine()
       public static int countPunct(String s)


    Sample Output
    Here is a screenshot of my code running.


  12. SimplePunctuation output


  13. Write a program, Words, which prompts the user to enter a string and then reports back to the user how many words are in their string. Your program should have one method to get the user's input string and one method that can be used to determine how many words are in the string. You'll need to strip out any digits in your user's string.

    These are the required function headers:

       public static String getLine()
       public static int countWords(String s)


    Sample Output
    Here is a screenshot of my code running.


  14. Words output


  15. Write a GUI application that reads in an unknown number of test scores from a text file, sums the scores, and reports the average and the number of scores to the user. You may make use of the JFileChooser class, if you like.

    These are the required function headers:

       public static File getFile()
          throws Exception
       public static void wrangleTestScores(
          File file) throws Exception
       public static void main(String[] args)
          throws Exception



    Sample Output
    Here is a screenshot of my code running.


  16. Gooey Grades output


  17. At Gringotts Wizarding Bank, currency comes in bronze Knuts, silver Sickles, and golden Galleons.

    1 Galleon = 17 Sickles and 1 Sickle = 29 Knuts

    One Galleon is equivalent to $7.83 at current exchange rates. Write a GUI program called CurrencyConverter to calculate how many Galleons, Sickles, and Knuts you should receive in return for $100, using the minimum number of coins possible.

    These are the required function headers:

       public static int getDollars()
       public static String convert(int cash)


    Sample Output
    Here are screenshots of my code running.


  18. Currency Converter input

    Currency Converter output


  19. Write a program that uses a regular expression to test the validity of a US ZIP code.

    These are the required function headers:

       public static String getZip(Scanner input)
       public static boolean isValid(
          String zipcode)


    Sample Output
    Here are screenshots of my code running.


  20. Zip Code valid 5-digit output

    Zip Code valid 9-digit output

    Zip Code invalid output