*********************************** # Example illustrating inheritance and abstract classes. * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. * Curve.java An (abstract) curved Shape (open or closed). * StraightEdgedShape.java A Shape with straight edges (open or closed). * Measurable.java Interface defining classes with measurable areas. * Circle.java A circle that extends Shape and implements Measurable. * MeasureUtil.java Operates on Measurable instances. * Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable. * Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon. * MeasureTest.java Driver for example. ************************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Shape.java ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** The parent class for all closed, open, curved, and * straight-edged shapes. * ############################ public abstract class Shape { protected int x, y; 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; } } ############################# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Curve.java An (abstract) curved Shape (open or closed) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** A curved shape (open or closed). Subclasses will include * arcs and circles. * *********************** public abstract class Curve extends Shape {} ############################## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ StraightEdgedShape.java A Shape with straight edges (open or closed). ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** A Shape with straight edges (open or closed). Subclasses * will include Line, LineSegment, LinkedLineSegments, * and Polygon. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public abstract class StraightEdgedShape extends Shape {} ################################ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Measurable.java Interface defining classes with measurable areas ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** Used in classes with measurable areas. * ************** public interface Measurable { double getArea(); } ################################# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Circle.java A circle that extends Shape and implements Measurable. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** A circle. Since you can calculate the area of * circles, class implements the Measurable interface. * *********************************** public class Circle extends Curve implements Measurable { private double radius; public Circle(int x, int y, double radius) { setX(x); setY(y); setRadius(radius); } public double getRadius() { return(radius); } public void setRadius(double radius) { this.radius = radius; } /** Required for Measurable interface. */ public double getArea() { return(Math.PI * radius * radius); } } ############################ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MeasureUtil.java Operates on Measurable instances ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** Some operations on Measurable instances. * ************************ public class MeasureUtil { public static double maxArea(Measurable m1, Measurable m2) { return(Math.max(m1.getArea(), m2.getArea())); } public static double totalArea(Measurable[] mArray) { double total = 0; for(int i=0; i<marray .length; i++) { total = total + mArray[i].getArea(); } return(total); } } ######################### ~~~~~~~~~~~~~~~~~~~~~~~~~ Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable. ~~~~~~~~~~~~~~~~~~~~~~~~~ /** A closed Shape with straight edges. * *************************************** public abstract class Polygon extends StraightEdgedShape implements Measurable { private int numSides; public int getNumSides() { return(numSides); } protected void setNumSides(int numSides) { this.numSides = numSides; } } ########################### ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon. ~~~~~~~~~~~~~~~~~~~~~~~~~~~ /** A rectangle implements the getArea method. This satisfies * the Measurable interface, so rectangles can be instantiated. * ***************************** public class Rectangle extends Polygon { private double width, height; public Rectangle(int x, int y, double width, double height) { setNumSides(2); setX(x); setY(y); setWidth(width); setHeight(height); } public double getWidth() { return(width); } public void setWidth(double width) { this.width = width; } public double getHeight() { return(height); } public void setHeight(double height) { this.height = height; } /** Required to implement Measurable interface. */ public double getArea() { return(width * height); } } ########################## ~~~~~~~~~~~~~~~~~~~~~~~~~~ MeasureTest.java Driver for example. ~~~~~~~~~~~~~~~~~~~~~~~~~~ /** Test of MeasureUtil. Note that we could change the * actual classes of elements in the measurables array (as * long as they implemented Measurable) without changing * the rest of the code. ************************ public class MeasureTest { public static void main(String[] args) { Measurable[] measurables = { new Rectangle(0, 0, 5.0, 10.0), new Rectangle(0, 0, 4.0, 9.0), new Circle(0, 0, 4.0), new Circle(0, 0, 5.0) }; System.out.print("Areas:"); for(int i=0; i<measurables.length; i++) System.out.print(" " + measurables[i].getArea()); System.out.println(); System.out.println("Larger of 1st, 3rd: " + MeasureUtil.maxArea(measurables[0], measurables[2]) + "nTotal area: " + MeasureUtil.totalArea(measurables)); } } @@@@@@@@@@@@@@@@@@@@@@@@ ~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10394
Categories:Programming Code Examples, Java/J2EE/J2ME, Object Oriented Programming
Tags:Java/J2EE/J2MEObject Oriented Programming
Post Data:2017-01-02 16:04:39
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada