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
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...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
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...
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...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...
SIMPLE PAINTING GUI APP in Java Your mission in this exercise is to implement a very...
SIMPLE PAINTING GUI APP in Java Your mission in this exercise is to implement a very simple Java painting application. Rapid Protyping The JFrame app must support the following functions: (you can use any other programming languages that you are comfortable)  Draw curves, specified by a mouse drag.  Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated).  Shape selection...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT