Square Pasture (Bronze)
Link
Java
import java.util.*;
import java.io.*;
public class SquarePasture {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("square");
int x1 = io.nextInt(), y1 = io.nextInt(), x2 = io.nextInt(), y2 = io.nextInt();
int x3 = io.nextInt(), y3 = io.nextInt(), x4 = io.nextInt(), y4 = io.nextInt();
//find the sides of the smallest rectangle covering both pastures
int left = Math.min(x1, x3);
int right = Math.max(x2, x4);
int bottom = Math.min(y1, y3);
int top = Math.max(y2, y4);
int side = Math.max(right - left, top - bottom);
io.print(side * side);
io.close();
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}
When I tried getting it graded, it gives me an error of:
Exception in thread “main” java.io.FileNotFoundException: square.in (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
at java.base/java.io.FileInputStream.(FileInputStream.java:153)
at java.base/java.io.FileInputStream.(FileInputStream.java:108)
at java.base/java.io.FileReader.(FileReader.java:60)
at usaco.SquarePasture$Kattio.(SquarePasture.java:47)
at usaco.SquarePasture.main(SquarePasture.java:17)
Checklist
See How to ask for help on a problem for more information.
- Include a link to the problem
- Format your post (especially code blocks)
- Include what you’ve tried so far
- Add comments to your code
- Read the debugging module