Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class mahipatb_BugSimulation {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner reader = new Scanner (System.in);
- System.out.println("Enter the starting position of the bug");
- int z = reader.nextInt();
- int start = 1;
- mahipatb_Bug b1 = new mahipatb_Bug();
- b1.setPosition(z);
- b1.setDirection("right");
- System.out.println( "Creating Bug at position " + b1.getPosition() + " facing " + b1.getDirection() );
- System.out.println("How many times should the bug move?");
- int x = reader.nextInt();
- b1.setPosition(x);
- while (x > 0)
- {
- System.out.println("Bug Moved");
- x--;
- }
- System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
- System.out.println("How many times should the bug turn?");
- int l = reader.nextInt();
- while (l > 0)
- {
- System.out.println("Bug Turned");
- l--;
- }
- System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
- System.out.println("How many times should the bug move?");
- x = reader.nextInt();
- b1.setPosition(x);
- while (x > 0)
- {
- System.out.println("Bug Moved");
- x--;
- }
- System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
- }
- }
- public class mahipatb_Bug {
- private int position;
- private String direction;
- public mahipatb_Bug()
- {
- position = 0;
- direction = " ";
- }
- public mahipatb_Bug(int w,String z)
- {
- position = w;
- direction = z;
- }
- ; public int getPosition()
- {
- return position;
- }
- public String getDirection()
- {
- return direction;
- }
- //setters - mutator
- public void setPosition(int newPost)
- {
- position = newPost;
- }
- public void setDirection( String x)
- {
- direction = x;
- }
- public int move() {
- int move = 0;
- if (position%2 == 0)
- {
- move =move - position;
- }
- else
- {
- move =move + position;
- }
- position = move;
- return move;
- }
- public void turn(int i)
- {
- if (i%2 == 0)
- {
- direction = "left";
- }
- else
- {
- direction = "right";
- }
- }
- }
Add Comment
Please, Sign In to add comment