News Ticker

Menu

COP 2800 (Java Programming I) Homework Full Solution


Homework 2: (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.


  1. A subclass is a subset of a superclass.
  2. When invoking a constructor from a subcass, its superclass's no-arg constructor is always invoked.
  3. You can override a private method defined in a superclass.
  4. 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" );
    }
 }

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

6. Indicate true or false for the follow statements:

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

      b) 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?)

a)

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

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?

a)

class A {
    abstract void unfinished ( ) {
    }
}

b)

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

c)

class A {
  abstract void unfinished ( ) ;
}

d)

abstract class A {
  protected void unfinished ( ) ;
}

e)

abstract class A {
   abstract void unfinished ( ) ;
}

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.)

a)

interface A {
   void print () {
   };
}

b)

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

c)

abstract interface A {
  print () ;
}

d)

interface A {
  void print () ;
}


Buy now

Share This:

Post Tags:

Hassnain

I'm Hassnain. A full time Java Developer. I enjoy to make modern projects. I love to create JavaFX, Java Swing GUI and write about Java Programming, OOP. Now I'm working with CS2IT. You can buy our projects from here.

No Comment to " COP 2800 (Java Programming I) Homework Full Solution "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM