Question

Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with...

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:\Users\Default\Desktop\rects.txt, you might have code like the following in the main method:

Rectangle[] r = readRectangles("C:\\Users\\Default\\Desktop\\rects.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;
}

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

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.

Add a comment
Know the answer?
Add Answer to:
Suppose you define a file format to describe an array of java.awt.Rectangle objects. You start with...
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
  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid...

    Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid 29 Barbatus Druid 70 Ukaimar Mage 47 Mariko Priest 33 Farmbuyer Priest 70 Valefar Warlock 42 Teslar Paladin 64 Nerdsbane Hunter 12 We wish to store information regarding several characters in a video game. Each character has a name, and a class, and a level. This information is repeated, 3 lines per character, for any number of characters. To simplify processing, the very first...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

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