Sunteți pe pagina 1din 10

COMP 249 - Tutorial #4 Abstract Classes & Exceptions

Question 1- What is the result of this program?


public class Inherit { abstract class Speaker { abstract public void speak(); } class Cat extends Speaker { public void speak() { System.out.println("Meow!"); } } class Dog extends Speaker { public void speak() { System.out.println("Woof!"); } } public Inherit() { Speaker d = new Dog(); Speaker c = new Cat(); d.speak(); c.speak(); } public static void main(String[] args) { new Inherit(); } } a.

Meow! b.Woof!
Woof! Meow!

c.Woof! Woof!

d. Meow! Meow!

Question 2 In this question, we create a base class MyShape to provide a method to return the area of 2-D shapes represented by class MyRectangle and class MyCircle. Area of Rectangle = height*width; Area of Circle = 3.14*radius*radius; 2.1. Define the classes MyRectangle and MyCircle to correspond to the classes MyShape and Test below and the expected result provided below.
abstract class MyShape { abstract double getArea(); } public class Test { public static void main(String[] args) { MyCircle c1= new MyCircle(); c1.setRadius(2.0); System.out.println(c1);

MyRectangle r1= new MyRectangle(); r1.setHeight(2.0); r1.setWidth(3.0); System.out.println(r1); } }

Expected result:
Area of this Circle: 12.56 Area of this Rectangle: 6.0

2.2. Rewrite the class MyShape without using abstract class. This new class MyShape should be still match classes Test, MyRectangle and MyCircle and the expected result. 2.3. The capability to reference instances of MyRectangle and MyCircle as MyShape types brings the advantage of treating a set of different types of shapes as one common type. Define a method TotalArea in the class Test in order to get the result as below.
public class Test { //Define method TotalArea here public static void main(String[] args) { MyCircle c1= new MyCircle(); c1.setRadius(2.0); System.out.println(c1); MyCircle c2= new MyCircle(); c2.setRadius(3.0); System.out.println(c2); MyRectangle r1= new MyRectangle(); r1.setHeight(2.0); r1.setWidth(3.0); System.out.println(r1); MyShape shapes[]={c1,c2,r1}; // We are using the TotalArea method here System.out.println("Total Area is: " + TotalArea(shapes)); } }

Expected result:
Area of this Circle: 12.56 Area of this Circle: 28.26 Area of this Rectangle: 6.0 Total Area is: 46.82

Question 3: What is the output of the following program, if the main calls "aMethod();"? what is main calls "bMethod();"?
class AException extends Exception{ public AException(String msg){ super(msg); } } class BException extends Exception{ public BException(String msg){ super(msg); } } public class Except { static void aMethod() throws AException, BException { throw(new BException("Failed again!")); } static void bMethod() throws AException, BException { try { aMethod(); } catch (BException be) { } System.out.println("I made it"); } public static void main(String[] args) throws BException{ try{ //bMethod(); aMethod(); } catch (AException ae) { System.out.println(ae.getMessage()); } finally{ System.out.println("In finally"); } System.out.println("After finally"); } }

Question 4: Consider the following program:


public class Test { public static String l; /** * @param args */ public static void main(String[] args) {

} public static void Test1() { int[] myArray = new int[5]; for(int i = 0; i <= myArray.length; i++) { myArray[i] = i; } } public static void Test2() { double i = 1/0; i += i; } public static void Test3() { System.out.println(l.length()); } public static void Test4() { Object o = new Integer(42); String l = (String)o; System.out.println(l.length()); } public static void Test5() { System.out.println(Integer.parseInt("12o")); } public static void Test6() { System.out.println(Math.log(-0.123)); } }

Given the above program, if the main method were run 6 times, each time using a different Test, which tests would produce which RuntimeExceptions? Question 5: Consider the following class.
class Base { protected void m(int i) { // some code } } public class Child extends Base { // Method Here }

For each of the following methods, indicate if it can or cannot be legally inserted in place of the comment //Method Here. If a method cannot be inserted, briefly explain why not.

void m(int i) throws Exception {} void m(char c) throws Exception {} public void m(int i) {} protected void m(int i) throws Exception {}

Question 6:

Write a method called safeDivide that takes 2 integers num and denum, checks for a possible division by zero and throws a DivisionByZero exception if it is the case. Use your method safeDivide to divide 2 integers entered by the user entered on the screen. If there is a 1st attempt at dividing by zero, we give the user a 2nd chance. After the 2nd chance, we quit the program.

Question 7:
suppose that there is no compilation error in the following definitions, answer sub-questions a, b, c and d.
public class Animal { int age; private String color; public Animal(){ age = 0; color = "white"; } public Animal(int age, String color){ this.age = age; this.color = color; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } void setColor(String color) { this.color = color; } public String toString(){ return "Age="+age+" Color is "+ color; } } class Cat extends Animal{ String name; public Cat(int age, String color, String name){ super(age,color); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Dog extends Animal{ String type; public Dog(int age, String color, String type){ super(age,color); this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type;

} }

What is the best representation of the Animal, Cat and Dog classes in the UML class diagram? 1.

2.

3.

4.

Question 8: If the method definition is associated with its invocation when the code is compiled, that called: a) early binding b) late binding c) dynamic Binding d) none of the above

Question 9: Question to analysis and represent using UML notation (Class Diagram) Design a class called Human that represents human beings of this world. A human being is defined by the following information: Name (string) Address (string) Date of birth (of type Date) Provide the following functions: A constructor that initializes the data members. Accessor methods that return the value of the data members. Mutator methods that change the address, and date of birth of a Human being.
9

A toString method that prints information about a human. Design a class called Passenger that represents passengers of an airline company. A passenger is defined by the following information: Passenger ID (int) Name (string) Address (string) Tel (string) Date of birth (of type Date) Provide the following functions: A constructor that initializes the data members. Accessor methods that return the value of the data members. Mutator methods that change the address, tel., and date of birth of a passenger. A toString method that prints information about a passenger. Design a class Flight that represents information about a particular flight. A flight is defined by a flight number (string), departure and arrival dates (type Date), departure and arrival times (type Time), the arrival city (string), and the list of passengers on this flight (an array whose elements are of type Passenger from). Assume that all flights have the different capacity. The class Flight should have the following member methods: A constructor that sets the flight number, departure date and time, arrival date and time. A function that returns the duration of the flight in hours. A function that adds a passenger to the list of passengers. Recall that a passenger is of type Passenger. A function that removes a passenger from the list of passengers. The function takes passenger ID as input. A function that returns true if a given person is on the passengers list, false otherwise. A function that prints the list of passengers. A function that returns the number of seats available. In this question, we encourage you to use the program analysis steps explained in a UML class diagram, and which consist of identifying and formulating the problem, thinking of which classes you should use, analyzing the problem and show class interactions. You need to briefly document the problem analysis steps.

10

S-ar putea să vă placă și