Skip to content

Instantly share code, notes, and snippets.

@KipchirchirIan
Created March 15, 2019 23:57
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/1c91c9fab71d39753dd4f2ffd7f33804 to your computer and use it in GitHub Desktop.
Save KipchirchirIan/1c91c9fab71d39753dd4f2ffd7f33804 to your computer and use it in GitHub Desktop.
Java NOOP(Non-Object Oriented) Demo
public class Main {
public static void main(String[] args) {
Shapes s = new Shapes();
s.calculateCircleArea(5.0);
s.displayArea();
s.displayFormattedArea();
System.out.println(); // spacing
s.calculateRectangleArea(10, 10);
s.displayArea();
s.displayFormattedArea();
}
}
public class Shapes {
double result = 0.0; // Holds our result
public Shapes() {}
public double calculateCircleArea(double radius)
{
// Math.PI is a static property - can be accessed anywhere in your program
// without instantiating a class/creating on object of a class.
result = Math.PI * radius * radius;
return result;
}
public double calculateRectangleArea(double length, double width)
{
result = length * width;
return result;
}
// Continue with the rest of the Shapes here
// ..........................................
public void displayArea()
{
System.out.println("The area of the shape is: " + result + " square meters");
}
public void displayFormattedArea()
{
// A formatted output for readability
// %.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 area of the shape is: %.2f square meters%n ", result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment