Skip to main content

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

CONTACT DETAILS

For any other questions or other tasks please feel free to contact me
via email: mhassnainjamil@gmail.com
via WhatsApp: +92-324-7042178
via skype: hassnainjamil1

Comments

Popular posts from this blog

The Zoo Management System - entity relationship diagram & MS Access Database

Zoo Management System - Project Details: You are the employee of a big, worldwide working Zoo Management Company. Your company is responsible for the Zoo management. Your boss thinks it would be a great idea to store all data for each Zoo in a brand new self-developed ZOO Management System. Up to now, the ZOO management company has maps of each ZOO available. Your boss knows that you took a course in introduction on an ERP system, so he asks you if you could help designing such a system. Each ZOO must have the same organizational structure, which should look like this: Each Zoo has a Zoo-Address. Each Zoo has many visitors (Visitor Ticket Process (VTP). Many Zoo-Attractions belong to a Zoo. Module 1: Entity Relationship Diagram Design a ER (entity-relationship) diagram for your ZOO Management System. Use the information provided below with the entities and its attributes. Put the entities in the correct relationship to each other (organizational structure). Module 2: DB Implem...

EIT Knowledge and Innovative Community Scholarships has been announced

Admission Criteria To qualify for our programmes, applicants need to fulfill the admission requirements based on previous studies, English proficiency and relevant documentation. Previous Studies: A Completed Bachelor’s Degree In order to be admitted into a KIC InnoEnergy MSc programme, you must have completed a Bachelor’s degree encompassing a minimum of 180 ECTS credits or equivalent academic qualifications from an internationally recognized university. Please note that admissions depend on the specific BSc degree you hold for entry into the MSc programme you are interested in. Conditional Acceptance – Undergraduate Students in Final Year Students in their final year of undergraduate education may also apply and if expected to qualify, receive a conditional offer. If you have not completed your studies, please include a written statement from your university’s administration office (or equivalent department), confirming that you are enrolled in the final year of your study programme ...

Human Physiology by Stuart Ira Fox [PDF] (12th edition) free download