You are viewing our Forum Archives. To view or take place in current topics click here.
Java Programming! Urgent help! [Probably simple]
Posted:
Java Programming! Urgent help! [Probably simple]Posted:
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Hi there, I am looking some simple help, I have to code a program for my assignment and have been doing well up until this point, now I seem to be having a slight problem, this is the last part that I have to do to complete it, due tomorrow much help appreciated.
What I'm asking.
I am simply asking how I make and average amount of moves from the total moves taken by each 'robot' So basically my program comes from becker and is a robot package, I have to make the robot escape the room from where ever it starts (I have done this already) No I have moves++; this is to add up all the amount of moves that each robot takes so everytime a robot moves it will add one onto the moves, this is fine. Now what I need to do is make it so that all the robots moves are added up and then divided by the amount to robots (to give the average) This then needs to be printed out into the console.
So basically all I am asking is a way to add up all the total moves from each robot (eg: robot1 takes 20 moves and robot2 take 4 and robot3 takes 8, this will add up a total to become 32, with this it will then be divided by 3 (amount of robots) to give me the average, which would be 10.66* this is the answer that needs to be printed out into the console)
My code is in the spoilers:
//THIS IS THE ROBOT CLASS
package me.Stuart.Uni;
import becker.robots.*;
/**
* @(#)EscapeBot.java
*
* EscapeBot application
*
* @author Stuart Haire
* @version 1.00 2013/3/26
*/
public class EscapeBot extends RobotSE
{
//No new attributes
public EscapeBot(City aCity, int aStreet, int anAvenue, Direction aDirection)
{ super(aCity, aStreet, anAvenue, aDirection);
}
//The default moves set as 0.
private int moves = 0;
public void escape()
{
this.findWall(); //Looks for wall first
while(!this.canPickThing()) //Until it is on the thing it will keep going round the wall looking for exit.
{
this.followWall();
}
System.out.println("Number of moves: " + moves); //Prints out amount of moves taken.
}
//findWall finds the wall straight in front of the robot so that it can follow the wall around.
private void findWall()
{
while(this.frontIsClear() && !this.canPickThing())
{
this.move();
moves++; //Counts the moves
}
}
//Checks the wall to find exit.
public void followWall()
{
while(!this.frontIsClear() && !this.canPickThing()) //Check if front is blocked and if it CANT pick a thing
{
this.turnLeft();
while(this.frontIsClear() && !this.canPickThing()) //Checks if the front is clear and it CANT pick a thing
{
this.move();
moves ++; //Counts the moves
if(!this.canPickThing())
{
this.turnRight();
}
}
}
}
}
//THIS IS THE MAIN CLASS
package me.Stuart.Uni;
import becker.robots.*;
/**
* @(#)EscapeBot.java
*
* EscapeBot main
*
* @author Stuart Haire
* @version 1.00 2013/3/26
*/
public class EscapeRoom
{
public static void main(String[] args)
{
// Set up the initial situation
City belfast = new City(-1,-1,10,10);
Thing t2 = new Thing(belfast, -1, 1);
Wall w1 = new Wall(belfast, 0, 0, Direction.NORTH);
// Wall w2 = new Wall(belfast, 0, 1, Direction.NORTH);
Wall w3 = new Wall(belfast, 0, 2, Direction.NORTH);
Wall w4 = new Wall(belfast, 0, 3, Direction.NORTH);
Wall w5 = new Wall(belfast, 0, 0, Direction.WEST);
Wall w6 = new Wall(belfast, 1, 0, Direction.WEST);
Wall w7 = new Wall(belfast, 2, 0, Direction.WEST);
Wall w8 = new Wall(belfast, 2, 0, Direction.SOUTH); //Remove to make larger
Wall w9 = new Wall(belfast, 2, 1, Direction.SOUTH); //Remove to make larger
Wall w10 = new Wall(belfast, 2, 2, Direction.SOUTH); //Remove to make larger
Wall w11 = new Wall(belfast, 2, 3, Direction.SOUTH); //Remove to make larger
Wall w12 = new Wall(belfast, 2, 3, Direction.EAST); //Remove to make larger
Wall w13 = new Wall(belfast, 1, 3, Direction.EAST); //Remove to make larger
Wall w14 = new Wall(belfast, 0, 3, Direction.EAST); //Remove to make larger
// Wall larger1 = new Wall(belfast, 0, 4, Direction.NORTH);
// Wall larger2 = new Wall(belfast, 0, 4, Direction.EAST);
// Wall larger3 = new Wall(belfast, 1, 4, Direction.EAST);
// Wall larger4 = new Wall(belfast, 2, 4, Direction.EAST);
// Wall larger5 = new Wall(belfast, 3, 4, Direction.EAST);
// Wall larger6 = new Wall(belfast, 3, 4, Direction.SOUTH);
// Wall larger7 = new Wall(belfast, 3, 3, Direction.SOUTH);
// Wall larger8 = new Wall(belfast, 3, 2, Direction.SOUTH);
// Wall larger9 = new Wall(belfast, 3, 1, Direction.SOUTH);
// Wall larger10 = new Wall(belfast, 3, 0, Direction.SOUTH);
// Wall larger11 = new Wall(belfast, 3, 0, Direction.WEST);
//This instance of RobotSE should be replaced by an instance
//of EscapeBot.
EscapeBot karel = new EscapeBot(belfast, 2, 3, Direction.EAST);
//Then call a method so that the robot escapes
karel.escape();
int r = 0;
for (int i=1; i<=12; i++) //For loop to keep spawning in new robots until the amount is reached.
{
int x = 0;
int y = 0;
Direction d = Direction.NORTH;
//Randomising Avenue
if(Math.random() > 0)
x = 0;
if(Math.random() > 0.25)
x = 1;
if(Math.random() > 0.5)
x = 2;
if(Math.random() > 0.75)
x = 3;
//Randomising Street
if(Math.random() > 0)
y = 0;
if(Math.random() > 0.33)
y = 1;
if(Math.random() > 0.66)
y = 2;
//Randomising Direction
if(Math.random() > 0)
d = Direction.NORTH;
if(Math.random() > 0.25)
d = Direction.EAST;
if(Math.random() > 0.5)
d = Direction.SOUTH;
if(Math.random() > 0.75)
d = Direction.WEST;
//The creation of the new robots each time.
EscapeBot Stuart = new EscapeBot(belfast, y, x, d);
Stuart.escape();
}
}//main
}//class
What I'm asking.
I am simply asking how I make and average amount of moves from the total moves taken by each 'robot' So basically my program comes from becker and is a robot package, I have to make the robot escape the room from where ever it starts (I have done this already) No I have moves++; this is to add up all the amount of moves that each robot takes so everytime a robot moves it will add one onto the moves, this is fine. Now what I need to do is make it so that all the robots moves are added up and then divided by the amount to robots (to give the average) This then needs to be printed out into the console.
So basically all I am asking is a way to add up all the total moves from each robot (eg: robot1 takes 20 moves and robot2 take 4 and robot3 takes 8, this will add up a total to become 32, with this it will then be divided by 3 (amount of robots) to give me the average, which would be 10.66* this is the answer that needs to be printed out into the console)
My code is in the spoilers:
//THIS IS THE ROBOT CLASS
package me.Stuart.Uni;
import becker.robots.*;
/**
* @(#)EscapeBot.java
*
* EscapeBot application
*
* @author Stuart Haire
* @version 1.00 2013/3/26
*/
public class EscapeBot extends RobotSE
{
//No new attributes
public EscapeBot(City aCity, int aStreet, int anAvenue, Direction aDirection)
{ super(aCity, aStreet, anAvenue, aDirection);
}
//The default moves set as 0.
private int moves = 0;
public void escape()
{
this.findWall(); //Looks for wall first
while(!this.canPickThing()) //Until it is on the thing it will keep going round the wall looking for exit.
{
this.followWall();
}
System.out.println("Number of moves: " + moves); //Prints out amount of moves taken.
}
//findWall finds the wall straight in front of the robot so that it can follow the wall around.
private void findWall()
{
while(this.frontIsClear() && !this.canPickThing())
{
this.move();
moves++; //Counts the moves
}
}
//Checks the wall to find exit.
public void followWall()
{
while(!this.frontIsClear() && !this.canPickThing()) //Check if front is blocked and if it CANT pick a thing
{
this.turnLeft();
while(this.frontIsClear() && !this.canPickThing()) //Checks if the front is clear and it CANT pick a thing
{
this.move();
moves ++; //Counts the moves
if(!this.canPickThing())
{
this.turnRight();
}
}
}
}
}
//THIS IS THE MAIN CLASS
package me.Stuart.Uni;
import becker.robots.*;
/**
* @(#)EscapeBot.java
*
* EscapeBot main
*
* @author Stuart Haire
* @version 1.00 2013/3/26
*/
public class EscapeRoom
{
public static void main(String[] args)
{
// Set up the initial situation
City belfast = new City(-1,-1,10,10);
Thing t2 = new Thing(belfast, -1, 1);
Wall w1 = new Wall(belfast, 0, 0, Direction.NORTH);
// Wall w2 = new Wall(belfast, 0, 1, Direction.NORTH);
Wall w3 = new Wall(belfast, 0, 2, Direction.NORTH);
Wall w4 = new Wall(belfast, 0, 3, Direction.NORTH);
Wall w5 = new Wall(belfast, 0, 0, Direction.WEST);
Wall w6 = new Wall(belfast, 1, 0, Direction.WEST);
Wall w7 = new Wall(belfast, 2, 0, Direction.WEST);
Wall w8 = new Wall(belfast, 2, 0, Direction.SOUTH); //Remove to make larger
Wall w9 = new Wall(belfast, 2, 1, Direction.SOUTH); //Remove to make larger
Wall w10 = new Wall(belfast, 2, 2, Direction.SOUTH); //Remove to make larger
Wall w11 = new Wall(belfast, 2, 3, Direction.SOUTH); //Remove to make larger
Wall w12 = new Wall(belfast, 2, 3, Direction.EAST); //Remove to make larger
Wall w13 = new Wall(belfast, 1, 3, Direction.EAST); //Remove to make larger
Wall w14 = new Wall(belfast, 0, 3, Direction.EAST); //Remove to make larger
// Wall larger1 = new Wall(belfast, 0, 4, Direction.NORTH);
// Wall larger2 = new Wall(belfast, 0, 4, Direction.EAST);
// Wall larger3 = new Wall(belfast, 1, 4, Direction.EAST);
// Wall larger4 = new Wall(belfast, 2, 4, Direction.EAST);
// Wall larger5 = new Wall(belfast, 3, 4, Direction.EAST);
// Wall larger6 = new Wall(belfast, 3, 4, Direction.SOUTH);
// Wall larger7 = new Wall(belfast, 3, 3, Direction.SOUTH);
// Wall larger8 = new Wall(belfast, 3, 2, Direction.SOUTH);
// Wall larger9 = new Wall(belfast, 3, 1, Direction.SOUTH);
// Wall larger10 = new Wall(belfast, 3, 0, Direction.SOUTH);
// Wall larger11 = new Wall(belfast, 3, 0, Direction.WEST);
//This instance of RobotSE should be replaced by an instance
//of EscapeBot.
EscapeBot karel = new EscapeBot(belfast, 2, 3, Direction.EAST);
//Then call a method so that the robot escapes
karel.escape();
int r = 0;
for (int i=1; i<=12; i++) //For loop to keep spawning in new robots until the amount is reached.
{
int x = 0;
int y = 0;
Direction d = Direction.NORTH;
//Randomising Avenue
if(Math.random() > 0)
x = 0;
if(Math.random() > 0.25)
x = 1;
if(Math.random() > 0.5)
x = 2;
if(Math.random() > 0.75)
x = 3;
//Randomising Street
if(Math.random() > 0)
y = 0;
if(Math.random() > 0.33)
y = 1;
if(Math.random() > 0.66)
y = 2;
//Randomising Direction
if(Math.random() > 0)
d = Direction.NORTH;
if(Math.random() > 0.25)
d = Direction.EAST;
if(Math.random() > 0.5)
d = Direction.SOUTH;
if(Math.random() > 0.75)
d = Direction.WEST;
//The creation of the new robots each time.
EscapeBot Stuart = new EscapeBot(belfast, y, x, d);
Stuart.escape();
}
}//main
}//class
#2. Posted:
Status: Offline
Joined: Oct 26, 201014Year Member
Posts: 3,869
Reputation Power: 177
Status: Offline
Joined: Oct 26, 201014Year Member
Posts: 3,869
Reputation Power: 177
I don't know much Java, but this is a simple solution to solve.
Take the variable for the amount of turns for each robot, add the turns together and divide by the number of robots you have.
Psuedo code (Since I don't know Java):
Take the variable for the amount of turns for each robot, add the turns together and divide by the number of robots you have.
Psuedo code (Since I don't know Java):
numRobots = number of robots;
robot1 = number of turns for robot 1
robot2 = number of turns for robot 2
robot3 = number of turns for robot 3
average = (robot1 + robot2 + robot3) / numRobots
- 1useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
to Access the Move Counter on each Robot, make yourself a get function to return the Move variable in the Robot Object.
Create a TotalMoves variable in your main loop
Call the subroutine after the escape function in your main loop, to add up the total number of moves.
Then once the loop is complete, divide the TotalMoves Variable by the number of robots
Give that a go, I haven't tested but this should do the trick i hope!
public int getMoves(){
return moves;
}
Create a TotalMoves variable in your main loop
int TotalMoves = 0;
Call the subroutine after the escape function in your main loop, to add up the total number of moves.
//The creation of the new robots each time.
EscapeBot Stuart = new EscapeBot(belfast, y, x, d);
Stuart.escape();
TotalMoves = TotalMoves + Stuart.getMoves();
Then once the loop is complete, divide the TotalMoves Variable by the number of robots
double AverageMoves = TotalMoves / 12;
Give that a go, I haven't tested but this should do the trick i hope!
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Imp wrote
double AverageMoves = TotalMoves / 12;
Thanks very much it works perfectly to give me the total number of moves, the only problem is that went I put
System.out.println("Average Moves: " + AverageMoves);
at the end of my program it gives me: Average Moves: 0.0
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Status: Offline
Joined: Aug 25, 201113Year Member
Posts: 255
Reputation Power: 9
Sorry got it, I had the
double AverageMoves = TotalMoves / 12;
in the wrong place, I simply moved it to get it to work correctly.
Now I have one last question, I got total amount of moves to be 98, so when this is divided by 12 you get 8.16666667, so why does that answer display as 8.0 not 8.1 or rounded up to 8.2?
double AverageMoves = TotalMoves / 12;
in the wrong place, I simply moved it to get it to work correctly.
Now I have one last question, I got total amount of moves to be 98, so when this is divided by 12 you get 8.16666667, so why does that answer display as 8.0 not 8.1 or rounded up to 8.2?
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Stuartie wrote Sorry got it, I had the
double AverageMoves = TotalMoves / 12;
in the wrong place, I simply moved it to get it to work correctly.
Now I have one last question, I got total amount of moves to be 98, so when this is divided by 12 you get 8.16666667, so why does that answer display as 8.0 not 8.1 or rounded up to 8.2?
Try doing this
DecimalFormat MoveFormat = new DecimalFormat("#.##");
System.out.println("Average Moves: " + MoveFormat.format(AverageMoves));
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.