CSIT115- Sample Exam Page 1 of 4 public interface TwoDPoints {

advertisement
CSIT115- Sample Exam
Page 1 of 4
Question 1
(a) What is wrong with the following interface?
public interface TwoDPoints {
public int getX(){
return 0;
}
}
(b) Fix the interface in part a.
Question 2
Define each of the following Java or object-oriented terms in 20 words or less:




Interface
Abstract method
Instance variable
Class variable
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;
/** 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
CSIT115- Sample Exam
*/
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
* @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;
}
/**
Page 2 of 4
CSIT115- Sample Exam
Page 3 of 4
* 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;
}
}
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
}
Question 5
Write few sentences about different types of class relationships.
CSIT115- Sample Exam
Page 4 of 4
Question 6
What is the difference between a component and a container? Give few examples of
each.
Download