Java Abstract Classes and Methods Outcome: Show that you are able to: demonstrat
ID: 3905837 • Letter: J
Question
Java Abstract Classes and Methods
Outcome:
Show that you are able to:
demonstrate the ability to create abstract classes
demonstrate the ability to create abstract methods
demonstrate the ability to understand that an abstract class can have both concrete and abstract methods
understand that abstract classes cannot be instantiated
understand that you cannot have an object of type abstract class
understand that the purpose of an abstract class is to impose rules on all child classes.
You are to make up your own theme for this assignment.
You are to make up your own classes for this assignment.
You are to create an Abstract Class with Abstract Methods, a class that uses the abstract methods and a test class
You are to create all necessary setters, getters, constructors and methods.
You are to create a reasonable test case
YOU CANNOT:
Use global variables, word goto, the break command outside a case statement
Explanation / Answer
In this solution, I have created an abstract class Shape which represents a 2D shape on a 2D plane. It has attributes to store x,y coordinates with non- abstract methods to manipulate them. It has two abstract methods to find the area and perimeter of the shape. Created two child classes Rectangle and Circle and implemented the two abstract methods along with other methods. Created a Test class to test the working of the above classes. Comments are included. Go through them, understand how things work and drop a comment if you have any doubts. Thanks.
// Shape.java
/**
* Abstract super class Shape represents blueprint of a 2D shape in a 2D plane
*
*/
public abstract class Shape {
// x, y coordinates of the shape
private int x;
private int y;
/**
* constructor to initialize values for x and y, all super classes must pass
* values for x and y at the time of instantiation
*/
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
/**
* getters and setters of the x, y coordinates (non abstract methods)
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
/**
* returns the position as (x,y) pair
*/
public String toString() {
return "position: (" + x + ", " + y + ")";
}
/**
* two abstract methods of the class, no definition (it will be done by child classes)
*/
//abstract method to return the area of shape
public abstract double getArea();
//abstract method to return the perimeter of shape
public abstract double getPerimeter();
}
// Rectangle.java
/**
* Rectangle is a sub class of Shape, represents a rectangle with length and
* breadth lying on a 2D plane
*
*/
public class Rectangle extends Shape {
//additional attributes for length and breadth
private double length;
private double breadth;
// constructor to initialize all fields
public Rectangle(int x, int y, double length, double breadth) {
//passing x,y coordinate values to the super class constructor
super(x, y);
this.length = length;
this.breadth = breadth;
}
/**
* definitions for the two abstract methods
*/
@Override
public double getArea() {
//finding and returning the area of rectangle
double area = length * breadth;
return area;
}
@Override
public double getPerimeter() {
//finding and returning the perimeter of rectangle
double perimeter = 2 * (length + breadth);
return perimeter;
}
//getters and setters for length, breadth attributes
public double getLength() {
return length;
}
public double getBreadth() {
return breadth;
}
public void setLength(double length) {
this.length = length;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public String toString() {
/**
* appending additional info to the String returned by super class toString method
*/
return super.toString() + ", length: " + length + ", breadth: "
+ breadth;
}
}
// Circle.java
/**
* Circle is a sub class of Shape, represents a circle with a specific radius
* lying on a 2D plane
*/
public class Circle extends Shape {
// additional attribute of radius
private double radius;
// constructor to initialize all fields
public Circle(int x, int y, double radius) {
// passing x,y coordinate values to the super class constructor
super(x, y);
this.radius = radius;
}
/**
* definitions for the two abstract methods
*/
@Override
public double getArea() {
// finding and returning the area of circle
double area = Math.PI * radius * radius;
return area;
}
@Override
public double getPerimeter() {
// finding and returning the perimeter (circumference) of circle
double circumference = 2 * Math.PI * radius;
return circumference;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String toString() {
/**
* appending additional info to the String returned by super class
* toString method
*/
return super.toString() + ", radius: " + radius;
}
}
// Test.java
public class Test {
public static void main(String[] args) {
/**
* creating a Rectangle object lying on (100,100) position with length
* 25 and breadth 75.5
*/
Shape rectangle = new Rectangle(100, 100, 25, 75.5);
/**
* creating a Circle object lying on (0,0) position with radius of 12.5
*/
Shape circle = new Circle(0, 0, 12.5);
/**
* The below line will cause error as abstract class cannot be
* instantiated, uncomment it to check
*/
// Shape shape = new Shape(0, 0);
// displaying the rectangle shape details
System.out.println(rectangle);
// invoking the two abstract methods with rectangle object
System.out.println("Area: " + rectangle.getArea());
System.out.println("Perimeter: " + rectangle.getPerimeter());
// displaying the circle shape details
System.out.println(circle);
// invoking the two abstract methods with circle object
System.out.println("Area: " + circle.getArea());
System.out.println("Perimeter: " + circle.getPerimeter());
}
}
/*OUTPUT*/
position: (100, 100), length: 25.0, breadth: 75.5
Area: 1887.5
Perimeter: 201.0
position: (0, 0), radius: 12.5
Area: 490.8738521234052
Perimeter: 78.53981633974483
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.