Guest User

Untitled

a guest
Oct 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. import java.util.*;
  2. public class mahipatb_BugSimulation {
  3.  
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. Scanner reader = new Scanner (System.in);
  7. System.out.println("Enter the starting position of the bug");
  8. int z = reader.nextInt();
  9. int start = 1;
  10.  
  11. mahipatb_Bug b1 = new mahipatb_Bug();
  12.  
  13. b1.setPosition(z);
  14. b1.setDirection("right");
  15. System.out.println( "Creating Bug at position " + b1.getPosition() + " facing " + b1.getDirection() );
  16. System.out.println("How many times should the bug move?");
  17. int x = reader.nextInt();
  18.  
  19. b1.setPosition(x);
  20.  
  21. while (x > 0)
  22. {
  23. System.out.println("Bug Moved");
  24. x--;
  25. }
  26. System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
  27. System.out.println("How many times should the bug turn?");
  28. int l = reader.nextInt();
  29. while (l > 0)
  30. {
  31. System.out.println("Bug Turned");
  32. l--;
  33. }
  34.  
  35.  
  36. System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
  37. System.out.println("How many times should the bug move?");
  38. x = reader.nextInt();
  39.  
  40. b1.setPosition(x);
  41.  
  42. while (x > 0)
  43. {
  44. System.out.println("Bug Moved");
  45. x--;
  46. }
  47. System.out.println( "Bug is at position " + b1.move() + " facing " + b1.getDirection() );
  48.  
  49. }
  50. }
  51.  
  52.  
  53.  
  54. public class mahipatb_Bug {
  55. private int position;
  56. private String direction;
  57.  
  58.  
  59. public mahipatb_Bug()
  60.  
  61. {
  62. position = 0;
  63. direction = " ";
  64. }
  65. public mahipatb_Bug(int w,String z)
  66.  
  67. {
  68. position = w;
  69.  
  70. direction = z;
  71. }
  72.  
  73.  
  74.  
  75. ; public int getPosition()
  76. {
  77.  
  78. return position;
  79. }
  80. public String getDirection()
  81. {
  82.  
  83.  
  84. return direction;
  85. }
  86. //setters - mutator
  87. public void setPosition(int newPost)
  88. {
  89. position = newPost;
  90. }
  91. public void setDirection( String x)
  92. {
  93. direction = x;
  94. }
  95. public int move() {
  96. int move = 0;
  97. if (position%2 == 0)
  98. {
  99. move =move - position;
  100. }
  101. else
  102. {
  103.  
  104. move =move + position;
  105. }
  106. position = move;
  107. return move;
  108.  
  109.  
  110. }
  111. public void turn(int i)
  112. {
  113. if (i%2 == 0)
  114. {
  115. direction = "left";
  116. }
  117. else
  118. {
  119. direction = "right";
  120. }
  121.  
  122.  
  123. }
  124. }
Add Comment
Please, Sign In to add comment