Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started