Skip to content

Instantly share code, notes, and snippets.

@KipchirchirIan
Last active March 15, 2019 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KipchirchirIan/14cea19c097901390e602b8aee1b9565 to your computer and use it in GitHub Desktop.
Save KipchirchirIan/14cea19c097901390e602b8aee1b9565 to your computer and use it in GitHub Desktop.
Basic OOP concepts using Shapes
package com.bitrev.shapesbase;
public class Circle extends Shapes { // Circle is a shape therefore inherits properties of a shape
private double radius;
public Circle(double radius)
{
super(); // calling the parent class constructor
this.radius = radius;
}
public double getRadius()
{
return this.radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
public void calculateArea()
{
double result = Math.PI * Math.pow(getRadius(), 2);
setArea(result);
}
public void calculatePerimeter()
{
double result = 2 * Math.PI * getRadius();
setPerimeter(result);
}
}
package com.bitrev.myapp;
import com.bitrev.shapesbase.Circle;
import com.bitrev.shapesbase.Shape;
public class Main {
public static void main(String[] args) {
// Calculate Area and Perimeter of Circle
Circle c = new Circle(5.0); // Create new Circle object and set radius from constructor
c.calculateArea();
c.calculatePerimeter();
// Realize that this method is not declared in Circle class?? That is because Circle inherits all
// properties and methods(public) from the parent class Shapes
c.displayArea();
c.displayPerimeter();
System.out.println();
c.setRadius(10); // set radius of Circle from method
c.calculateArea();
c.calculatePerimeter();
c.displayArea();
c.displayPerimeter();
// Do your own implementations of Rectangle, Triangle e.t.c here.
// You first need to create classes for those shapes and inherit the Shapes class
}
}
package com.bitrev.shapesbase;
public class Shapes { // Shapes is a parent class that encompasses all shapes...circles, rectangles, triangles e.t.c
private double area = 0.0; // from all shapes we can obtain an area
private double perimeter = 0.0; // from all shapes we can obtain a perimeter
public Shapes() {
}
public double getArea()
{
return area;
}
public void setArea(double area)
{
this.area = area;
}
public double getPerimeter()
{
return perimeter;
}
public void setPerimeter(double perimeter)
{
this.perimeter = perimeter;
}
public void displayArea() { // we can call this method for any object that's being referenced to at that time
System.out.printf("The area of the shape is: %.2f%n", getArea());
}
public void displayPerimeter() // this can be combined with displayArea() to print in one line
{
// %.2f means print to 2 decimal places. f- decimal e.g. %f, d - integer e.g. %d, s - string e.g. %s
// %n - means move cursor to a new line
System.out.printf("The perimeter of the shape is: %.2f%n", getPerimeter());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment