I, entitled
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)
Second, the source code
1.Rectangle class
/ ** rectangle class, a class with a long, wide two member variables and methods to find the circumference * / Package COM; public class the Rectangle { Double A; Double B; Double C () { return 2 * A + 2 * B ; } }
2.Square class
/ ** subclass of class rectangular - square classes, methods defined in the class seeking area, find the circumference rewriting process * / Package COM; public class Square the extends the Rectangle { Double A; Double B; Double C () { return *. 4 A; } Double Area () { return A * A; } }
3.Test class
/ * Main class, the input side length of a square, a square object is created, the square area and perimeter requirements and output * / Package COM; Import java.util.Scanner; public class the Test { / ** * @param args * / public static void main (String [] args) { // the TODO Auto-Generated Stub Method System.out.println ( "enter a square side length:" ); Scanner SC = new new Scanner (the System.in); square sq. = new new Square (); sq.a = sc.nextDouble (); System.out.println ("Square area" + sq.area ()); System.out.println ( "square circumference" + sq.C ()); } }
Third, the operating results