CS 112b
Spring 1999
Ileana Streinu

Midterm Exam
Part 1

Thursday, March 25, 1999 10:30 - 12:00am

Write your name and class account on this exam sheet NOW.

General description

There are 4 problems, some subdivided into smaller questions. Do as many as you can, or as much of a bigger problem that you can. There should be enough time to finish, or at least to start working on all the questions.

Start with the ones that you consider to be easiest, then move to those that may take more time.

The last problem comes with several copies of a general Java template. Use one of them to give the final answer to the question, and the others for intermediate solutions, if you want so. ` On each sheet, write your name and the version number.

Advice on the last problem

If you want, you can use extra blank sheets to work out the solution before filling in the Java template(s). Even if you do not finish, submit all intermediate sheets and filled-in templates to receive partial credit. Separate sheets that have not been marked with your name and the version number will not be considered.

To insure you get at least partial credit for this last problem, I recommend that you use the templates for describing intermediate versions of the applet. For instance, you can sketch the structure of the applet in version 1 (e.g. decide the names of the variables and where they go, which methods are going to be used and what part of the code should go into each of them), before working out details for the next version.

Be generous with comments. Part of the grade will be based on how readable and neat your code is.

  1. Basic programming concepts(20 points)

    1. What is the following (partial) program doing? (Explain in a few short sentences, in English).

      int data[];
      int n;
      int button;

      .... // the button variable gets a value somewhere here
      ...

      n=10;
      data = new int[n];

      int i;
      if (button == 0)
      {
      data[0]=0;
      for (i = 1 ; i < n ; i++)
      data[i] = data[i-1];
      }

      else
      {
      for (i = 0; i < n ; i++)
      data[i]=data[i-1]*i;
      }

      ....







    2. Write (in the space below) simple Java code to compute the index of the element of an array of n elements which is equal to a given value x. Assume that the array is named data and the number of elements is n. Assume that ppropriate values have been put into these variables by somebody else (in some other part of the program). I.e. do not worry about declaring the array, allocating space for it or initializing its values. Just write the (few!!) instructions that would produce the desired index value (in a variable named index). However, declare any local variables that you might need for this part of the program to work correctly.

      You should not use any template here, a full Java applet is not being asked for.































    3. Write (in the space below) the definition and full code for a function to compute the same thing as in the previous problem. For this part, you just need to reuse the code you wrote for the previous problem, and fit it into a function definition. Start with the first line of the function definition, specifying its parameters and returned value (to receive partial credit even if you do not have the complete code).































    4. Is the following Java program correct? Explain.
      public class myApplet extends Applet
      {
      int array[];
      int n;

      init()
      {
      n=2;
      array[0]=0;
      array[1]=1;
      }

      }

























  2. Objects (24 points)

    Define a class named Sandwich. Each Sandwich has an id (integer), bread (an integer between 1-4, for the type of bread) and contents (which can be 1 for chicken, 2 for tuna salad).














    Now finish the definition of the class, as well as make use of this class, by answering the following questions. Use the space below each question to write the answer.
    1. The Sandwich class definition should be in a file named .........................

    2. Write the code for an empty Sandwich constructor. By "empty", I mean that it is meant to be used in constructing an object of this type without giving it any specific values provided by the user - but rather initializing the variables with some defaults values (e.g. all 0's).




    3. Write the code for a Sandwich constructor that takes in three integers (corresponding to id, bread and contents values) and initializes the current object with those values.









    4. Write the code for a Print method, which prints on the Java console the values of the three variables of the current Sandwich object.









    5. Write a statement that declares a variable of type Sandwich.




    6. Write a statement that creates (constructs) an object of type Sandwich with initial values (35, 2, 1) and assigns it to the variable defined at step 5.




    7. Write a statement that simultaneously declares a variable of type Sandwich and assigns to it a new object of type Sandwich with initial values (46, 3, 2).




    8. Write a statement that applies the method Print on the object of type Sandwich, which has been created at step 6 above.




    9. Declare an array of 10 objects of type Sandwich.




    10. Write Java code to fill in the array you declared at step 9 with information about 10 students. Fill in the values in a for loop (repeting 10 times).



















    11. Write a Java statement to print on the Java console (using System.out.println) the bread used for the i-th sandwich in the array declared at step 9, where i is an integer variable.




    12. Write Java code (a few well chosen statements, not a complete Java applet!!!) to print on the Java console the information about all the sandwiches in the array declared at step 9 and created at step 10.




















  3. Sorting (10 points)

    Run the Insertion Sort algorithm on the following array of integers:

    { 4, 13, 2, 5, 21, 3}

    Explain whether you are using the min or max version of the algorithm.

    I mean: I do not want to see only the final sorted array!! You should work in stages, following the Insertion Sort algorithm. At each stage, write down how the array currently looks. You do not need to explain what the algorithm does. You do not need to write the code for the algorithm. Just show the array, after each stage.
































  4. Java applet basics (46 points)

    The applet has two buttons and responds to mouse clicks. The buttons are labeled Circle and Fill.

    If the last button pressed on was Circle, the user can draw an empty circle by click-and-drag. When the user presses the Fill button, the last circle that was drawn on the screen is now drawn as a filled circle.Then the applet will not respond to mouse clicks for drawing circles until the Circle button is pressed again. In the beginning, the applet behaves as if the Circle button has been pressed.

    The applet draws only one circle at a time. It does not remember what the user did in the past. Each time the user draws a new circle, the one that was there before will disappear. Each time the circle button is pressed, the old circle disappears from the screen.

    The circle drawing is done by click and drag. The user clicks on a point on the screen, then drags the mouse towards the bottom right part of the screen and finally releases the mouse. A circle is drawn at all these events.

    The myApplet.java template.
Last modified March 24, 1999.

Ileana Streinu