CS 1711 Recitation Laboratory 4:
Review for Exam 2

Review for Exam 2

There is nothing to turn in for this recitation. You will get full credit for it if you show up and make an honest attempt to do the review problems given here.

The review problems in this recitation are a sample of some of the types of problems you should expect on the exam. The exam may also contain other types of problems as listed in the Week 6 Lecture Review.

Some of the things not covered here include completing partially written class definitions and diagramming the state of variables.

The presenter will not finish all of the problems given in the lab. If this is the case, you should finish them on your own.

All of the questions have solutions, available by clicking on the underbar at the beginning of the question. These will be released after the last recitation section of the week it meets.

The problem list has been broken into two parts. The first parts contains problems specific to the material covered in class. The second part contains general programming problems that you should be able to do using your knowledge of the previous course.

Part 1

  1. _ Suppose we have the following declaration:
    String s = "xyzabcdefabc";
    Find the value of each of the following:
    1. s.charAt(7)
    2. s.indexOf("abc")
    3. s.indexOf("abx")
    4. s.length()
    5. s.substring(3,7)
    6. s.substring(3)
  2. _ An Employee has a first name, last name, social security number, home address, and monthly salary. Assume reasonable names for the attributes and write a toString method the descrribes the state of the object. Each attribute should be included with a description and be on its own line.
  3. _ Design a class to represent an song. A song has a title, and artist, a length, and a price. All of these should be accessible, but once created, only the price can be changed. Have the class implement Comparable based on the title. Write a toString method that can be used to print the entire state of the object in an easily identifiable format with one item per line.
  4. _ Suppose that a Song has an artist. Assume an appropriate data type for artist and that the Song class has appropriate getter methods. Write a method that takes an array of songs and an artist as parameters andl returns the number of songs with that artist.
  5. _ Write a method that takes two parameters, an array of String and an int, and prints those strings that have length at least equal to the second parameter. Each string should be on its own line.

Part 2

  1. _ Write a code segment that sets average equal to sum divided by count if count is not zero. Otherwise, average is not changed.
  2. _ Write a code segment that sets wages based on hoursWorked. Employees are paid at a rate of $12 an hour for the first 30 hours and $15 an hour for all hours over 30.
  3. _ Suppose we have:
       int x = 5;
       int y = 3;
       double z = 3.0;
       double w = 2.0;
    Evaluate each of the following expressions:
    1. x/y
    2. x/z
    3. (x + w + z)/y
    4. x % y
    5. x/y+z
  4. _ When buying nuts in bulk, the price per pound depends on the number of pounds purchased according to the table below. Write a code segment that sets the total price, totalPrice, from the number of pounds purchased, poundsPurchased. You can assume that totalPrice has already been declared as a double and poundsPurchased has been declared as an int. You can assume that poundsPurchased is not negative.
    Note: you are asked to calculate the total price, while the table gives the price per pound.
    pounds purchasedprice per pound
    2 or fewer $8.95
    3 to 9$7.85
    10 to 19 $6.50
    20 to 49 $5.95
    50 or over $5.00
    Note: there are many correct and many incorrect solutions to this problem. If you want to see if you did it correctly, make a eclipse project with two classes. In one class write a method that takes poundsPurchased as a parameter, sets totalPrice using your solutions, and returns totalPrice. Now write a test program that dows the calculation for the following number of pounds: 0, 1, 2, 3, 4, 8, 9, 10, 11, 18, 19, 20, 21, 48, 49, 50 and 51.
  5. _ Write a code segment that prompts the user for two numbers and outputs the maximum of the numbers entered.
  6. _ Write a code segment using a do - while loop that continues prompting the user to enter an integer until the value entered is between 1 and 10. After reading in the desired value, print it to the screen. Start with the line:
    Scanner scan = new Scanner(System.in);
  7. _ Write a method:
    public void outputChars(char c, int n):
    that prints the character, c, n times in a row on the same line.
  8. _ Write a code segment that will output an isoceles triangle of asterisks as in the example below. The first row contains 1 asterisk, the second contains 3, and third contains 5, etc. The variable rows contains the number of rows of asterisks. If rows has the value 4, the output should look like this:
    The last row of asterisks has no leading blanks. You may assume that rows is greater than 0.
  9. _ Write a code segment that sets the boolean variable isEven to true if the integer val is even and sets isEven to false if val is odd.
  10. _ Write a code segment that declares an array of 1000 integers, creates the array, and sets each element to the square of its index.
  11. _ Declare and create an array of 1000 integers and set the elements with odd index to -1 and the elements with even index to +1.
  12. _ Write a code segment that doubles all of the values in the integer array, values.
  13. _ Write a code segment that sets count to the number of times val appears in the array, values.
  14. _ Suppose that a is an array of doubles. Write a code segment that sets min to the minimum value in the array. If the array has no elements, do not change min.
  15. _ Write a code segment that reverses the elements in the integer array, a.
  16. _ Write a method that has an array of doubles as its only parameter, and reverses the elements in the array.
  17. _ Write a method that has an array of doubles as it only parameter, and returns a new array with the elements in reverse order.
  18. _ Given the following declaration:
    String[] x = new String[1000];
    Assume that the array has been filled appropriately. Write a code segment to interchange the element with index 3 with the one with index 7. Declare any additional variables that you need to use.
  19. _ Write a method:
    public void interchange(double[] a, int j, int k);
    that interchanges the element of a at index j with the element at index k.
  20. _ Write a method:
    public int countOccurrences(String x, char c);
    that returns the number of times c appears in x.