Question

Java Object Oriented Create a class that models a rectangle located in the first Cartesian quadrant....

Java Object Oriented

Create a class that models a rectangle located in the first Cartesian quadrant. The class should store information about a rectangle such that it can be drawn in an X,Y cartesian coordinate system, including rectangles which are not aligned parallel to the X-Y axes, that is, they are oriented making an angle with the X axis.

Your class should provide one constructor methods for specifying the rectangle The constructor should receive the rectangle area, the length of the long side, the angle that the long side makes with the X axis, and the coordinates for the vertex closer to the origin and the X-axis.

Your class should provide methods for (computing and) returning the area of the rectangle

Your class should provide methods for (computing and) returning the length of short and long side sides of the rectangle

Your class should provide methods for (computing and) returning the angle it's long side makes with the horizontal (x axis)

Your class should provide methods for (computing and) returning the coordinates for all the corners of the rectangle starting with the coordinate closer to the origin and traversing the rectangle in counter clockwise order.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Rectangle {

    private double area, longSide, angleX;
    private double lowerLeftX, lowerLeftY;

    // angle should be in radians
    public Rectangle(double area, double longSide, double angleX, double lowerLeftX, double lowerLeftY) {
        this.area = area;
        this.longSide = longSide;
        this.angleX = angleX;
        this.lowerLeftX = lowerLeftX;
        this.lowerLeftY = lowerLeftY;
    }

    double getArea() {
        return area;
    }

    double lengthShortSide() {
        return area/longSide;
    }

    double lengthLongSide() {
        return longSide;
    }

    Coordinate getLowerLeft() {
        return new Coordinate(lowerLeftX, lowerLeftY);
    }

    Coordinate getLowerRight() {

        double width = Math.cos(angleX) * longSide;
        double height = Math.sin(angleX) * longSide;

        return new Coordinate(lowerLeftX + width, lowerLeftY + height);
    }

    Coordinate getUpperLeft() {
        double shortSide = lengthShortSide();
        double width = Math.cos(Math.PI/2 - angleX) * shortSide;
        double height = Math.sin(Math.PI/2 - angleX) * shortSide;

        return new Coordinate(lowerLeftX - width, lowerLeftY + height);
    }

    Coordinate getUpperRight() {
        double shortSide = lengthShortSide();
        Coordinate lowerRight = getLowerRight();

        double angle = Math.PI/2 - angleX;

        double width = Math.cos(angle) * shortSide;
        double height = Math.sin(angle) * shortSide;

        return new Coordinate(lowerRight.x - width, lowerRight.y + height);
    }

}

class Coordinate {
    double x, y;

    public Coordinate(double x, double y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Coordinate [x=" + x + ", y=" + y + "]";
    }   
}

====================================================================

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class RectangleDemo extends JComponent {

    private static final long serialVersionUID = 1L;

    public void paintComponent(Graphics g) {
        Rectangle r = new Rectangle(20000, 120, Math.PI / 6, 120, 60);

        Coordinate lowerLeft = r.getLowerLeft();
        Coordinate lowerRight = r.getLowerRight();
        Coordinate upperLeft = r.getUpperLeft();
        Coordinate upperRight = r.getUpperRight();

        System.out.println(lowerLeft);
        System.out.println(lowerRight);
        System.out.println(upperLeft);
        System.out.println(upperRight);
        drawLine(g, lowerLeft.x, lowerLeft.y, lowerRight.x, lowerRight.y);
        drawLine(g, upperRight.x, upperRight.y, lowerRight.x, lowerRight.y);
        drawLine(g, upperRight.x, upperRight.y, upperLeft.x, upperLeft.y);
        drawLine(g, lowerLeft.x, lowerLeft.y, upperLeft.x, upperLeft.y);
    }

    void drawLine(Graphics g, double x, double y, double x2, double y2) {
        g.drawLine((int) x, (int) y, (int) x2, (int) y2);
    }

    public static void main(String args[]) {
        RectangleDemo panel = new RectangleDemo();

        // frame to hold the panel
        JFrame application = new JFrame();

        // set frame to exit on close
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        application.add(panel);
        application.setSize(500, 500);
        application.setVisible(true);
    }
}

