






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







import package1.*; public class Main { public static void main(String[] args) { // Instantiate Solids cube = new Cube( 5 ), cylinder = new Cylinder( 5 , 10 ), sphere = new Sphere( 5 ); // Interface AbstractSolids absCube = new AbstractCube( 5 ), absCylinder = new AbstractCylinder( 5 , 10 ), absSphere = new AbstractSphere( 5 ); // Abstract // Display System.out.println("\n= Using 'INTERFACE CLASSES' =\n"); System.out.println("Area: " + cube.Area()); System.out.println("Volume: " + cube.Volume()); System.out.println("Circumference: " + cube.Circumference() + "\n"); System.out.println("Area: " + cylinder.Area()); System.out.println("Volume: " + cylinder.Volume()); System.out.println("Circumference: " + cylinder.Circumference() + "\n"); System.out.println("Area: " + sphere.Area()); System.out.println("Volume: " + sphere.Volume()); System.out.println("Circumference: " + sphere.Circumference() + "\n"); // Display Abstract Concrete Methods System.out.println("= Using 'ABSTRACT CLASSES' =\n"); System.out.println(absCube.ShapeName()); System.out.println(absCube.Example()); System.out.println("Area: " + absCube.AbstractArea()); System.out.println("Volume: " + absCube.AbstractVolume()); System.out.println("Circumference: " + absCube.AbstractCircumference() + "\n"); System.out.println(absCube.ShapeName());
package package1; public class Cube implements Solids { // Variables double side; // Constructor public Cube(double side) { this.side = side; } @Override public double Area() { // TODO Auto-generated method stub return 6 * (side * side); } @Override public double Volume() { // TODO Auto-generated method stub return Math.pow(side, 3 ); } @Override public double Circumference() { // TODO Auto-generated method stub return 12 * side; } }
package package1; public class Cylinder implements Solids { // Variables double radius, height; // Constructor public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } @Override public double Area() { // TODO Auto-generated method stub return ( 2 * Math.PI * radius * height) + ( 2 * Math.PI * Math.pow(radius, 2 )); } @Override public double Volume() { // TODO Auto-generated method stub return Math.PI * (radius * radius) * height; } @Override public double Circumference() { // TODO Auto-generated method stub return Math.PI * 2 * radius; } }
package package1; public abstract class AbstractSolids { String name = null, example = null; // Commmon Method public String ShapeName() { return "This 3D-Shape is: " + name; } // Commond Method public String Example() { return "Example: " + example; } public abstract double AbstractArea(); public abstract double AbstractVolume(); public abstract double AbstractCircumference(); }
package package1; public class AbstractCube extends AbstractSolids { int side = 0 ; public AbstractCube(int side) { // TODO Auto-generated constructor stub this.side = side; name = "Cube"; example = "Rubiks Cube Toy";
@Override public double AbstractArea() { // TODO Auto-generated method stub return 6 * (side * side); } @Override public double AbstractVolume() { // TODO Auto-generated method stub return Math.pow(side, 3 ); } @Override public double AbstractCircumference() { // TODO Auto-generated method stub return 12 * side; } }
package package1; public class AbstractCylinder extends AbstractSolids { double radius, height; public AbstractCylinder(double radius, double height) { this.radius = radius; this.height = height; name = "Cylinder"; example = "Cylindrical Battery"; } @Override
return 4 * Math.PI * (radius * radius); } @Override public double AbstractVolume() { // TODO Auto-generated method stub return ( 4 * 22 * radius * radius * radius) / ( 3 * 7 ); } @Override public double AbstractCircumference() { // TODO Auto-generated method stub return Math.PI * 2 * radius; } }