Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I need Help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi everyone
I need help for this project:
Write an application that controls the movements of a robot rat via a control panel. The robot rat will move about a "floor" represented by a two-dimensional boolean array. The robot rat has a pen attached to its tail. As the robot rat moves about the floor it will leave a mark if the pen is in the down position and not if the pen is in the up position.

Control the robot rat with several buttons displayed by your application. You can make the robot rat turn right, turn left and move forward a number of spaces that are contained in a JTextField. Another button will let you toggle the pen's position.

Each time you move the rat forward print the floor to show the rat's progress. You may print the floor pattern to the console or to a GUI component.

Thank you in advance for your help





import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class RobotRat extends JFrame implements ActionListener {

private JPanel _panel1 = null;
private JPanel _panel2 = null;
private JLabel _label1 = null;
private JTextField _textfield1 = null;
private JButton _button1 = null;
private JButton _button2 = null;
private JButton _button3 = null;
private JButton _button4 = null;
private JButton _button5 = null;
private JButton _button6 = null;
private JButton _button7 = null;
private JButton _button8 = null;
private JButton _button9 = null;

private int _rows = 0;
private int _cols = 0;
private boolean[][] floor = null;

private static final int UP = 0;
private static final int DOWN = 1;
private int _pen_position;

private static final int NORTH = 0;
private static final int NORTH_EAST = 1;
private static final int EAST = 2;
private static final int SOUTH_EAST = 3;
private static final int SOUTH = 4;
private static final int SOUTH_WEST = 5;
private static final int WEST = 6;
private static final int NORTH_WEST = 7;
private int _rats_direction;

private int _current_row;
private int _current_col;

public RobotRat(int rows, int cols){
super("RobotRat Control Panel");

_rows = rows;
_cols = cols;
_pen_position = UP;
_rats_direction = EAST;
_current_row = 0;
_current_col = 0;

floor = new boolean[_rows][_cols];

_panel1 = new JPanel();
_panel2 = new JPanel();

_label1 = new JLabel("Spaces:");
_textfield1 = new JTextField(10);

_button1 = new JButton("NW");
_button1.addActionListener(this);

_button2 = new JButton("N");
_button2.addActionListener(this);

_button3 = new JButton("NE");
_button3.addActionListener(this);

_button4 = new JButton("W");
_button4.addActionListener(this);

_button5 = new JButton("Toggle Pen");
_button5.addActionListener(this);

_button6 = new JButton("E");
_button6.addActionListener(this);

_button7 = new JButton("SW");
_button7.addActionListener(this);

_button8 = new JButton("S");
_button8.addActionListener(this);

_button9 = new JButton("SE");
_button9.addActionListener(this);


_panel1.add(_label1);
_panel1.add(_textfield1);

_panel2.setLayout(new GridLayout(3,3,0,0));
_panel2.add(_button1);
_panel2.add(_button2);
_panel2.add(_button3);
_panel2.add(_button4);
_panel2.add(_button5);
_panel2.add(_button6);
_panel2.add(_button7);
_panel2.add(_button8);
_panel2.add(_button9);

this.getContentPane().setLayout(new GridLayout(2,1,0,0));
this.getContentPane().add(_panel1);
this.getContentPane().add(_panel2);
this.setSize(300, 300);
this.setLocation(200, 200);
this.show();



} // end constructor





public void actionPerformed(ActionEvent ae){

if(ae.getActionCommand().equals("NW")){
moveNorthWest();
}else if(ae.getActionCommand().equals("N")){
moveNorth();
}else if(ae.getActionCommand().equals("NE")){
moveNorthEast();
}else if(ae.getActionCommand().equals("Toggle Pen")){
togglePen();
}


} // end actionPerformed()


private void printFloor(){
for(int i = 0; i < floor.length; i++){
for(int j = 0; j < floor[i].length; j++){
if( floor[i][j] ){
System.out.print("-");
}else {
System.out.print("0");
}
}
System.out.println();
}

}


private void initializeFloor(){
for(int i = 0; i < floor.length; i++){
for(int j = 0; j < floor[i].length; j++){
floor[i][j] = false;
}
}
}


private void moveNorthWest(){
System.out.println("North West!");
_rats_direction = NORTH_WEST;
move();
printFloor();
}

private void moveNorth(){
System.out.println("North!");
// you need to add program logic here to move rat
_rats_direction = NORTH;
move();
printFloor();
}

private void moveNorthEast(){
System.out.println("North East");
// you need to add program logic here to move rat
_rats_direction = NORTH_EAST;
move();
printFloor();
}

private void togglePen(){
switch(_pen_position){
case UP: _pen_position = DOWN;
System.out.println("Pen is now down");
break;
case DOWN: _pen_position = UP;
System.out.println("Pen is now up");
break;
default : _pen_position = UP;
}
}


private void move(){
int spaces_to_move = 0;

try{

spaces_to_move = Integer.parseInt(_textfield1.getText());
}catch(NumberFormatException nfe){
System.out.println("Invalid integer value!");
System.out.println("spaces to move is being set to 1.");
spaces_to_move = 1;
_textfield1.setText("1");
}

switch(_pen_position){

case UP: switch(_rats_direction){

case NORTH : System.out.println("Pen Up - Move North");
break;
case NORTH_EAST: System.out.println("Pen Up - Move North East");
break;
case EAST: System.out.println("Pen Up - Move East");
break;
case SOUTH_EAST: System.out.println("Pen Up - Move South East");
break;
case SOUTH: System.out.println("Pen Up - Move South");
break;
case SOUTH_WEST: System.out.println("Pen Up - Move South West");
break;
case WEST: System.out.println("Pen Up - Move West");
break;
case NORTH_WEST: System.out.println("Pen Up - Move North West");

}
break;
case DOWN:switch(_rats_direction){

case NORTH :
break;
case NORTH_EAST:
break;
case EAST:
break;
case SOUTH_EAST:
break;
case SOUTH:
break;
case SOUTH_WEST:
break;
case WEST:
break;
case NORTH_WEST:

}
} // end outer switch


} // end move() method



public static void main(String[] args){

System.out.println("RobotRat lives!");
RobotRat rr = new RobotRat(20, 20);
}




}
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Gregori,

We'd be happy to HELP, but nobody here is going to DO it for you. posting 250 lines of code without asking anything won't get you far around here.

if you have a SPECIFIC question, feel free to ask that, and post the relevant section of the code. PLEASE use code tags around your code to preserve the formatting. Make is easy for us to help you.

What specific problem are you having? what is it doing different than you expected?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This topic is not relevant to SCJP either. I suggest Java In General (Beginner)
 
Gregori Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sorry. Here is what is happening. This code brings up a control panel with seven direction buttons ( North, South, East, South East, North West...)it also has a toggle pen button. Above the button panel there is a panel with a text field to enter number of spaces to move. The dos window also appears with the message "robot rat lives" .The boolean array is 20x20. I need to set this up so that when an integer is entered in the text field and the directional button is pushed the "rat" will move position in the array without extending past the limits of the array. I also need to have this set so if the pen is toggled down the track of the rat is out put. Is that of any help???

private void moveNorthWest(){
System.out.println("North West!");
_rats_direction = NORTH_WEST;
move();
printFloor();
}

private void moveNorth(){
System.out.println("North!");
// MY PROFESSOR SAID THAT I need to add program logic here to move rat
_rats_direction = NORTH;
move();
printFloor();
}

private void moveNorthEast(){
System.out.println("North East");
// MY PROFESSOR SAID THAT I need to add program logic here to move rat
_rats_direction = NORTH_EAST;
move();
printFloor();
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing this thread, as it is a cross-post from the intermediate forum. (Please don't cross-post.)
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic