PIC 20A Lecture 1, Summer 2001

Instructor: Fedor A. Andrianov

MIDTERM EXAM



STUDENT DATA:




1. (30 points):
You have been given a design document for a veterinary registration system for implementation in Java. It states: "A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating if it has been neutered, and a textual description of its markings." Given that the Pet class has already been defined and you expect the Cat class to be used freely throughout the application, please answer the following questions:



2. (30 points):
The following program contains several bugs. Please find and fix them.
    public class Bob extends Cloneable {

	protected Bob clone() {
	    return this;
	}

	public static int main(String[] args) {
	    Bob[] msoft = new Bob[1000];
	    Bob team [1000];
	    for (int j = 0; j < msoft.length(); j++) {
		team[j] = msoft[j].clone();
	    }
	    delete msoft[];
	}

    }



Answer:





























3. (40 points):
Create a program SwapChar that takes a string and two integers as command-line arguments. The program should print out a modified string obtained from the original string by swaping two characters at positions specified by the two integers. For example, suppose that you enter the following:
    java SwapChar oat 0 2
The program should display
    tao
and exit. In addition, your program should catch and handle all exceptions that might arise because of a bad input. Each type of exception should be handled independently, by printing an appropriate error message which would specify what exactly was wrong with the input. Hint: please remember that invalid array access causes ArrayIndexOutOfBoundsException. You might also find useful some of the following methods:
    public static int java.lang.Integer.parseInt(String s)
	    throws NumberFormatException;
    public char java.lang.String.charAt(int index)
	    throws IndexOutOfBoundsException;
    public char java.lang.StringBuffer.charAt(int index)
	    throws IndexOutOfBoundsException;
    public void java.lang.StringBuffer.setCharAt(int index, char c)
	    throws IndexOutOfBoundsException;
    public java.lang.StringBuffer java.lang.StringBuffer.append(char c);
    


Good Luck !

Last modified on by fedandr@math.ucla.edu.