0% found this document useful (0 votes)
15 views6 pages

Shape Exercise

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)
15 views6 pages

Shape Exercise

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
You are on page 1/ 6

Exercise 2 - Shape abstract class

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

Shape (abstract)

+Shapes()

+getPerimeter() :double(abstract).

+getArea():double(abstract)

+toString():string

Rectangle Square Triangle Circle

-length: double -Side: double -side: double -radius: double

-width: double -height: double

+rectangle() +Square() +Triangle() +Circle()

+rectangle( double L, +Square (double S) +triangle (double Side, +Circle (double radius)
double W) double h)
+getArea():double +getArea():double
+getArea():double +getArea():double
+getPerimeter():double +getPerimeter():double
+getPerimeter():double +getPerimeter():double
+toString():string +toString():string
+toString():string +toString():string

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
length and width of the Rectangle.

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


1- An attribute Side (double).
2- Default Constructor
3- Parameterized constructor which initialize the value.
4- Method getArea() returning the area of a square (area = Side2).
5- Method getPerimeter() returning the perimeter of a square (Perimeter = 4*S)
6- toString() method which return the name of the shape as in the super class and the
side length of the Square.

D- 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
side length and the height of the triangle.

E- Create a class Circle which inherits from Shape class. It contains:


1- An attribute radius(double).
2- Default Constructor
3- Parameterized constructor which initialize the value.
4- Method getArea() returning the area of a circle(area =PI * radius2).
5- Method getPerimeter() returning the circumference of the circle (circle = 2* PI *
radius).
6- toString() method which return the name of the shape as in the super class and the
radius of the circle.

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


1- Create an rectangle object belongs to the shape class Where the rectangle has (
length = 4.5 and width =2).
2- Create a square object belongs to the shape class Where the square has side length
=3.5
3- Create an equilateral Triangle object belongs to the shape class where the triangle
has side length= 2.5 cm and height = 3.2.
4- Create a circle object belongs to the shape class where its radius length= 5.5 cm
5- 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 super.toString()+" Rectangle and its length = "+length+" and its width = "+width; }
}
package Ex2;
public class Square extends Shape {
double s;
public Square(){
}
public Square(double b){
s = b;}
public double getArea(){
return s*s;
}
public double getpremieter(){
return 4*s;
}
public String toString(){
return super.toString()+" Square and its side = "+s ; }
}

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 super.toString()+" Triangle and its side = "+side+" and its height = "+height;
}
}
package Ex2;
public class Circle extends Shape {
double radius;
final static double PI = 3.141592564 ;
public Circle(){
}
public Circle (double r){
radius = r;}
public double getArea(){
return radius*radius*PI;
}
public double getpremieter(){
return 2*radius*PI;
}
public String toString(){
return super.toString()+" Circle and its radius = "+radius;
}
}

package Ex2;
public class TestShape {
public static void main(String[] args) {
//1-Create the four objects
Shape R = new Rectangle (4.5, 2);
Shape S=new Square (3.5);
Shape T = new Triangle (2.5, 3.2);
Shape C= new Circle( 5.5);
//Print the information of each object along with their area and Premiter
System.out.println("the first shape is "+R.toString()+” its area = “+R.getArea()+” and its
pemieter = “ + R.getperimeter());
System.out.println("the second shape is "+S.toString()+” its area = “+S.getArea()+” and its
pemieter = “ + S.getperimeter());
System.out.println("the third shape is "+T.toString()+” its area = “+T.getArea()+” and its
pemieter = “ + T.getperimeter());
System.out.println("the last shape is "+C.toString()+” its area = “+C.getArea()+” and its pemieter
= “ + C.getperimeter());
}
}

You might also like