Question: I have a Java program that finds the smallest possible rectangular fence to enclose sheep at different coordinates. Right now, the program reads from the

I have a Java program that finds the smallest possible rectangular "fence" to enclose sheep at different coordinates. Right now, the program reads from the keyboard. But I would need it to read from a text file instead. This is what a text file's contents would be:

 ``` 5 0 0 0 5 10 5 3 3 10 0

```

and here is my current code:

```

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class BSheep { final static int MIN_SHEEP = 2; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int nSheep = sc.nextInt(); // validate nSheep if (nSheep < MIN_SHEEP) { System.out.println("Invalid input! We need at least " + MIN_SHEEP + " sheep to build a fence!"); System.exit(1); } int xMin, xMax, yMin, yMax; // reading the 1st coordinate, using it to initialize min, max values xMin = xMax = sc.nextInt(); yMin = yMax = sc.nextInt(); // read the remaining coordinates for (int i = 1; i <= nSheep - 1; i++) { int x = sc.nextInt(); int y = sc.nextInt(); if (x < xMin) xMin = x; if (x > xMax) xMax = x; if (y < yMin) yMin = y; if (y > yMax) yMax = y; } System.out.print("Fence Coordinates: {(" + xMin + "," + yMin + "), "); System.out.print("(" + xMax + "," + yMin + "), "); System.out.print("(" + xMax + "," + yMax + "), "); System.out.println("(" + xMin + "," + yMax + ") "); } // end of main function } // end of class definition

```

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!