Created
March 15, 2019 23:57
-
-
Save KipchirchirIan/1c91c9fab71d39753dd4f2ffd7f33804 to your computer and use it in GitHub Desktop.
Java NOOP(Non-Object Oriented) Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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