Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

Lecture 20: Methods
Date: Thursday, November 8, 2012


Lecture Assignment 20

Due at the start of class, Tuesday, November 13.

Nothing to turn in, but your job is to bring questions to class about past lecture assignments and programs that we can review for the exam.

You still need to know everything from before the first exam (mainly Chapters 1-3). You should be able to answer all of textbook's Checkpoint questions in Chapter 4, and all Review Questions and Exercises at the end of Chapter 4.

Here are a few additional sample questions:

1. Assume you have a Java program that includes the following construction of a random number generator object:

  Random r = new Random();

Complete the following Java statements so that they accomplish the task as described in the preceding comment.

// x should contain an integer between 0 and 9
int x = 

// y should contain an integer between 1 and 200
int y = 

// z should contain an integer between -50 and -10 (2 points)
int z =

// a should contain a floating-point value between 0.0 and 100.0 (1 point)
double a = 

// b should contain a floating-point value between -1.0 and 0.0 (2 points)
double b =

// c should contain a floating-point value between -10.0 and 10.0 (2 points)
double c =

2. Convert the following Java conditional to use a switch statement that accomplishes the exact same thing.

if ((day == 1)||(day == 2)||(day == 3)||(day == 4)) {
    hours = 9;
else if (day == 5) {
    hours = 8;
else {
    hours = 0;

3. Rewrite this for loop as a while loop:

  for (int x = 0; x < 40; x += 2) {
    sum += x;

4. Write a group of Java statements that will prompt for and read in a value representing the number of potatoes to mash up for a Thanksgiving dinner, where that value must be between 5 and 25. If a number outside that range is entered, print an appropriate message and ask for the number to be reentered.

5. Consider the Checkout class example

a. Modify the program to read the items from a file named "purchases.txt" and to print its output to a file named "total.txt".

b. Further modify the program so it continues to read values from the file until there are no prices remaining in the file. If any items whose prices are zero or negative are encountered, the program should print an error message but not include that item in the total cost.