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)
Code
/ ** * There are three categories, three methods * Rect rectangle is based, variable three members are a, b, sum representing the length, width, perimeter, a method GetSum () find the circumference rectangular * Square is square type, two member variables, to calculate the perimeter of a method override, a new definition of the method for calculating the area; * method, is the main class, area and perimeter squares method calls calculates and outputs; * / Package U; Import Java. * util. ; class Rect { public Double A; public Double B; public Double SUM; public Double GetSum () { SUM = (A + B) * 2 ; return SUM; } } class Square the extends Rect { // inherited public Double the getArea () { return A * A; } public Double GetSum () { // perimeter overridden methods return A *. 4 ; } } public class Method, { public static void main (String [] args) { System.out.println ( "Please enter the side length of the square:" ); Scanner Reader = new new Scanner (the System.in); square SC = new new square (); sc.a = reader.nextDouble (); System.out.println ( "area of the square is:" + sc.getSum ()); // call to the subclass method System.out.println ( "square perimeter is:" + sc.getArea ()); } }
Operation result