Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.util.*;
- public class Rectangle{
- private MyPoint upperLeft;
- private MyPoint lowerRight;
- public Rectangle(){
- this.upperLeft = new MyPoint();
- this.lowerRight = new MyPoint();
- }
- public Rectangle(MyPoint upperLeft, MyPoint lowerRight){
- this.upperLeft = upperLeft;
- this.lowerRight = lowerRight;
- }
- public MyPoint getupperLeft(){
- return upperLeft;
- }
- public MyPoint getlowerRight(){
- return lowerRight;
- }
- public void setupperLeft(MyPoint upperLeft){
- this.upperLeft = upperLeft;
- }
- public void setlowerRight(MyPoint lowerRight){
- this.lowerRight = lowerRight;
- }
- public double length(){
- double x1, x2;
- x1=upperLeft.getX();
- x2=lowerRight.getX();
- return x1>x2?((x1-x2)):((x2-x1));
- }
- public double width(){
- double y1, y2;
- y1=upperLeft.getY();
- y2=lowerRight.getY();
- return y1>y2?((y1-y2)):((y2-y1));
- }
- public double area(){
- return length() * width();
- }
- public double perimeter(){
- return (length()+width())*2;
- }
- public static void main(String[] args){
- Scanner yey = new Scanner(System.in);
- Rectangle recta = new Rectangle();
- int x1, x2, y1, y2;
- System.out.println("nPoint A");
- System.out.print("nEnter coordinates x1: ");
- x1 = yey.nextInt();
- System.out.print("Enter coordinates y1: ");
- y1 = yey.nextInt();
- MyPoint point1 = new MyPoint(x1, y1);
- recta.setupperLeft(point1);
- System.out.println("nPoint B");
- System.out.print("nEnter coordinates x2: ");
- x2 = yey.nextInt();
- System.out.print("Enter coordinates y2: ");
- y2 = yey.nextInt();
- MyPoint point2 = new MyPoint(x2, y2);
- recta.setlowerRight(point2);
- System.out.print("nnLength: " + recta.length());
- System.out.print("nnWidth: " + recta.width());
- System.out.print("nArea: " + recta.area());
- System.out.print("nPerimeter: " +recta.perimeter());
- System.out.print("nn");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement