Title: using the interface and the interface to the callback, simple factory mode, when inputting different characters indicate corresponding pattern obtained by using the graphical object factory class, then the figure is calculated in the column bottom volume.
A source code
1、Shape.java
Package cn.edu.ccut.jia; public interface the Shape { // define the shape of the interface abstract Double the getArea (); }
2、Rectangle.java
Package cn.edu.ccut.jia; // define a rectangle class, creating a long, wide two member variables defined mensuration method public class the Rectangle the implements the Shape { Double length; Double width; public the Rectangle ( Double length, Double width) { the this .length = length; the this .width = width; } public Double the getArea () { return length * width; } }
3, Zheng.java
Package cn.edu.ccut.jia; // define a class square, rectangular and inherited class public class Zheng the extends the Rectangle { Zheng ( Double Side) { Super (Side, Side); // Super call the parent class method } public Double the getArea () { // area method override return length * length; } }
4, Yuan.java
Package cn.edu.ccut.jia; // define a circle class, create a member variable r, defined mensuration method public class Yuan the implements the Shape { Double R & lt; Double the PI = 3.14 ; public Yuan ( Double R & lt) { the this .r = R & lt; } public Double the getArea () { return R & lt * R & lt * the PI; } }
5, Tringle.java
Package cn.edu.ccut.jia; // define a triangle class, create three member variables w, x, y, defined mensuration method public class Tringle the implements the Shape { Double W; Double X; Double Y; public Tringle ( Double W, Double X, Double Y) { the this .W = W; the this .x = X; the this .y = Y; } public Double the getArea () { Double Q = (W + X + Y) / 2 ; return the Math. sqrt (q * (qw) * (qx) * (q-Y)); } }
6, Tixing.java
Package cn.edu.ccut.jia; // define a trapezoidal class, create three member variables a, b, h, define mensuration method public class Tixing the implements the Shape { Double A; Double B; Double H; public Tixing ( Double A, Double B, Double H) { the this II.A = A; the this .B = B; the this .h = H; } public Double the getArea () { return (A + B) * H / 2 ; } }
7、Cone.java
Package cn.edu.ccut.jia; // define a cartridge class, and create a high interface object member variable, to calculate the volume defined method public class Cone { Double high; the Shape Shape; public Cone (the Shape Shape, Double high) { the this .shape = Shape; the this .high = High; } Double getv () { return High * shape.getArea (); } }
8、Foctory.java
Package cn.edu.ccut.jia; // create a factory class Import Classes in java.util *. ; public class Foctory { Scanner Reader = new new Scanner (the System.in); the Shape getShape ( char D) { the Shape Shape = null ; Switch ( D) { Case 'R & lt' : System.out.println ( "enter a high squareness length, width and pillar" ); Shape = new new the rectangle (reader.nextDouble (), reader.nextDouble ()); BREAK ; Case 'Z' : System.out.println ( "Please enter the side of the square pillar and high" ); Shape = new new Zheng (reader.nextDouble ()); BREAK ; Case 'Y' : System.out.println ( "Please enter the circle and high cylinder radius " ); Shape = new new Yuan (reader.nextDouble ()); BREAK ; Case 'S' : System.out.println ( " enter a high and long sides of the triangle pillar " ); Shape = new new Tringle (reader.nextDouble (), reader.nextDouble (), reader.nextDouble ()); BREAK ; Case 'T' : System.out.println ( "Please enter the trapezoid on the bottom, a lower base, and the post high high" ); Shape = new new Tixing (reader.nextDouble (), reader.nextDouble (), reader.nextDouble ( )); BREAK ; } return Shape; } }
9、Test.java
Package cn.edu.ccut.jia; Import cn.edu.ccut.jia *. ; Import Classes in java.util *. ; public class the Test { public static void main (String [] args) { the while ( to true ) { Scanner Reader = new new Scanner (the System.in); System.out.println ( "Please enter your choice shape: rectangular r, square z, triangular s, round y, trapezoidal T" ); char D = reader.next () the charAt. (0 ); Foctory F = new new Foctory (); Cone C = new new Cone(f.getShape(d),reader.nextDouble()); System.out.println(c.getV()); } } }
Second, run shot