UCL Logo

Problem Class Questions 2006
for COMP1008 (Object-Oriented Programming)

Week 4 (Starting 6th February)

These questions are taken from past exam papers from other universities. They should all provide good practice for you! If you want to do well in the final exam you need to be able to answer questions like this quickly and with confidence.

Q1. Write a class called Product.  An Product object should represent a product stocked in a supermarket, e.g. a 500 gram pot of Onken pineapple yogurt.  It should contain the following information: a code for the product, the name of the product, the cost of the product, and the quantity of the product currently in stock.  Assume the code and the name are represented by strings of characters.  Include the following constructor and methods in the class definition.

(i)  A constructor Product(code,name) which creates a new product with the given code and name.  Initially, the cost and the quantity of the product should be set to zero.

(ii) An instance method getName() that will return the name of the product.

(iii) An instance method addStock(int n) that will add n to the quantity of the product in stock.

(iv) An instance method outOfStock() that will return the value true if there is none of this product in stock.  Otherwise it will return false.

It should not be possible to access the attributes of a Product object except by using the methods listed above.

 

Q2. Examine the following program:

 class QClass 
 {
   public static void main(String [] args) 
   {
     int arr[] = new int[args.length];
     float var1 = 0; 
   
     for (int i = 0; i < args.length; i++) 
       arr[i] = (new Integer(args[i])).intValue();
   
     for (int i = 0; i < arr.length; i++)
       var1 = (i%2==0)?0:arr[i]; 
     System.out.println(var1/args.length);
   }
}

What would be the output of the above program given the following invocation:
> java QClass 20 45 60 85 100 205 300 405

(Note: ?: is the conditional operator. It has the form (boolean-exp) ? <true-value> : <false-value>. If the boolean expression evaluates to true the true-value is returned otherwise the false-value.)

 

Q3. Write a complete class named VoteCounter that can be used to keep track of the votes on a yes/no question. The class should contain two instance variables to count the number of yes votes and the number of no votes. It should have a method voteYes() that is called to cast a "yes" vote, and a method voteNo() that is called to cast a "no" vote. Finally, it should have a method, getWinner(), to report whether ``yes'' or "no" got the most votes. This method should return 1 if "yes" has more votes, 2 if "no" has more votes, or 0 if both have the same number of votes.

 

Q4. a) Suppose that you wanted to represent roman numbers as objects. How would you design a class, RomanNumber, so that each object belonging to the class represents a roman number? List the instance variables, methods, and constructors that you would include in the class, and state what each one is for. For the methods and constructors, specify the parameters and return type as well as the name.

b) Identify how you would implement addition, subtraction and multiplication methods.

b) Assume you have a working RomanNumber class. Write a complete main program, using that class, that will read two roman numbers from the user, add them, and print the result. The class you described in parts a) and b) must include any methods that you need to write this program.

 

Q5. Object-oriented programming is supposed to make it easier to design a program to perform a given task. Explain what is meant by object-oriented programming, and discuss what it offers to help a programmer who is confronted with a complex programming task.

 

Q6. What will happen if you try to compile/run the following code?

   public class Q21
   {
     int maxElements; 
     void Q21()
     {
       maxElements = 100;
       System.out.println(maxElements);
     }  
     Q21(int i)
     {
       maxElements = i;
       System.out.println(maxElements);
     }  
     public static void main(String[] args)
     {
       Q21 a = new Q21();
       Q21 b = new Q21(999);
     }
   }
Last updated: September 2, 2006

Computer Science Department - University College London - Gower Street - London - WC1E 6BT - Telephone: +44 (0)20 7679 7214 - Copyright 1999-2006 UCL


 Search by Google