Write a Java application as required:
(1) write a rectangle class rectthat contains:
Two protected Properties: Wide width of rectangle ,height height of rectangle .
Two methods of construction:
1. A construction method with two parameters to initialize the width and height properties;
2. A construction method without parameters that initializes the rectangle to a width and height of ten.
Two methods:
method for finding rectangular areas Area ()
Method for finding the perimeter of a rectangle perimeter ()
(2) by inheriting the rect class, write a rectangular class plainrect with a definite position , which is determined by
The coordinates of the upper-left corner of the rectangle are identified, including:
Add two properties: the upper-left corner of the rectangle coordinates StartX and starty.
Two methods of construction:
A construction method with 4 parameters for StartX,starty,width , and Height Property
Initialization
Constructs without parameters, initializes the rectangle to the upper-left coordinate, the length and width are 0
the rectangle;
Add a method:
A method isinside that determines whether a point is inside a rectangle (double x,double y). As in the moment
Inside the shape, returns true, Otherwise, returns false.
Tip: A point in a rectangular class means satisfying a condition:
x>=startx&&x<= (startx+width) &&y<starty&&y>= (startY-height)
(3) writing test Procedures for Plainrect class
Create a rectangular object with the upper-left coordinate (ten,ten), and awidth of ten ;
Calculates and prints the area and perimeter of the output rectangle;
Determines whether the point (25.5) is inside the rectangle and prints out the relevant information.
Public classRect {protected Doublewidth; protected Doubleheight; Rect (DoubleWidthDoubleheight) { This. height=height; This. width=width; } Rect () {width=0; Height=0; } Public voidArea () {DoubleA; A=width*height; System.out.println ("The area of the rectangle is" +a); } Public voidperimeter () {DoubleA; A=width*2+height*2; System.out.println ("The perimeter of the rectangle is" +a); }
Public classPlainrectextendsRect {Private DoubleStartX; Private DoubleStarty; Public DoubleGetstartx () {returnStartX; } Public voidSetstartx (DoubleStartX) { This. StartX =StartX; } Public DoubleGetstarty () {returnStarty; } Public voidSetstarty (Doublestarty) { This. Starty =Starty; } plainrect (DoubleWidthDoubleHeightDoubleStartX,Doublestarty) { This. width=width; This. Height =height; This. startx=StartX; This. starty=Starty; } plainrect () {width=0; Height=0; StartX=0; Starty=0; } Public BooleanIsinside (DoubleXDoubley) {if(x>=startx&&x<= (Startx+width) &&y<starty&&y>= (startY-height)) { return true; } Else { return false; } }
Public class Ceshiplainrect { publicstaticvoid main (string[] args) { Plainrect p=new plainrect (10,20,10,10); P.area (); P.perimeter (); System.out.println (P.isinside (25.5));} }
Java Inheritance exercises 8