topic:
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:
TestA.java:
factorial Package Penalty for; / * * * * @ClassName: TestA * @author Dandelion * @date: 2019 Nian 9 24 afternoon 7:15:32 * TODO: Create a parent and child classes, parent class is rectangular, subclass square, subclass inherits * TODO: find the circumference methods. * / Public class TestA { public int length; public int width; public int Perimeter; public int the getPerimeter () { Perimeter = (length + width) * 2 ; return Perimeter; } } class TestB, the extends TestA { public int getArea() { return length * length; } public int getPerimeter() { return length * 4; } }
Testc.java:
factorial Package Penalty for; Import Classes in java.util. * ; / * * * * @ClassName: TESTC * @author Dandelion * @date: 2019 Nian 9 24 afternoon 7:18:27 * TODO test perimeter and area * / public class {TestC public static void main (String [] args) { the System. OUT .println ( " Please enter the length and width of the rectangle: " ); Scanner iNPUT = new new Scanner (. the System in ); TestA Testa = new new TestA (); testa.length =input.nextInt (); testa.width = input.nextInt (); int Perimeter = testa.getPerimeter (); the System. OUT .println ( " circumference: " + Perimeter); . the System OUT .println ( " Enter square length: " ); TestB, TestB = new new TestB, (); testb.length = input.nextInt (); int Area = testb.getArea (); int squarePerimeter = testb.getPerimeter (); the System. OUT.println ( " square perimeter and area: " + Area + " \ T " + squarePerimeter); } }
operation result: