Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter

(JAVA)

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 (Links to an external site.)Links to an external site.

Attached you will find a file called "Rectangle.java".Rectangle.javaimage text in transcribed 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions