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)

Edited 4 Years Ago by Hypnos_16: n/a

a : is something being mentioned, it's not an actual error that will block your program
b : what error do you mean? can you show how you tried with params and what error message you get?
c : input what code? is that how you start your app, a line from your input file that's being read ..
d : not sure.

you should add a number of print statements, just to check whether or not you read the values you think you are reading.

also: never do this:

public static void main( String[] args ) throws IOException {

you'll never be able to avoid your application from crashing, since the main method is the very last place where you can catch the Exception and handle with it. change it to:

public static void main( String[] args ){
try{
// add your code here
}
catch(IOException ioe) {
ioe.printStackTrace();
// add whatever code you need to deal with the exception
}
finally{
// add this block if there's code you need to run, to finalize the program, whether
// an exception was thrown or not
}

d) i've solved, minor issue really.

i think b and c's issues go hand in hand,

When i run the program i have to give three command line arguments. The first one is the file with the information on the shapes i'm drawing. (shapes.data) and the next two have to be the x and y coordinates i use to look at a specific point and say if it is in fact in one of the shapes i've drawn.

So when type into the command line
java Containment shapes.data x y (x and y will be actual integers. Merely an example here) it should take the information from shapes.data, draw the shapes then take the x and y values and use them to look for points.

However, when i try to type this is in i don't get an error or anything, the entire program seems to freeze and nothing happens. I can only run it with

java Containment < shapes.data

and i think because i can only run it under this situation is why i'm having trouble with other command line arguments. So this is really the main issue right now.

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;
import java.io.BufferedReader;
import java.io.FileReader;
import java.awt.Polygon;

public class Containment {
    private Readable reader;
    private Scanner lines;
    public boolean checker;
    public static int xSearch, ySearch;
    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") ) {
            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 Rectangle(x1,y1,x2,y2,d);
        }
        else if ( type.equals("ellipse") ) {
            double x = shapeScanner.nextDouble();
            double y = shapeScanner.nextDouble();
            double w = shapeScanner.nextDouble();
            double h = shapeScanner.nextDouble();
            String d = shapeScanner.nextLine().trim();
	    d = d.replace('"', ' ');
            return new Circle(x,y,w,h,d);
        }
	else if ( type.equals("polygon") ) {
            double x1 = shapeScanner.nextDouble();
            double y1 = shapeScanner.nextDouble();
            double x2 = shapeScanner.nextDouble();
            double y2 = shapeScanner.nextDouble();
            double x3 = shapeScanner.nextDouble();
            double y3 = shapeScanner.nextDouble();
            double x4 = shapeScanner.nextDouble();
            double y4 = shapeScanner.nextDouble();
            String d = shapeScanner.nextLine().trim();
	    d = d.replace('"', ' ');
            return new Polygon(x1,y1,x2,y2,x3,y3,x4,y4,d);
	}
	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(xSearch,ySearch)){
		System.out.println(desc + "true");
	    }
	    else {
		System.out.println(desc + "false");
	    }
	}
    }

    class Circle extends Shape {
        private double x, y, w, h;
        public Circle(double x, double y, double w, double h, String desc ) {
            super( desc );
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
    	    Ellipse2D.Double Cir = new Ellipse2D.Double(x,y,w,h);
	    if (Cir.contains(xSearch,ySearch)){
		System.out.println(desc + "true");
	    }
	    else {
		System.out.println(desc + "false");
	    }
        }
    }
    class Polygon extends Shape {
	private double x1, y1, x2, y2, x3, y3, x4, y4;
	public Polygon(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, String desc ) {
	    super ( desc );
            this.x1 = x1;
            this.y1 = y1;
	    this.x2 = x2;
            this.y2 = y2;
	    this.x3 = x3;
            this.y3 = y3;
	    this.x4 = x4;
            this.y4 = y4;
	    double[] xValues = {x1, x2, x3, x4};
	    double[] yValues = {y1, y2, y3, y4};
	    Polygon Poly = new Polygon(xValues, yValues, 4);
	}
    }

    public static void main( String[] args ) throws IOException {
	BufferedReader rd = new BufferedReader(new FileReader(args[0]));
	xSearch = Integer.parseInt(args[1]);
	ySearch = Integer.parseInt(args[2]);
        Containment sf = new Containment( rd );
        Shape s;
        while ( (s=sf.nextShape()) != null ) {
        }
    }
}

Updated Code, only one issue remains

running this program nets this result

Containment.java:128: cannot find symbol
symbol  : constructor Polygon(double[],double[],int)
location: class Containment.Polygon
	    Polygon Poly = new Polygon(xValues, yValues, 4);
	                   ^
1 error

Can't figure out why that happens.

Edited 4 Years Ago by Hypnos_16: n/a

public Polygon(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, String desc )

that is the only constructor you have there, but you're rying to instantiate one with a constructor that takes only

Polygon Poly = new Polygon(xValues, yValues, 4);

also, watch out with what you are doing: if you correct this, to set the right parameters, you'll end uw in an endles loop, because the constructor will always recursively call itself, trying to create new instances of itself

Scratch that. I got it done!

Edited 4 Years Ago by Hypnos_16: n/a

This article has been dead for over six months. Start a new discussion instead.