Create a program called, Containment, that accepts three command line arguments, a file that contains a list of shapes, and a pair of numbers the specifies the (x,y) coordinates for a point. The program should print out the list of shapes, and a true/false value that indicates if the point is contained in the shape.
The shapes file contains one shape per line. An example of the file with all the possible shapes is:
rect "r1" 20.0 5.0 50.0 25.0
ellipse "e1" 0.0 0.0 5.0 3.0
polygon "p1" 20.0 0.0 40.0 0.0 40.0 20.0 20.0 20.0
There are three possible shapes, a rectangle, an ellipse, and a polygon. The first word a line specifies the shape. The next word, surrounded by quotes specifies the identification, the remaining contents depend on the shape. The coordinate system is the same as Java's, the y coordinates increase from top to bottom, the x coordinates increase from left to right.
rect: The rectangle is specified by its top left corner and its width and height.
ellipse: The ellipse is specified by its top left corner and its width and height of the rectangle that bounds the ellipse
polygon: A polygon is specified by a list of x,y coordinates
The Containment program should output a line for every shape in the shape file, the line should contain the identification of the shape and either true or false depending on the points containment in the shape. Some examples of the output are:
java Containment shapes.data 2 2
r1 false
e1 true
p1 false
That was the question given to me in my assignment. There are a few issues i'm having with my code (Given Below)
import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
public class Containment {
private Readable reader;
private Scanner lines;
public boolean checker;
public int Counter = 0;
Containment( Readable reader ) {
this.reader = reader;
this.lines = new Scanner( reader );
}
public Shape nextShape() {
if ( !lines.hasNextLine() ) return null;
String line = lines.nextLine();
Scanner shapeScanner = new Scanner( line );
String type = shapeScanner.next();
if ( type.equals("rect") ) {
String d = shapeScanner.nextLine().trim();
double x1 = shapeScanner.nextDouble();
double y1 = shapeScanner.nextDouble();
double x2 = shapeScanner.nextDouble();
double y2 = shapeScanner.nextDouble();
d = d.replace('"', ' ');
return new Rectangle(x1,y1,x2,y2,d);
}
else if ( type.equals("ellipse") ) {
double x1 = shapeScanner.nextDouble();
double y1 = shapeScanner.nextDouble();
double x2 = shapeScanner.nextDouble();
double y2 = shapeScanner.nextDouble();
String d = shapeScanner.nextLine().trim();
d = d.replace('"', ' ');
return new Circle(x1,y1,x2,y2,d);
}
else if ( type.equals("polygon") ) {
ArrayList xValues = new ArrayList();
ArrayList yValues = new ArrayList();
if (Counter % 2 != 0){
double x = shapeScanner.nextDouble();
xValues.add(x);
}
}
return null;
}
class Shape {
private String desc;
private int x, y;
public Shape( String desc ) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
public String toString() {
return desc;
}
public boolean contains(int x, int y) {
return true;
}
}
class Rectangle extends Shape {
private double x1, y1, x2, y2;
public Rectangle(double x1, double y1, double x2, double y2, String desc ) {
super( desc );
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
Rectangle2D.Double box = new Rectangle2D.Double(x1,y1,x1,y2);
if (box.contains(15,30)){
System.out.println(desc + "true");
}
else {
System.out.println(desc + "false");
}
}
}
class Circle extends Shape {
private double x1, y1, x2, y2;
public Circle(double x1, double y1, double x2, double y2, String desc ) {
super( desc );
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
Ellipse2D.Double cir = new Ellipse2D.Double(x1,y1,x1,y2);
if (cir.contains(15,30)){
System.out.println(desc + "true");
}
else {
System.out.println(desc + "false");
}
}
}
public static void main( String[] args ) throws IOException {
InputStreamReader rd = new InputStreamReader(System.in);
Containment sf = new Containment( rd );
Shape s;
while ( (s=sf.nextShape()) != null ) {
}
}
}
Here's shapes.data that is also required
rect 10 20 30 40 "r1"
ellipse 10 20 30 40 "e1"
polygon 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 "p1"
a) I realize there's no condition for polygons. That's one problem i'm having, i've tried making array lists i just can't get it to work properly i get the following error
Note: Containment.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
b) I get an error when i try to use arguments as inputs for where to look for x and y locations in the code. Right now i'm using 15 and 30 as just an example
c) I can only seem to run the program when i input the code as
java.Containment < shapes.data
not as just
java.Containment shapes.data
as needed
d) New error, that when i run the program at all now, i get this error (even though i haven't changed anything from the last time i was working on it)
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Containment.nextShape(Containment.java:27)
at Containment.main(Containment.java:105)