Breaking News
recent

Please Help Community by G+

COP 2800 (Java Programming I) Homework Full Solution

Object Oriented Concepts:

(Inheritance, Polymorphism, Abstract classes, Interfaces, Exceptions and some other concepts)Answer the following questions as briefly (but completely) as possible:

1- What is the printout of running the class C (via the command “java C”) in the code below (saved in C.java)? (And, is that what you expected?)

class A {
public
    A()
    {
        System.out.println("A's no-arg constructor is invoked");
    }
}

class B extends A {

}

public class C {
public
    static void main(String[] args)
    {
        B b = new B();
    }
}

2- What problem do you expect to see if compiling the program below? What was the error(s), if any, actually produced?


class A {
public
    A(int x)
    {
    }
}

class B extends A {
public
    B()
    {
    }
}

public class C {
public
    static void main(String[] args)
    {
        B b = new B();
    }
}

3- Which of the follow statements are true? Which are false? Explain why.

A subclass is a subset of a superclass.
When invoking a constructor from a subcass, its superclass's no-arg constructor is always invoked.
You can override a private method defined in a superclass.
You can override a static method defined in a superclass.

4- What is the benefit of using the @Override annotation?

5- (a) Show the output of the following program:

public
class Test {
public
    static void main(String[] args)
    {
        A a = new A(3);
    }
}

class A extends B {
public
    A(int t)
    {
        System.out.println("A's constructor is invoked");
    }
}

class B {
public
    B()
    {
        System.out.println("B's constructor is invoked");
    }
}

5- (b). Is the no-arg constructor of Object invoked when new A(3) is invoked?

6- Indicate true or false for the follow statements:


  • You can always successfully cast an instance of a subclass to a superclass.
  • You can always successfully cast an instance of a superclass to a subclass.

7- What's wrong with the following code?

public
class Test {
public
    static void main(String[] args)
    {
        Object fruit = new Fruit();
        Object apple = (Apple)fruit;
    }
}

class Apple extends Fruit {
}

class Fruit {
}

8- When overriding the equals method, a common mistake is mistyping its signature in the subclass. 

For example, the equals method is incorrectly written as equals(Circle circle) as shown in (a) below. It should be written as equals(Object circle), as shown in (b) below. Show the output of running class Test using the Circle class first from (a), and then from (b). Explain the output.(Next, try adding the “@Override” annotation to the equals method in (a), and then try compiling. Repeat with (b). Are the results as expected?)

Part (a)

class Circle {
    double radius;
public
    boolean equals(Circle circle)
    {
        return this.radius == circle.radius;
    }
}

Part (b)

class Circle {
    double radius;
public
    boolean equals(Object circle)
    {
        return this.radius == ((Circle)circle).radius;
    }
} public class Test {
public
    static void main(String[] args)
    {
        Object circle1 = new Circle();
        Object circle2 = new Circle();
        System.out.println(circle1.equals(circle2));
    }
}

9- How would you prevent a class from being extended? How would you prevent a method from being overridden?

10- Which of the following classes define a legal abstract class?

Part (a)

class A {
abstract void unfinished ( ) {
}
}

Part (b)

public class abstract A {
abstract void unfinished ( ) {
}
}

Part (c)

class A {
abstract void unfinished ( ) ;
}

Part (d)

abstract class A {
protected void unfinished ( ) ;
}

Part (e)

abstract class A {
abstract void unfinished ( ) ;
}

Part (f)

class A {
abstract int unfinished ( ) ;
}

11. Suppose A is an interface. Can you create an instance using “new A()”?

12. Which of the following (if any) is a correct interface? (Assume I1 and I2 are correctly defined elsewhere.)

Part (a)

interface A {
void print () {
};
}

Part (b)

abstract interface A extends I1, I2 {
abstract void print () {
};
}

Part (c)

abstract interface A {
print () ;
}

Part (d)

interface A {
void print () ;
}


a

Buy now
HJ Soft

HJ Soft

No comments:

Post a Comment

Powered by Blogger.