Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class RectangleMain //the main class
- {
- public static void main(String[] args)
- {
- int x; //variable for x-coord
- int y; //y-coord variable
- int w; //width variable
- int h; //height variable
- Scanner screen= new Scanner(System.in); //scanner for x coordinate, y coordinate, width, and height
- System.out.print("Type in an x-coordinate.");
- x = screen.nextInt();
- System.out.println();
- System.out.print("Type in a y-coordinate.");
- y = screen.nextInt();
- System.out.println();
- System.out.print("Type in a width.");
- w = screen.nextInt();
- System.out.println();
- System.out.print("Type in a height.");
- h = screen.nextInt();
- System.out.println();
- Rectangle r1 = new Rectangle(x,y,w,h); //instantiating the object and calling constructor
- System.out.println("The top-left corner of the rectangle is (" + r1.getX() + "," + r1.getY() + ")"); //calling and printing return method for (x,y) point
- System.out.println("The width of the rectangle is " + r1.getWidth()); //calling and printing return method for width
- System.out.println("The height of the rectangle is " + r1.getHeight()); //calling and printing return method for height
- System.out.println("The area of the rectangle is " + r1.Area(w,h)); //calling and printing return method for area
- System.out.print(Rectangle.toString(r1)); //calling and printing string method
- }
- }
- public class Rectangle //object class
- {
- private int x1;
- private int y1;
- private int h1;
- private int w1;
- public Rectangle (int x2, int y2, int w2, int h2) //constructor for rectangle
- {
- x1 = x2;
- y1 = y2;
- h1 = h2;
- w1 = w2;
- }
- public int getHeight() //returns height
- {
- return h1;
- }
- public int getWidth() //returns width
- {
- return w1;
- }
- public int getX() //returns x-coordinate
- {
- return x1;
- }
- public int getY() //returns y-coordinate
- {
- return y1;
- }
- public static int Area(int w, int h) //calculates and returns area
- {
- return w*h;
- }
- public static String toString(Rectangle r) //string method
- {
- return ("Rectangle[x=" + r.x1 + "," + " y=" + r.y1 + "," + " width=" + r.w1 + "," + " height=" + r.h1 + "]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment