0% found this document useful (0 votes)
90 views4 pages

Java Polymorphism and Inheritance Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views4 pages

Java Polymorphism and Inheritance Exercises

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Test

Name: Section: ID:

Exercise 1: (2 marks)

For each of the following question choose the right alternative:


1- What concepts come under Polymorphism in java?

(Method overloading- Constructor overloading - Method overriding - All the previous)

2- If class B is subclassed from class A then which is the correct syntax


(class B:A{} - class B extends A{} - class B extends class A{}- class B implements A{})
3- It can be compiled-time (overload) as well as run-time (overriding).

(Polymorphism – Encapsulation – Inheritance)

4- A new class is created that has the features from the already existing class.

(Polymorphism – Encapsulation – Inheritance)

Exercise 2 - Shape abstract class (8 marks)

Based on inheritance and override methods concept, write a Java code that represents the
following class:

A- Create an abstract class Shape which contains:


1- Default Constructor
2- Abstract method getArea() .
3- Abstract method getPerimeter() .
4- toString() method which returns “ The shape is”

B- Create a class Rectangle which inherits from Shape class. It contains:


1- Two attributes length(double) and Width(double)
2- Default Constructor
3- Parameterized constructor which initialize the value.
4- Method getArea() returning the area of a rectangle (area = Length * width).
5- Method getPerimeter() returning the perimeter of a rectangle (Perimeter =
2*(Length + width))
6- toString() method which return the name of the shape as in the super class and the
area and the perimeter of the Rectangle.

C- Create a class Triangle which inherits from Shape class. It contains:


1- Two attributes side(double) and height(double)
2- Default Constructor
3- Parameterized constructor which initialize the value.
4- Method getArea() returning the area of a triangle(area =0.5 * side * height).
5- Method getPerimeter() returning the Perimeter of a triangle(Perimeter = 3* side).
6- toString() method which return the name of the shape as in the super class and the
area and the perimeter of the triangle.

D- Create a TestShape class that has the main method:


1- Create an rectangle object belongs to the shape class Where the rectangle has ( length
= 6.5 and width =5.2).
2- Create an equilateral Triangle object belongs to the shape class where the triangle has
side length= 3 cm and height = 2.5
3- Print their information with their area and perimeter after applying toString method.

Solution:

package Ex2;
public abstract class Shape {
public Shape(){
}
public abstract double getArea();
public abstract double getPerimeter();
public String toString(){

return " The shape is ";

}
}
package Ex2;
public class Rectangle extends Shape {
double length, width;
public Rectangle(){
}
public Rectangle(double len, double br){
length = len;
width = br;
}
public double getArea(){
return length*width;
}
public double getperimeter(){
return 2*(length+width);
}
public String toString(){
return [Link]()+" Rectangle and its length = "+length+" and its width = "+width+” its
area = “+getArea()+” and its pemieter = “+getperimeter();
}
}
package Ex2;
public class Triangle extends Shape {
double side, height;
public Triangle(){
}
public Triangle(double b, double h){
side = b;
height = h;
}
public double getArea(){
return 0.5*side*height;
}
public double getpremieter(){
return 3*side;
}
public String toString(){
return [Link]()+" Triangle and its side = "+side+" and its height = "+height+”
its area = “+getArea()+” and its pemieter = “ +getperimeter();
}
}
package Ex2;
public class TestShape {
public static void main(String[] args) {
//1-Create the two objects
Shape R = new Rectangle (6.5, 5.2);
Shape T = new Triangle (3, 2.5);
//Print the information of each object along with their area and Premiter
[Link]("the first shape is "+[Link]);
[Link]("the second shape is "+[Link]);
}
}

You might also like