My stack wont POP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AZRebelCowgirl73
    New Member
    • Nov 2006
    • 47

    My stack wont POP!

    I am currently working on a homework assignment and I am stumped. Basically I have done most of what is required, however my stack is not correctly using the pop function. This is what I have so far. (note: I have added alot of print data to show myself what is going on within this program.

    import ch03.stacks.*;
    import java.awt.geom.* ;
    import java.util.*;
    public class Robot {
    private Stack<Point2D.D ouble> stack; //stack to hold array objects
    private Point2D.Double point;
    public Robot() {
    this.point = null;
    this.stack = null;
    }
    public void goInitial() {
    //begins at coordinates (0.0, 0.0)
    if (this.stack != null) {
    System.out.prin tln("already initialized");
    return;
    }
    this.point = new Point2D.Double( 0.0, 0.0);
    this.stack = new Stack<Point2D.D ouble>();
    }
    public double getDX() {
    return this.point.getX ();
    }
    public double getDY() {
    return this.point.getY ();
    }
    public void goRelative(doub le x, double y) {
    //push the coordinates on the top of stack
    //return currPosition
    if (this.stack == null) {
    System.out.prin tln("not initialized yet");
    return;
    }
    double nextX = this.point.getX () + x;
    double nextY = this.point.getY () + y;
    this.point = new Point2D.Double( nextX, nextY);
    this.stack.push (this.point);
    }
    public void goBack() {
    //pop the coordinates from the top of the stack
    if (this.stack == null) {
    System.out.prin tln("not initialized yet");
    return;
    }
    if(this.stack.s ize() == 0) {
    System.out.prin tln("nothing to go back too");
    return;
    }
    this.point = this.stack.pop( );
    }
    public void printRobot() {
    System.out.prin tln("(" + getDX() + ", " + getDY() + ")");
    }
    }

    *************** *************** *************** *************** *************** *************** *************** *************** *****
    import java.util.*;
    import java.io.*;

    public class RobotDriver {
    public static void main (String [] args) throws IOException {
    Robot robot = new Robot();
    Scanner stdin = new Scanner(System. in);
    System.out.prin tln("Please choose a file to load the car part data from.");
    System.out.prin t("File: ");
    String fileName = stdin.nextLine( );
    try {
    BufferedReader br = new BufferedReader( new FileReader(file Name));
    //Read dist
    String line = br.readLine();
    double dist = Double.parseDou ble(line);
    while((line = br.readLine()) != null) {
    String[] command = line.split(" ");
    if ("goInitial".eq uals(command[0])) {
    robot.goInitial ();
    robot.printRobo t();
    }
    else if ("goRelative".e quals(command[0])) {
    double x = Double.parseDou ble(command[1]);
    double y = Double.parseDou ble(command[2]);
    robot.goRelativ e(x, y);
    robot.printRobo t();
    //check boundaries here!(if/else)
    }
    else if ("goBack".equal s(command[0])) {
    robot.goBack();
    robot.printRobo t();
    }
    }
    }
    catch(FileNotFo undException fnfe) {
    System.out.prin tln("~~" + fileName + "~~ File not found!");
    }
    catch(IOExcepti on ioe) {
    }
    }
    }
    *************** *************** *************** *************** *************** *************** *************** ***************
    basically i have this for an assignment create a robot class with the methods goInitial() goRelative(D,D) and goBack. My goBack method is not popping like i need it to! All the extra print stuff is for me to see what is going on in the program(not actually required) Then I have to design a reader input program that creates a robot that walks through the minefield, when the robot is finished reading in the file, the program should then print out the path in reverse order(reason for stack usage) the correct path taken by the robot. When i use the following file:
    Test1.txt
    21.2
    goInitial
    goRelative 2.6 3.1
    goRelative 2.1 -1.6
    goBack
    goRelative 4.1 2.8
    goRelative -2.2 1.8
    goRelative 2.9 4.5
    goBack
    goRelative 5.6 3.8
    goRelative 5.2 6.3

    the go back commands are not functioning correctly! Can anyone help me? Does anyone even understand my problem? Thanks ALI :P
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by AZRebelCowgirl7 3
    I am currently working on a homework assignment and I am stumped. Basically I have done most of what is required, however my stack is not correctly using the pop function. This is what I have so far. (note: I have added alot of print data to show myself what is going on within this program.

    import ch03.stacks.*;
    import java.awt.geom.* ;
    import java.util.*;
    public class Robot {
    private Stack<Point2D.D ouble> stack; //stack to hold array objects
    private Point2D.Double point;
    public Robot() {
    this.point = null;
    this.stack = null;
    }
    public void goInitial() {
    //begins at coordinates (0.0, 0.0)
    if (this.stack != null) {
    System.out.prin tln("already initialized");
    return;
    }
    this.point = new Point2D.Double( 0.0, 0.0);
    this.stack = new Stack<Point2D.D ouble>();
    }
    public double getDX() {
    return this.point.getX ();
    }
    public double getDY() {
    return this.point.getY ();
    }
    public void goRelative(doub le x, double y) {
    //push the coordinates on the top of stack
    //return currPosition
    if (this.stack == null) {
    System.out.prin tln("not initialized yet");
    return;
    }
    double nextX = this.point.getX () + x;
    double nextY = this.point.getY () + y;
    this.point = new Point2D.Double( nextX, nextY);
    this.stack.push (this.point);
    }
    public void goBack() {
    //pop the coordinates from the top of the stack
    if (this.stack == null) {
    System.out.prin tln("not initialized yet");
    return;
    }
    if(this.stack.s ize() == 0) {
    System.out.prin tln("nothing to go back too");
    return;
    }
    this.point = this.stack.pop( );
    }
    public void printRobot() {
    System.out.prin tln("(" + getDX() + ", " + getDY() + ")");
    }
    }

    *************** *************** *************** *************** *************** *************** *************** *************** *****
    import java.util.*;
    import java.io.*;

    public class RobotDriver {
    public static void main (String [] args) throws IOException {
    Robot robot = new Robot();
    Scanner stdin = new Scanner(System. in);
    System.out.prin tln("Please choose a file to load the car part data from.");
    System.out.prin t("File: ");
    String fileName = stdin.nextLine( );
    try {
    BufferedReader br = new BufferedReader( new FileReader(file Name));
    //Read dist
    String line = br.readLine();
    double dist = Double.parseDou ble(line);
    while((line = br.readLine()) != null) {
    String[] command = line.split(" ");
    if ("goInitial".eq uals(command[0])) {
    robot.goInitial ();
    robot.printRobo t();
    }
    else if ("goRelative".e quals(command[0])) {
    double x = Double.parseDou ble(command[1]);
    double y = Double.parseDou ble(command[2]);
    robot.goRelativ e(x, y);
    robot.printRobo t();
    //check boundaries here!(if/else)
    }
    else if ("goBack".equal s(command[0])) {
    robot.goBack();
    robot.printRobo t();
    }
    }
    }
    catch(FileNotFo undException fnfe) {
    System.out.prin tln("~~" + fileName + "~~ File not found!");
    }
    catch(IOExcepti on ioe) {
    }
    }
    }
    *************** *************** *************** *************** *************** *************** *************** ***************
    basically i have this for an assignment create a robot class with the methods goInitial() goRelative(D,D) and goBack. My goBack method is not popping like i need it to! All the extra print stuff is for me to see what is going on in the program(not actually required) Then I have to design a reader input program that creates a robot that walks through the minefield, when the robot is finished reading in the file, the program should then print out the path in reverse order(reason for stack usage) the correct path taken by the robot. When i use the following file:
    Test1.txt
    21.2
    goInitial
    goRelative 2.6 3.1
    goRelative 2.1 -1.6
    goBack
    goRelative 4.1 2.8
    goRelative -2.2 1.8
    goRelative 2.9 4.5
    goBack
    goRelative 5.6 3.8
    goRelative 5.2 6.3

    the go back commands are not functioning correctly! Can anyone help me? Does anyone even understand my problem? Thanks ALI :P
    1.) Use code tags when posting code
    2.) What makes you sure it's the pop method not working. If you think that's the problem then you should have just posted the pop method that you have.

    Comment

    • AZRebelCowgirl73
      New Member
      • Nov 2006
      • 47

      #3
      Originally posted by r035198x
      1.) Use code tags when posting code
      2.) What makes you sure it's the pop method not working. If you think that's the problem then you should have just posted the pop method that you have.
      I am sorry! I dont know how to post tags, and I am sorry but I am assuming the pop method is not working because the same values come up as the previous line rather then the line before that!

      Comment

      • AZRebelCowgirl73
        New Member
        • Nov 2006
        • 47

        #4
        Originally posted by AZRebelCowgirl7 3
        I am sorry! I dont know how to post tags, and I am sorry but I am assuming the pop method is not working because the same values come up as the previous line rather then the line before that!
        The goBack() method is working, my problem is in the printing of the stack itself! Thanks!!!! ALI :P

        Comment

        • AZRebelCowgirl73
          New Member
          • Nov 2006
          • 47

          #5
          I cant seem to figure out how to print the stack. If i try to use the top() method for stacks, it says it is not allowed. As I said I am new at this so I can't figure out how to print the stack! ALI :P

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by AZRebelCowgirl7 3
            I cant seem to figure out how to print the stack. If i try to use the top() method for stacks, it says it is not allowed. As I said I am new at this so I can't figure out how to print the stack! ALI :P
            You didn't post your stack class (I'm not asking you to post it), so we can't tell whether the methods in it are correct or not. What's the full error message that you get when you use the top method?

            Comment

            • AZRebelCowgirl73
              New Member
              • Nov 2006
              • 47

              #7
              Here is my print method! it is printing but there is alot of extra info!

              Code:
              public void printRobot() {
                      while (!this.stack.empty()) {
                      Point2D.Double result = this.stack.pop();
                      System.out.println("(" + result + ")");
                      }
                    }
              it prints for example
              (Point2D.Double[15.999999999999 97, 16.4])
              I need to just print (15.99, 16.4)

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by AZRebelCowgirl7 3
                Here is my print method! it is printing but there is alot of extra info!

                Code:
                public void printRobot() {
                        while (!this.stack.empty()) {
                        Point2D.Double result = this.stack.pop();
                        System.out.println("(" + result + ")");
                        }
                      }
                it prints for example
                (Point2D.Double[15.999999999999 97, 16.4])
                I need to just print (15.99, 16.4)
                Use decimal format, like I've pointed out in this thread.

                Comment

                Working...