Question

Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...

Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields:

int x (x-coordinate for its top left corner)

int y (y coordinate for its top left corner)

double height (height of the rectangle)

double width (width of the rectangle)

Your rectangle objects should have the following methods:

public Rectangle(int newx, int newy, int newwidth, int newheight)

Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height.

public double getHeight() (returns the rectangles height)

public double getWidth() (returns the rectangle's width)

public int getX() (returns the rectangle's x-coordinates)

public int getY() (returns the rectangle's y-coordinate)

public String toString() (returns a string representation of this rectangle such as "Rectangle [x=2, y=13, height=14, width = 5]")

public double area() (returns area of rectangle)

public double perimeter() (returns perimeter of the rectangle)

Write a client program called RectangleClient that creates objects of theRectangle class called rect1 and rect2. Assign values to the field of these objects. Print out these rectangle objects using toString method, and then print out their area and perimeter.

Homework Answers

Answer #1
class Rectangle {

    private int x, y;
    private double height, width;

    public Rectangle(int newx, int newy, int newwidth, int newheight) {
        x = newx;
        y = newy;
        width = newwidth;
        height = newheight;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public double getHeight() {
        return height;
    }

    public double getWidth() {
        return width;
    }

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

    public double area() {
        return height * width;
    }

    public double perimeter() {
        return 2*(height+width);
    }
}

class RectangleClient {
    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle(0, 0, 7, 4), rect2 = new Rectangle(3, -5, 7, 10);
        System.out.println("Rectangle 1: " + rect1);
        System.out.println("Area: " + rect1.area());
        System.out.println("Perimeter: " + rect1.perimeter());

        System.out.println("Rectangle 2: " + rect2);
        System.out.println("Area: " + rect2.area());
        System.out.println("Perimeter: " + rect2.perimeter());
    }
}

Know the answer?
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
Similar Questions
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
Defining a Class Define the class Rectangle. It’s constructor takes a pair of numbers representing the...
Defining a Class Define the class Rectangle. It’s constructor takes a pair of numbers representing the top-left corner, and two other numbers representing the width and height. It has the following methods: get_bottom_right() - return the bottom right corner as a pair of numbers. move(p) - move the rectangle so that p becomes the top-left corner (leaving the width and height unchanged). resize(width, height) - set the width and height of the rectangle to the supplied arguments (leaving the top-left...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a...
Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a graphics system that has classes for various figures — say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and either a center point or upper-left corner point, while a box and circle might have only a center point (or upper-right corner point) and an edge length or radius, respectively. In a well-designed system, these would...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...
Using Java Create the class RightTriangle. Its constructor will require the lengths only of its two...
Using Java Create the class RightTriangle. Its constructor will require the lengths only of its two legs. It will then calculate the length of the third side (its “hypotenuse”) using the Pythagorean Theorem: a2 + b2 = c2. This class will have no instance variables itself, so it will need to set the appropriate variables in its parent class, Triangle. It will inherit equals from its parent class You also should enhance the String returned from the toString method of...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 4. List and describe the THREE (3) necessary conditions for complete similarity between a model and...
    asked 3 minutes ago
  • In C++ Complete the template Integer Average program. // Calculate the average of several integers. #include...
    asked 8 minutes ago
  • A uniform rod is set up so that it can rotate about a perpendicular axis at...
    asked 10 minutes ago
  • To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
    asked 31 minutes ago
  • How could your result from GC (retention time, percent area, etc.) be affected by these following...
    asked 41 minutes ago
  • QUESTION 17 What are the tasks in Logical Network Design phase? (Select five. ) Design a...
    asked 43 minutes ago
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 52 minutes ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 1 hour ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 1 hour ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 1 hour ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 1 hour ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 1 hour ago