Question

What if you had to write a program that would keep track of a list of...

What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards.

The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of class Rectangle as we want and store all of them in an ArrayList. Class ArrayList is defined in the Java API. https://docs.oracle.com/javase/8/docs/api/index.html

Attached you will find a file called "Rectangle.java".Rectangle.java???? Inside this source code file is the definition of class Rectangle. One object of class Rectangle represents the length and width of a rectangle.

Step 1) In order to familiarize yourself with this class Rectangle (attached), create a second source code file that defines a second class TestRectangle. This class TestRectangle contains "public static void main(String[] args)". Inside this main(), write the Java code that creates a few new Rectangle objects of different sizes, and calls the methods from class Rectangle on those objects. You do not need to submit this code, it is just so that you understand how to create new Rectangle objects and call methods on these new objects.

Once you are able to create new Rectangle objects and call methods on these new objects, then are ready to write the main() that will satisfy the requirements of this assignment.

Step 2) In the main() of class TestRectangle, prompt the user and read the length and width for one Rectangle object. Construct a new Rectangle object and set the length and width to the user's input values.

Step 3) In the main() of class TestRectangle, define a new ArrayList of Rectangle objects. Add the Rectangle object you constructed in Step 2) to the ArrayList. Print the ArrayList to confirm that your Rectangle object got into the ArrayList.

Step 4) Write a loop that repeats this process four times. Your main() will now prompt and read the length and width from the user, create a new Rectangle object and add it to the ArrayList - four times. At this point you must have a total of 4 Rectangle objects inside your one ArrayList. Print the ArrayList so that you can verify that all 4 Rectangle objects are in the ArrayList.

Hints:

-Start Step 4) by writing down what will appear on the console when your program is completed. You will not be able to write a program if you don't know exactly how this program should work.

-If both of these source code files are in the same project, you will not need to import Rectangle in the source code file that contains the main().

-Declare a final int for the number of Rectangle objects read from the user, so that it is easy to change the number of Rectangle objects that are stored by your program.

Rectangle.java

/**

* One object of this class represents a rectangle's length and width.

*/

public class Rectangle {

private int length;

private int width;

/**

* returns the length of the Rectangle object

*/

public int getLength() {

return this.length;

}

/**

* sets the length of the Rectangle to "newLength"

*/

public void setLength(int newLength) {

this.length = newLength;

}

/**

* returns the width fo the Rectangle object

*/

public int getWidth() {

return this.width;

}

/**

* Sets the width of the Rectangle to "newWidth"

*/

public void setWidth(int newWidth) {

this.width = newWidth;

}

/**

* Returns a String containing the length and

* width of the Rectangle.

*/

public String toString() {

return "For this Rectangle: length = " + this.length + ",

and width = " + this.width;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

*****************************************Code********************************************

import java.util.*;

/*** One object of this class represents a rectangle's length and width.*/

class Rectangle {
    private int length;
    private int width;

    /*** returns the length of the Rectangle object*/
    public int getLength() {
        return this.length;// Returns the length
    }

    /*** sets the length of the Rectangle to "newLength"*/
    public void setLength(int newLength) {
        this.length = newLength;//Used to set length
    }

    /*** returns the width fo the Rectangle object*/
    public int getWidth() {
        return this.width;//Used to get width
    }

    /*** Sets the width of the Rectangle to "newWidth"*/
    public void setWidth(int newWidth) {
        this.width = newWidth;//Used to set width
    }

    /*** Returns a String containing the length and * width of the Rectangle.*/
    public String toString() {
        return "For this Rectangle: length = " + this.length + ",and width = " + this.width;

    }
}

public class example {
    // numberOfObjects is final variable which will store the how many objects do we have to create
    private static final int numberOfObjects = 4;

    public static void main(String args[]) {
        // Creating scanner object for taking the data from the user
        Scanner in = new Scanner(System.in);
        ArrayList<Rectangle> al = new ArrayList<>();
        // Creating the arrayList of type Rectangle for storing the rectangle objects
        for (int i = 0; i < numberOfObjects; i++) {
            // Iterating the numberOfObjects times for taking the data from the user
            Rectangle r = new Rectangle();
            System.out.print("Enter the length and width:");
            // Prompting the user for entering the length and width
            r.setLength(in.nextInt());
            r.setWidth(in.nextInt());
            // setting the user given data to rectangle objects using setters
            al.add(r);
            //adding the rectangle object to array list
            System.out.println("*************************");
            System.out.println("Array List Elements:");
            for (Rectangle r1 : al) {
                // Printing the array list elements using for each method
                System.out.println(r1.toString());
                System.out.println("--------------------------");
            }
            System.out.println("*************************");
        }
        System.out.println("All the array list elements");
        // Displaying the all the contents of the array list
        for (Rectangle r1 : al) {
            System.out.println(r1.toString());
            System.out.println("--------------------------");
        }
    }
}

*****************************************Code Screens********************************************

41 42 43 7Creating scanner object for taking the data from the user Scanner innew Scanner(System. in); ArrayList<Rectangle> a

*****************************************Output Screens********************************************

sys1091@sys1091:~/IdeaProjects/ds/src$ javac example.java sys1091@sys1091:~/IdeaProjects/ds/src$ java example Enter the lengtsys1091@sys1091:/IdeaProjects/ds/srcs java example Enter the length and width:2 3 RKKKR Array List Elements: For this Rectangsys1091@sys1091:~/IdeaProjects/ds/src$ java example Enter the length and width:100 200 Array List Elements: For this Rectangl

Add a comment
Know the answer?
Add Answer to:
What if you had to write a program that would keep track of a list of...
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
  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

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

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

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

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