The point and rectangle of Java object-oriented exercises inheritance

Source: Internet
Author: User

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.

package rectangle;//rect class public class Rect {protected double height;protected double width; Rect () {this.width=10.0;this.height=10.0;} Rect (double height,double width) {this.width=width;this.height=height;} Public double area () {return width*height;} Public double perimeter () {return width*2+height*2;}}

  

Package rectangle; Plainrect class public class Plainrect extends Rect {double startx;double starty; Plainrect (double startx,double starty,double width,double height) {this.startx=startx;this.starty=starty;this.width =width;this.height=height;} Plainrect () {this.startx=0;this.starty=0;this.width=0;this.height=0;} public boolean isinside (double x,double y) {if (x>=startx&&x<= (startx+width) &&y>=starty &&y<= (Starty+height)) {return true;} Else{return false;}}}

  

Package Rectangle,//test main class public, class test {public static void main (string[] args) {plainrect j1=new plainrect (10,10,20,10); System.out.println ("The area of this rectangle is" +j1.area ()); System.out.println ("The area of this rectangle is" +j1.perimeter ()); System.out.println (J1.isinside (25.5, 13));}}

  

Run results

The point and rectangle of Java object-oriented exercises inheritance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.