CS 178 Programming with Multimedia Objects

Fall 2004

Readings and Practice Problems for the Final Exam (with Solutions)

Prepared by: Aditya Mathur

Final Exam schedule: Dec 15, 2004. 10:20am-12:20pm Room: TBA

Latest update: Dec 12, 2004

Readings for the Final Exam
Chapter
Sections
Exclude
2 2.1, 2.2. 2.3 Graphical programming (drawing points, lines, rectangles, etc)
3 3.1, 3.2, 3.3
4 4.1, 4.2, 4.3, 4.4 Canvas, Windows, Dialog, Popup Menus
5 5.1, 5.2,5.3 5.4 Packages, Protected, subclass
6 6.1, 6.2, 6.3, 6.4 switch, abstract classes, interfaces
7 7.1, 7.2, 7.3, 7.4
8 8.1, 8.2, 8.3, 8.5 Multidimensional arrays, heterogeneous arrays
9 9.1, 9.2 Throwing exceptions
10 10.1, 10.2 I/O for class types
12 12.3 All but Animation I. We expect you to understand interfaces

Runnable and Threads and methods:

start(), stop(), and run().

Part A: Closed Book. 10 multiple choice questions. Total: 20 points. Time: allowed: 30 minutes. It will cover the entire course material listed in the table above. No exceptions.

Part B: Open Book. Total: 80 points. Time allowed: 90 minutes.Two questions. Question 1: You will be asked to code a given algorithm into a Java applet. This and the next question will require a good understanding of all material covered during the class, labs, and projects.

Question 2: You will be asked to write Java code for a class. The class will have a few constructors, private and public methods, private and public variables, and static variables. The practice problems below should help you prepare to deal with this question.

Sample question for Part A:

geneName is an array of charS. Which of the following statements converts genename into an object g of type String?

(a) String g=new String("geneName");

(b) String g=geneName;

(c) String g=new String(geneName);

(d) String g=new char(geneName);

Preparation for Part B:

  1. Examine the DNAToProtein and ElementAndCompounds applets presented during lectures on November 10, 2004 (these are linked to the schedule page). For each applet, try writing the code on your own with help from the textbook, with help from Eclipse, without help from anyone, and without consulting your notes, then you are well prepared for Exam 2.
  2. Let s be an object of type String. Write a method named allSubstrings that takes s as an argument and returns an array of all substrings in s that begin with the first character in s, including the empty substring. For example, if s="Hello" then the allSubstrings method must return the following substrings, in this order: "", "H", "He", "Hel", "Hell", "Hello"

Source to Problem 2 (please look at it after attempting the problem yourself)

3. Array manipulation: Consider the following arrays:

    • String [] planetName=new String [maxPlanets];
    • double [] distanceFromEarth=new double [maxPlanets];
    • double [] travelTime=new double[maxPlanets];

Write a method findTime(double speed) that sets travelTime[j] to the time required for a spaceship to travel from the earth to planetName]j]. Assume that distanceFromEarth[j] is the distance in miles from the earth to planetName[j]. The argument to findTime( ) is the speed in miles/hr of the spaceship.

Solution: This is a simple problem. Use a loop for call findTime as many times as there are planets in the planetName array. Finding travel time given distance and speed is trivial.

4. Array manipulation. Consider the following two declarations:

    • String [] x=new String[maxElements];
    • String []y=new String[maxElements];

Write a method named arrayReverse() that reverses the elements in array x into array y. For example, if array x is:

x={"helium", "hydrogen", "iron", "uranium"} then the array y should be

y={"uranium", "iron", "hydrogen", "helium"}

Source to Problem 4 (please look at it after attempting the problem yourself)

5. Constructing your own class. (a) Write a class named Dog. It has the following constructors:

    • Dog(String breed, String name)
    • Dog(String name)

(b) The Dog class provides the following methods:

public void setBreed(String breed) // Sets the breed of a Dog object.

public void setWeight(double weight) // This sets the weight of a Dog object.

public double getWeight() // Returns the weight of the Dog object.

public setOwner(String owner) // This sets the owner of a Dog object.

public String getOwner() // Returns the name of the owner of the Dog object.

public String getBreed(); // Returns the breed of the dog object.

public String getName() // Returns the name of the dog object.

(c) Write an applet named DogApplet that implements ActionListener. The applet has buttons labeled: NewDog, FindByBreed, FindByOwner, and showAll. It has text fields for typing dog breed, its name, its weight, and its owner. There is one text area named displayArea to display information about dogs created so far.

(d) When the user clicks on the NewDog button, the applet creates a new Dog object with its breed, name, weight, and owner. Assume that the applet can create and save in memory at most 100 dog objects.

A user can query the applet. When the user clicks the FindByBreed button, a list of all dogs with the user specified breed is displayed in the displayArea in the sorted order by breed. Similarly, when the user clicks the FindByOwner button, a list of all dogs owned by an owner is displayed in th sorted order by owner. Clicking the showAll button causes the entire database to be displayed in the alphabetical order of breeds. Use frames/panels/menus to make your applet look nice and easy to use!

Source to Problem 5 (please look at it after attempting the problem yourself)

The next two problems are based on the ButtonFun applet discussed on Dec 3 during recitation. Examine the ButtonFun applet. We will make several modifications to this applet and improve the game it implements.

6. Saving and retrieving the score

  • Declare scoreFile as a FileOutputStream and connect it to a DataOutputStream.
  • Add two buttons labeled Save Score and Retrieve Score. Clicking the Save Score button will ask the player for a file name, open this file as a FileOutputStream, and write the scores to the file in the following format in two lines:

1 score number_of_clicks

2 score number_of_clicks

For example, a sample output for the two players could be:

1 20 59

2 18 75

  • Clicking on the Retrieve Score button will ask the player for the filename from which the score is to be retrieved and display it in the appropriate boxes. Note that reading the scores from the file will require the creation of a
  • FileInputStream object and connecting it to a DataInputStream. See the example dsicussed in the class on Oct 20, 2004.

Source for Problems 6 and 7 (please look at it after attempting the problems yourself)

The score save/retrieve feature will work if you compile and run the program on the same computer.

7. Making it a 2-person game

  • Add two buttons labeled Person 1 and Person 2. Prior to the start of the game the player clicks on one of these two buttons to indicate which player she/he is. The default is Player 1.
  • Add a text widget to display the score for Player 2. Make this widget identical to the one that already exists in ButtonFun.
  • Add variables to keep track of the score and number of clicks of the second player. Modify processGameButton() method so that the score of the current player is incremented and displayed.