CS 112b
Spring 1999
Ileana Streinu

Midterm Exam
Part 1

Tuesday, March 2, 1999 9:00 - 10:20am

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 a[];
      int n;
      int state;

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

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

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

      else
      {
      for (i = 0; i < n ; i++)
      a[i]=i;
      }

      ....







    2. Write (in the space below) simple Java code to compute the maximum element of an array of n elements. Assume that the array is named data, the number of elements is n and appropriate values have been put into these variables by somebody else. 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 maximum value (in a variable named max). 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 maximum value of an array of n elements. For this part, you just need to reuse the code you wrote for the previous problem, and fit it into a function definition. The function returns the maximum value (not index), and it has two parameters (the array and its number of elements). 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. In some program, an integer variable state can take on any of the values between 0 and 4. At some point in your program, you want to test the value of the state variable. For each one of these values, the program does something different (e.g. prints, if the value is 0; draws a square, if the value is 1; draws a circle, if the value is 2, etc.).

      Sketch a Java statement for testing these values and taking on the various actions related to them. You do not need to be specific about the action (i.e. do not write code for drawing a circle, etc. A comment would suffice). The purpose of this question is to see what Java instruction(s) you would use in this case.
























  2. Objects (24 points)

    Define a class named Student. Each Student has an id (integer), year (an integer between 1-4, for the year in college) and a grade (integer).














    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 Student class definition should be in a file named .........................

    2. Write the code for an empty Student 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 Student constructor that takes in three integers (corresponding to id, year and grade 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 Student object.









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




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




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




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




    9. Declare an array of 5 objects of type Student.




    10. Write Java code to fill in the array you declared at step 9 with information about 5 students. Choose whatever values you want, and either fill in the values one statement at a time (5 statements) or in a for loop (repeting 5 times).



















    11. Write a Java statement to print on the Java console (using System.out.println) the year in college of the i-th student 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 students in the array declared at step 9 and created at step 10.




















  3. Sorting (10 points)

    Run the Selection 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 Selection 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 Rectangle and Segment.

    If the last button pressed on was Rectangle, the user can draw a rectangle by click-and-drag. When the user presses the Segment button, a diagonal of the rectangle (from the top leftmost corner to the bottom righmost corner) is drawn. Then the applet will not respond to mouse clicks for drawing rectangles until the Rectangle button is pressed again. In the beginning, the applet behaves as if the Rectangle button has been pressed.

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

    Work under the simplifying assumption that the rectangle is always created from the upper leftmost corner towards the bottom rightmost corner (otherwise the code is slightly more complex).

    The rectangle 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 rectangle is drawn at all these events. The upper left corner is the point where the user first clicked and the lower right corner is where the mouse is currently being positioned.

    The diagonal drawn when the Segment button is pressed goes from the upper left corner to the bottom right corner of the current rectangle. Both the rectangle and the diagonal should be drawn, in this case.

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

Ileana Streinu