Question

In: Computer Science

Create a program to input the length and width of a rectangle and calculate and print...

Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input rectangle. All methods should be tested in the runner class.

Solutions

Expert Solution

//Rectangle.java
public class Rectangle {
    int length, width;

    public Rectangle() {
        length = 0;
        width = 0;
    }

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

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

    public String toString() {
        return "Rectangle{" +
                "length=" + length +
                ", width=" + width +
                '}';
    }

    public int area(){
        return length*width;
    }

    public int perimeter(){
        return 2*(length+width);
    }
}

///////////////////////////////////////////////////////////

//RectangleRunner.java
import java.util.Scanner;
public class RectangleRunner {
    public static void main(String[] args) {
        Rectangle rectangle1 = new Rectangle();
        System.out.println("Area of rectangle 1 = "+rectangle1.area());
        System.out.println("Perimeter of rectangle 1 = "+rectangle1.perimeter());
        System.out.println();

        Rectangle rectangle2 = new Rectangle(2,3);
        System.out.println("Area of rectangle 2 = "+rectangle2.area());
        System.out.println("Perimeter of rectangle 2 = "+rectangle2.perimeter());
        System.out.println();

        Scanner scanner = new Scanner(System.in);
        int length, width;
        System.out.print("Enter length: ");
        length = scanner.nextInt();
        System.out.print("Enter width: ");
        width = scanner.nextInt();
        Rectangle rectangle3 = new Rectangle(length, width);
        System.out.println("Area of rectangle 3 = "+rectangle3.area());
        System.out.println("Perimeter of rectangle 3 = "+rectangle3.perimeter());
        System.out.println();
    }
}


Related Solutions

(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults...
(Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle. this is c sharp program please type the whole program.
a function named area to calculate the area of a rectangle area = Length x width...
a function named area to calculate the area of a rectangle area = Length x width 2.a function named volume to calculate the volume of a sphere volume = 4/3 x 3.14 x radius x radius x radius 3.a function named volume to calculate the volume of a cuboid volume = Length x width x ht 4. Use a loop structure to calculate the sum of all odd numbers from 1 to 17 5) Use a loop structure to calculate...
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double...
double maxArea(Rectangle a, Rectangle b, Rectangle c) {     double width;     double length;     double area = 0;     area = width * length;     cout << "\n***maxArea called" << endl;          cout << "***       rectangleCount = " << Rectangle::rectangleCount << endl << endl;    } Compete this code to find the maximum area of rectangle between a,b,c
A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased
For the following exercises, use the written statements to construct a polynomial function that represents the required information.A rectangle has a length of 10 inches and a width of 6 inches. If the length is increased by x inches and the width increased by twice that amount, express the area of the rectangle as a function of x.  
4. Define the width of a rectangle as the longest length of its sides. Given a...
4. Define the width of a rectangle as the longest length of its sides. Given a closed rectangle A in Rn and a partition P of A, define the mesh of P as the maximum width of its subrectangles. Prove that a bounded function f : A → R is integrable on A if and only if, for every > 0, there is a δ > 0 such that U(f, P) − L(f, P) < for every partition P of...
Martin wants to create a program that requires the user to input 10 integers then print...
Martin wants to create a program that requires the user to input 10 integers then print the total number of even integers, highest even integer, lowest even integer, total number of odd integers, highest odd integer, and lowest odd integer. If, however, the user inputs zero (0), the program will display “You have not yet entered 10 integers.” Below is the sample output of the program Martin created. Enter the integers: Integer 1: 15 Integer 2: 9 Integer 3: 71...
Find the length and width of a rectangle whose perimeter is 21 meters and whose area...
Find the length and width of a rectangle whose perimeter is 21 meters and whose area is 20 square meters. Assign variables to the unknown(s)... Form a system of equations... Solve the system and state your answer in the context of the problem, show all steps clearly, be sure to check your answers:
1. Write a class called Rectangle that maintains two attributes to represent the length and width...
1. Write a class called Rectangle that maintains two attributes to represent the length and width of a rectangle. Provide suitable get and set methods plus two methods that return the perimeter and area of the rectangle. Include two constructors for this class. One a parameterless constructor that initializes both the length and width to 0, and the second one that takes two parameters to initialize the length and width. 2. Write a java program (a driver application) that tests...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT