Question

1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have th

JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

class RectangleTest {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Rectangle rectangle = new Rectangle();
        System.out.print("Enter width of rectangle: ");
        rectangle.setWidth(in.nextInt());
        System.out.print("Enter height of rectangle: ");
        rectangle.setHeight(in.nextInt());
        System.out.println("Width: " + rectangle.getWidth());
        System.out.println("Height: " + rectangle.getHeight());
        System.out.println("Area: " + rectangle.getArea());
    }
}

class Rectangle {

    private int width, height;

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getArea() {
        return width * height;
    }
}

Enter width of rectangle: 4 Enter height of rectangle: 7 Width: 4 Height: 7 Area: 28 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...
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
  • 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 please! Problem You will be using the point class discussed in class to create...

    In Java please! Problem You will be using the point class discussed in class to create a Rectangle class. You also need to have a driver class that tests your rectangle. Requirements You must implement the 3 classes in the class diagram below and use the same naming and data types. Following is a description of what each method does. Point Rectangle RectangleDriver - x: int - top Left: Point + main(args: String[) - y: int - width: int +...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • ****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...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Write a class definition for a Rectangle. The data fields should be: • An integer for...

    Write a class definition for a Rectangle. The data fields should be: • An integer for the value of the width of the Rectangle • An integer for the value of the height of the Rectangle An integer for the value of the area of the Rectangle It must have: . A default constructor A constructor that has a parameters for width and height and assigns them to the member variables. • The class should have mutators for all of...

  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

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
ADVERTISEMENT