CSCI A201/A597

Lecture Notes 15

Spring 2000


Second midterm review. A look ahead.

1. A Note on the Practical

First a note on the practical exam, most of you did very well on it, and grades have been posted already. There's one thing that you need to remember from this experience: don't stop when you hit a roadblock. Go around it, invent a bridge, simulate the road. Here's an example.

A few of you got stuck in the syntax of

Integer.parseInt(args[0])
What was this used for? To get data from the user. So if you can't remember exactly the syntax, and you want to test your program, what do you do? The easiest is to just ignore the user and feed data in your program directly. Take a look at this:
public class Practical {
  public static void main(String[] args) {
    int size = Integer.parseInt(args[0]), i, j;
      for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
          if (j == 0 || i == size - 1 && j <= (size - 1) / 2) {
            System.out.print("*"); 
          } else { 
            System.out.print(" "); 
          } 
        }
      System.out.println(); 
    }
  }
}
You want to test your program to see if it produces the pattern that you are designing it for (presumably, an uppercase "L") and you can't remember the exact form of the line that initializes the data from the command line. You have to keep going, you can't spend the whole two hours on just trying to figure that out. Replace it with a constant, 10 for example.

public class Practical {
  public static void main(String[] args) {
    int size = 10, i, j;
      for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
          if (j == 0 || i == size - 1 && j <= (size - 1) / 2) {
            System.out.print("*"); 
          } else { 
            System.out.print(" "); 
          } 
        }
      System.out.println(); 
    }
  }
}
And now you can only produce patterns of a certain size but at least you can test them out. You can change the size with a bit more trouble than if you had access to the command line, but you can still change it by replacing the constant with another value in the program source code and then recompiling.

This way you get as much partial credit as you deserve. And, perhaps by the time you're done finishing testing and developing the program this way you may even actually remember the exact syntax of the line that you were missing.

2. Midterm Review

I include below a number of exercises, with solutions, similar to the ones that you will see on the midterm exam. The midterm will be composed of only objective items, that is items that are either multiple-choice, or for which there is just one answer that you need to provide.

The questions on the midterm will test your overall basic understanding of your basic Java knowledge. Here's what it will cover:

In class we will discuss a few examples that I am listing below, and take any questions that you may have. Questions on the midterm will be like the ones you've seen in Quizsite in the last three assignments (we had about 110 items posted in all, I believe).

Example 1

Here's a question similar to the first question on quiz 4 that was due last week. Assume option can be only 1, 2 or 3 before each of the code fragments illustrated below. Which ones set i to the value that option has? We will look at this kind of problems with the help of diagrams.

Code Diagram
if (option == 1) 
  i = 1;
else if (option == 2) 
  i = 2;
else 
  i = 3;
In this particular case it's easy to see that the code sets i to the same value as option (given the assumption about the possible values that option can have).

For the remaining of these notes we will draw the diagrams and fill them with text in class.

Code Diagram
i = 1;
if (option >= 2)
  i = i + 1;
if (option == 3)
  i = i + 1; 

Code Diagram
if (option == 1)
  i = 1;
if (option == 2)
  i = 2;
else 
  i = 3;

Code Diagram
i = 1;
if (option > 1)
  if (option > 2)
    i = 3;
  else 
    i = 2;

Code Diagram
i = 1 if (option == 1);
i = 2 if (option == 2);
i = 3 if (option == 3);
Incorrect syntax, no diagram.

Example 2

Consider this code fragment

if (x > y)
  z = z + 1;
else 
  z = z + 2;
Which one of the following are equivalent to it? Note that the structure of the diagram is as important as what gets written inside the boxes. We sketch the diagrams below.

Code Diagram
if (x > y)
  z = z + 1; 
if (x <= y)
  z = z + 2;
Code Diagram
if (! (x > y))
  z = z + 2;
else
  z = z + 1;
Code Diagram
z = z + 1;
if (x <= y)
  z = z + 1;
Code Diagram
if (x > y)
  z = z + 1;
else if (x <= y)
  z = z + 2;
Code Diagram
z = z + 2;
if (!(x > y))
  z = z - 1;

Two code fragments will be considered equivalent when they behave in the same way (same output, same internal state) for identical inputs, for all possible inputs.

Other kinds of problems that you can see on the exam could be like the following two.

Problem 1 Consider the following two program fragments:

Fragment 1 Fragment 2
if (x == 5)
  x = x + 1;
else 
  x = 8;
if (x == 5)
  x = x + 1;
if (x != 5)
  x = 8;

Check all that apply:

Problem 2

Assume that x and y are integer variables. Consider the following nested if statement.

if (x > 3)
  if (x <= 5) 
    y = 1;
  else if (x != 6) 
    y = 2;
  else 
    y = 3;
else 
  y = 4;
If y has the value 2 after executing the above program fragment, then what do you know about x?

Problem 3

Assume that x and y are integer variables. and the code fragment shown below:

if (x > 3) { 
  if (x <= 5) 
    y = 1;
  else if (x != 6) 
    y = 2;
} else 
  y = 3;
Questions:
  1. If x is 1 before the fragment gets executed what's the value of y after the fragment is executed?

  2. Now erase the curly braces. What value does x need to have before the fragment gets executed for y to be 3 at the end of the fragment?
Assignment 6

Remember that assignment 6 consists of 6 batches of about 10 problems each, all objective, and open-ended, graded automatically by QuizSite. Some are true-false or multiple-choice. Note the due date: Thursday, before the midterm, at 10am.

After the midterm in lab eight we start on assignment 7 which will be due at the end of the lab next week (but you can turn it in earlier). Turning in will done through the handin program as before the QuizSite exercises.

Please bring these notes with you to class tomorrow.


Last modified: February 28, 2000 by Adrian German