3 import javax.swing.JComponent; 4 import javax. Swing. JFrame; 6 public class RectangleDemo extends JComponent { 7 private static final long serialVersionUID1L; 9 10 public void paintComponent (Graphics g) { Rectangle r- new Rectangle (20000, 120, Math.PI/ 6, 120, 60); 12 13 14 15 16 17 18 19 20 21 Coordinate lowerLeft = r.getLowerLeft(); Coordinate lowerRight r.getLowerRight); Coordinate upperLeftr.getUpperLeft) Coordinate upperRight r.getUpperRight(); System.out.println(lowerLeft); System.out.println(lowerRight); System.out.println(upperLeft); System.out.println(upperRight); drawLine(g, lowerLeft.x, lowerLeft.y, lowerRight.x, lowerRight.y); drawLine(g, upperRight.x, upperRight.y, lowerRight.x, lowerRight.y); drawLine(g, upperRight.x, upperRight.y, upperLeft.x, upperLeft.y); drawLine(g, lowerLeft.x, lowerLeft.y, upperLeft.x, upperLeft.y); 23 24 26 27 28 void drawLine (Graphics g, double x, double y, double x2, double y2) { 29 g.drawLine((int) x, (int) y, (int) x2, (int) v2) 2 Console 8 RectangleDemo Java Application] C1 Program FilesJavalidk1.8.0_731binVavaw.exe (Sep 7, 2018, 11:26:26 PM) Coordinate [x-120.0, y-60.0] Coordinate [x-223.92304845413264, y 120.0] Coordinate [x=36.666666666666686, y-204.33756729740645] Coordinate [x-140.58971512079933, y 264.33756729740645]

Hi. please find the answer above.. In case of any doubts, please ask in comments. If the answer helps you, please upvote. I am in great need of upvotes. Thanks!

Add a comment
Know the answer?
Add Answer to:
Java Object Oriented Create a class that models a rectangle located in the first Cartesian quadrant....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • Go to the Java API and review the Rectangle class briefly. As directed below, create a...

    Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...

  • My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two...

    My assignment is to create a rectangle class in java. •Create a MyRectangleClass •There are two private instance variables, length l and width w with double data type. •There is a constructor with two parameter •There are 4 public methods which are circumference, area, getWidth, and getLength. •circumference method will return circumference to the caller.(the formula for circumference is 2 *l + 2*w ) •Area method Return the area to the caller(:l*w) •getLength method will return length to the caller....

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named...

    java language please. thank you T-Mobile 9:00 AM For this assignment, create a Java class named "MyRectangle3d" Your class must have the following attributes or variables · x (the x coordinate of the rectangle, an integer) * y (the y coordinate of the rectangle, an integer) * length (the length of the rectangle, an integer) * width (the width of the rectangle, an integer) * height (the height of the rectangle, an integer) * color( the color of the rectangle,...

  • Create a new class called Point that represents a point in the Cartesian plane with two...

    Create a new class called Point that represents a point in the Cartesian plane with two coordinates. Each instance of Point should have to coordinates called x and y. The constructor for Point should take two arguments x and y that represent the two coordinates of the newly created point. Your class Point should override the __str__(self) method so that it returns a string showing the x- and y-coordinates of the Point enclosed in parentheses and separated by a comma....

  • In Java, write a class Rectangle. This Rectangle class should have only the following public methods...

    In Java, write a class Rectangle. This Rectangle class should have only the following public methods (you can add other non-public methods): Write a constructor that creates a rectangle using the x, y coordinates of its lower left corner, its width and its height in that order. Creating a rectangle with non-positive width or height should not be allowed, although x and y are allowed to be negative. Write a method overlap(Rectangle other). This method should return true if this...

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT