Practice Exam Solution

advertisement

CSIT115- Sample Exam Page 1 of 5

Question 1

(a) What is wrong with the following interface? public interface TwoDPoints { public int getX(){ return 0;

}

}

All the methods in interface should be abstract i.e. without any implementation whereas in TwoDPoints interface getX () is implemented

(b) Fix the interface in part a. public interface TwoDPoints { public int getX();

}

Question 2

Define each of the following Java or object-oriented terms in 20 words or less:

Interface: A Java interface is a collection of constants and abstract methods . An interface is used to identify a set of methods that a class will implement

Abstract method: An abstract method is a method header without a method body, i.e. No { }

Instance variable

– a variable that has a different instance for each object of a class.

Class variable

– a variable that has one instance for the entire class. A class variable is indicated with the static modifier.

Question 3

Draw a UML Class Diagram for the following class.

/**

* This class represents a pizza.

*/ public class Pizza {

/** The different toppings of this pizza */ private String[] toppings;

CSIT115- Sample Exam Page 2 of 5

/** The size of the pizza (small, medium, large) */ private String size;

/** How much this pizza costs, in dollars */ private double price;

/** The current number of toppings on this pizza */ private int numToppings;

/**

* Create a pizza, with a given size and number of toppings

*

* @param size

* The size of the pizza

* @param toppingsOrdered

* The number of toppings allowed for this pizza

*/ public Pizza(String size, double price) { this.size = size; this.price = price; toppings = new String[4];

} this.numToppings = 0;

/** add a topping to the pizza */ public void addTopping(String topping) {

} toppings[numToppings] = topping; numToppings++;

/**

* Get pizza price

* @return Returns the price.

*/ public double getPrice() { return price;

}

/**

* Set pizza price

* @param price

* The price to set.

*/ public void setPrice(double price) { this.price = price;

}

/**

* Get pizza size

* @return Returns the size.

*/ public String getSize() { return size;

}

/**

* Set pizza size

CSIT115- Sample Exam

}

Page 3 of 5

* @param size

* The size to set.

*/ public void setSize(String size) { this.size = size;

}

/**

* Get pizza toppings

* @return Returns the toppings.

*/ public String[] getToppings() { return toppings;

}

}

/**

* Set pizza toppings

* @param toppings

* The toppings to set.

*/ public void setToppings(String[] toppings) { this.toppings = toppings;

}

/**

* Return the string equivalent of this pizza

*

* @return Returns a string, representing the pizza

*/ public String toString() {

String returnString = size + " pizza, with"; if (numToppings == 0) returnString = returnString + " no toppings."; else { for (int i = 0; i < numToppings; i++) { returnString += " " + toppings[i];

}

} return returnString;

CSIT115- Sample Exam Page 4 of 5

Pizza

- toppings: String[]

- size: String

- price: double

- numToppings: int

{

}

}

+ addTopping(topping: String): void

+ getToppings(): String[]

+ setToppings(toppings: String[]):void

+ getSize(): String

+ setSize(size: String) : void

+ getPrice() : double

+ setPrice(price: double) : void

+ toString() : String

Question 4

Write code to reverse an integer array. For example if the array has 2, 10, 5, 9, 7, then output should be 7, 9, 5, 10, 2. public void reverse {int[] theArray){

//Write your code here

int length = theArray.length; for(int i = 0; i < length/2 ; i++) int temp = theArray[i]; theArray[i] = theArray[length – i - 1]; theArray[length – i – 1] = temp;

CSIT115- Sample Exam Page 5 of 5

Question 5

Write few sentences about different types of class relationships.

Dependency

Aggregation

Inheritance

Refer to Lecture 6

Question 6

What is the difference between a component and a container? Give few examples of each.

A GUI container is a component that is used to hold and organize other components

JFrame, JDialog, and JApplet are the three top level containers that are used to display graphics in GUI applications

Component is an object that defines a screen element to display information.

Examples are JButton, JLabel

Download