Title: writing an application, create a rectangle class, a class with a long, wide two member variables and methods find the circumference. Then create a subclass of the class of rectangular - square classes, methods defined in the class seeking area, find the circumference rewriting method. In the main class, the input side length of a square, a square object is created, the square area and perimeter requirements. (Note: all classes are in a package)
cn\edu\ccut\java\Rectangle.java
Package cn.edu.ccut.java; / * * class encapsulates the Rectangle matrix length, width, area, perimeter and member variables parameterized constructor, find the area, find the circumference of an example method * / public class the Rectangle { Double length ; // long rectangle Double weight; // width of the rectangle public the rectangle ( Double length, Double weight) { the this .length = length; the this .Weight = weight; } / * * a rectangular perimeter request * / public Double the getPerimeter () { return 2 * (length + weight); } }
cn\edu\ccut\java\Square.java
Package cn.edu.ccut.java; / * * class inherits a square rectangle class, defines a square area demand method, a method of rewriting request square perimeter. * / Public class Square the extends the Rectangle { / * * constructor * / public Square ( Double length) { Super (length, length); } / * * square area demand * / public Double the getArea () { return the this .length * the this .Weight; } / * * a square perimeter request * / public Double the getPerimeter () { return 2*(length+weight); } }
cn\edu\ccut\java\MainTest_Square.java
Package cn.edu.ccut.java; Import java.util.Scanner; / * * class Square test * / public class MainTest_Square { public static void main (String [] args) { Scanner in = new new Scanner (the System.in); System.out.println ( "Please enter a square:" ); Double length = in.nextDouble (); square square = new new square (length); System.out.println ( "perimeter:" + square.getPerimeter ( ) + "area:" + square.getArea ()); } }
operation result