0% found this document useful (0 votes)
25 views7 pages

Lab Task: Lahore Campus

The document defines an abstract Shape class with subclasses Rectangle and Square. Shape has abstract methods getArea() and getPerimeter() that subclasses must implement to calculate the area and perimeter. Rectangle implements these methods based on its width and length. Square extends Rectangle but sets both dimensions to the side length. The main method creates Shape objects of each type and prints their attributes.

Uploaded by

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

Lab Task: Lahore Campus

The document defines an abstract Shape class with subclasses Rectangle and Square. Shape has abstract methods getArea() and getPerimeter() that subclasses must implement to calculate the area and perimeter. Rectangle implements these methods based on its width and length. Square extends Rectangle but sets both dimensions to the side length. The main method creates Shape objects of each type and prints their attributes.

Uploaded by

Mohammad Umair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Task

Abstract

Lahore Campus

Name: Mohammad Umair

Reg. No: FA19-BCS-139

Section: C

Course: OOP

Instructor: Sir Mughees Ahmed

Observation
Q#1 Remove the implementation of getArea() from the Rectangle class. Is there any
error? Why?
Ans: Yes, Error in Rectangle class because it is inheriting from Shape class which is
abstract, and it is must to define the implementation of abstract method in child class.

Q#2 Remove the abstract keyword with Shape class. Is there any error? Why?

Ans: Yes, there are two methods implemented abstract in Shape class which requires a
class to be defined abstract.

Q#3 Try to create an object of class Shape. Is there any error? Why?

Ans: It is not possible to create an object in Shape class because it holds the general
concept not the proper implementation for the objects.

Shape:

public abstract class Shape {

private String color;


private boolean filled;

public String getColor() {


return color;
}

public void setColor(String


color) { this.color =
color;

public boolean isFilled() {


return filled;
}

public void setFilled(boolean


filled) { this.filled =
filled;
}

public Shape() {
setColor("Green");
setFilled(true);

public Shape(String color, boolean filled) {


setColor(color);
setFilled(filled);
}

@Override
public String toString() {
return " A " + isFilled() + " Shape with " + getColor() + "
color."; }

public abstract double getArea();

public abstract double getPerimeter();

Rectangle:
public class Rectangle extends Shape {
private double width;
private double length;

public Rectangle() {
setWidth(1.0);
setLength(1.0);
}

public Rectangle(double width, double length) {


setWidth(width);
setLength(length);
}
public Rectangle(String color, boolean filled, double width, double
length) { super(color, filled);

setWidth(width);
setLength(length);
}

public double getArea() {


return getLength() * getWidth();
}

public double getPerimeter() {


return 2 * getLength() + 2 * getWidth();
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}

public double getLength() {


return length;
}

public void setLength(double length) {


this.length = length;
}

@Override
public String toString() {
return " A Rectangle with width = " + getWidth() + " and length = "
+ getLe ngth() + " which is a subclass of "
+ super.toString();
}

Square:
public class Square extends Rectangle {

public Square() {
}

public Square(double side) {


super(side, side);
}

public Square(String color, boolean filled, double side) {


super(color, filled, side, side);
}

@Override
public void setWidth(double side) {

super.setWidth(side);
super.setLength(side);
}

@Override
public void setLength(double side) {

super.setWidth(side);
super.setLength(side);
}
@Override
public String toString() {
return "A square with side = " + getLength() + " which is a
subclass of " + super.toString();
}
}

ShapeTest:
import java.util.ArrayList;

public class Test {


public static void main(String[] args) {

ArrayList<Shape> shapes = new ArrayList<>();


shapes.add(new Rectangle());
shapes.add(new Rectangle(2.0, 3.0));
shapes.add(new Rectangle("red", false, 4.0, 5.0));

shapes.add(new Square());
shapes.add(new Square(3.0));
shapes.add(new Square("yellow", true, 5.0));

for (Shape shape : shapes) {


System.out.println("\n" + shape.toString());
System.out.println("\nArea = " + shape.getArea());
System.out.println("\nPerimeter = " +
shape.getPerimeter()); }

}
}

Output

You might also like