1

I am having a hard time getting this assignment done for school. There is more to it than just what is in the title, but basically this is what I need to do first. Read each line of the text file that have an x and a y point, extract them, and store them in a new node. I then need to add this new node into a linked list and repeat for every x y coordinate. No arrays are to be used.

This is the code I have so far:

public class Point {

public int data;
public Point next;

public Point(int data, Point next) {
    this.data = data;
    this.next = next;
}

@Override
public String toString() {
    return data + "";
}

}

public class ShapeAbstraction {

public void readCoordinates() {
    String fileName = "shapelist.txt";
    String line = null;

    try {
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {

            System.out.println(line);
        } 
        bufferedReader.close();
    }

    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file " + fileName);
    }

    catch(IOException ex) {
        System.out.println("Error reading file" + fileName);
    }

}

public static void main(String[] args) {
    ShapeAbstraction sa = new ShapeAbstraction();
    sa.readCoordinates();

    Point front = new Point(0, null);
    System.out.println(front);
}

}

The coordinates in the .txt file look like this

10 0
18 0
17 2
21 7
19 12

That's only the first 5 out of hundreds.

If someone could help that would be amazing. Thanks. Assignment for reference BTW we are now allowed to use the java linked list class.

3 Answers3

0

Think of a LinkedList as a chain of linked nodes, and each node contains your data or value (e.g. a point). When you start parsing the coordinates in your .txt file, start making nodes and linking them through such attributes in a node as "previous" and "next". These attributes are also the node type, which in C is represented as pointers. For example in Java:

private class Node {
   private Point value;
   private Node previous;
   private Node next;
}

You may find this link very helpful.

TonyW
  • 18,375
  • 42
  • 110
  • 183
0

Your Point class will need to hold two coordinates, and whatever the "importance value" is. Something along the lines of this, with constructors and methods and what not

private class Point {
    int x;
    int y;
    int importance;
    //Add constructors/methods that are needed
}

EDIT: OP commented saying they no longer have to implement the linked list class. I'll leave my details on doing that at the bottom for anyone interested..

Once that is set up you can then use that class in your ShapeAbstraction class. You'll need to parse each line to get the coordinates and create a new Point then add the Point to a LinkedList. Something along the lines of:

int x = line.split(" ")[0];
int y = line.split(" ")[1];
Point newP = new Point(x, y); //assuming you create this constructor
allPoints.add(newP); //where allPoints was created with new LinkedList<Point>();



You'll need a few more classes to handle implementing your own LinkedList. As the assignment says:

You must at least have classes for

  • a Point. A point contains the position x,y, and the importance value.
  • a Node. The basic structure for your linked list. It needs to be able to hold a Point, as well as the reference to the next Node.
  • a LinkedList. Your main data structure. You will need methods to add a point, to traverse the list. You also must be able to access elements in the list. Here it would make sense to have a method "getNext", along with a method "reset", which resets the reference used in "getNext" to the start.

You'll then have a Node class which, as the writeup says, needs to hold a Point and a reference to the next Node.

private class Node {
    Point point;
    Node next;
    //Add constructors/methods that are needed
}

And you'll need to implement a linked-list. There are other online resource for this, such as this SO question. You'll need to have a method which adds a Point object to the list:

public void add(Point p) {
    Node newN = new Node(p); //assuming you've created this constructor

    //Then add the point, depending on how you implement the linked-list.
    //It might look something like:
    last.next = newN;
    last = newN;
}

note: My code is not doing any error checking or null checking or anything, and I certainly did not write all the methods and constructors you'll need to write. Hopefully this will help clear up what needs to be done for you though!

Community
  • 1
  • 1
Jordan Shurmer
  • 946
  • 7
  • 21
  • Thanks, this helps a lot. I forgot to mention, we do not need to implement our own linked list method anymore. We may use the java linked list class now. – Patrick Boston Sep 22 '15 at 17:36
0

You could change your class Point like this:

public class Point {

public int x;
public int y;
public Point tail;

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

public void setTail (Point tail) {
    this.tail = tail;
}

public Point getTail () {
    return this.tail;
}

@Override
public String toString() {
    return "(" + x + "," + y + ")";
}

}

When you read lines from file, you could use the split() method of String class.

For each line you'll:

  • split it
  • get the coordinates from split result
  • create a new Point with the coordinates
  • set the new Point as tail for the previous point
  • you can do the above steps one time before the main loop, to initialize the first node, the Head of the linked list