Suppose you define a file format to describe an array of java.awt.Rectangle obje
ID: 3846287 • Letter: S
Question
Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with an integer saying the length of the array, then a sequence of integers describing the x, y, width, and height parameters that can feed into the constructor for java.awt.Rectangle (according to the documentation). Let's define a method that will return an array of Rectangles given a String representing a filename. You could test the method by creating a file such as this:
You could print out the Rectangles in a loop to make sure they're correctly constructed. For example, if you saved the file to C:UsersDefaultDesktop ects.txt, you might have code like the following in the main method:
Please select the correct implementation of readRectangles. Assume that Rectangle, File, FileNotFoundException, and Scanner are correctly imported.
A)
B)
C)
D)
Explanation / Answer
Ans:-->
B)
Reason :--> This is the right approach because scanner object read the first line of the file which is value 2 and store in length variable which is the array the length of array.
Which is used to create the two rectangle object.
after that for loop is used to read the x , y , width and height of first rects and then for second rects and store in rect[0] and rect[1] and then this array return to main method.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.