Suppose you define a file format to describe an array of java.awt.Rectangle obje
ID: 3846306 • 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:2 0 0 10 10 10 10 20 20
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:
Rectangle[] r = readRectangles("C:\Users\Default\Desktop ects.txt"); for (int i = 0; i < r.length; i++) { System.out.println(r); }
Please select the correct implementation of readRectangles. Assume that Rectangle, File, FileNotFoundException, and Scanner are correctly imported.
A. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < s.nextInt(); i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } B. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } C. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { int x = s.nextInt(), y = s.nextInt(), width = s.nextInt(), height = s.nextInt(); rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } D. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; }
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:
2 0 0 10 10 10 10 20 20
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:
Rectangle[] r = readRectangles("C:\Users\Default\Desktop ects.txt"); for (int i = 0; i < r.length; i++) { System.out.println(r); }
Please select the correct implementation of readRectangles. Assume that Rectangle, File, FileNotFoundException, and Scanner are correctly imported.
A. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < s.nextInt(); i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } B. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } C. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { int x = s.nextInt(), y = s.nextInt(), width = s.nextInt(), height = s.nextInt(); rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } D. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; }
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:
2 0 0 10 10 10 10 20 20
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:
Rectangle[] r = readRectangles("C:\Users\Default\Desktop ects.txt"); for (int i = 0; i < r.length; i++) { System.out.println(r); }
Please select the correct implementation of readRectangles. Assume that Rectangle, File, FileNotFoundException, and Scanner are correctly imported.
A. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < s.nextInt(); i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } B. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } C. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[length]; for (int i = 0; i < length; i++) { int x = s.nextInt(), y = s.nextInt(), width = s.nextInt(), height = s.nextInt(); rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; } D. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException { Scanner s = new Scanner(new File(filename)); int length = s.nextInt(); Rectangle[] rects = new Rectangle[s.nextInt()]; for (int i = 0; i < length; i++) { rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt()); } s.close(); return rects; }
Explanation / Answer
The correct implementation for readRectangles() is option B
B. public static Rectangle[] readRectangles(String filename) throws FileNotFoundException {
Scanner s = new Scanner(new File(filename));
int length = s.nextInt();
Rectangle[] rects = new Rectangle[length];
for (int i = 0; i < length; i++) {
rects[i] = new Rectangle(s.nextInt(), s.nextInt(), s.nextInt(), s.nextInt());
}
s.close();
return rects;
}
It uses scanner to first open the file and then parses the first int to get the length of the array. It then creates an array of required length and loops to create each rectangle object by parsing the 4 consecutive ints.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.