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
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
Comment