Computer Science 212 - Quiz 1 preparation
Practice problems (with answers)


Quiz 1 will contain a problem similar to one of the programming exercises below. Links to answers are provided at the bottom of each problem. You may also download all the answers in a ZIP file here.

It is recommended that you write some of the programs using command-line arguments, others using JOptionPane dialog boxes, and others using console input (as explained in lab and in the Tutorial on some Java basics), so you can gain practice with all three kinds of input. For additional practice, write three versions of each program, one for each style of input.


  1. Write a program containing a static method which takes a string parameter and returns the number of lower-case vowels ('a', 'e', 'i', 'o', 'u') in the string. Write a main method which inputs a string, entered by the user, and then outputs an appropriate message containing the value returned by the other function.
  2. For example, given the following sample input:

       Hello, there, this is a string.
    

    The output would be:

       Your string contained 8 lower-case vowels.
    

    Answers:


  3. Write a program containing a static method isPositiveOdd which takes an array of long integers as a parameter and returns a boolean value which will be true if the array consists of positive odd numbers only, false otherwise. Write a main method which inputs a sequence of long integers, entered by the user, and then outputs an appropriate message containing the value returned by the other function.
  4. In your version with command-line argument input, let the array of long integers have the same length as command-line argument array, and let the isPositiveOdd method assume that the array is completely filled. On the other hand, for the console input and dialog box versions, assume that the user will enter some unknown number of numbers, but no more than 50 numbers, and write the isPositiveOdd method so that it processes a subarray of some specified length, rather than the entire array. The main method should prompt the user to enter numbers one at a time and to type 'X' to quit.

    Answers:


  5. Write a method which computes the value of a quadratic expression a x^2 + bx + c, where a, b, c, and x are the method's parameters, all of type double. The method should return the value of the quadratic.
  6. Then write a main method which prompts the user to enter values for the three coefficients and x, and which then outputs the value of the quadratic as computed by your other method.

    Answers:


  7. Write a method which computes the value of a polynomial whose coefficients are stored in an array of double, where the zero-order term is stored at index 0 in the array and the highest-order term is stored at the highest index in the array. The method should have two parameters: (1) the array of coefficients, which is assumed to be completely filled, and (2) x, which is of type double as well. The method should return the polynomial's computed value, also of type double.
  8. Then write a main method which (in the versions using console input or dialog boxes for input) first prompts the user to enter the degree of the polynomial as a non-negative integer. The program should then create an array of length n+1, where n is the degree of the polynomial, and then prompt the user to enter the coefficients. After the user has entered the coefficients, the program should then prompt the user to enter x. It should then output the result computed by the other method.

    In a version using command-line arguments, the last command-line argument should be x and the other command-line arguments should be the coefficients, starting with the highest-order coefficient. When you use command-line arguments for all inputs, the degree of the polynomial can be determined from the number of command-line arguments and need not be included in the command-line arguments themselves.

    Answers:



Computer Science 212 | Quiz 1 preparation