Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package ask6;
  2.  
  3. import java.util.Scanner;
  4. import java.util.ArrayList;
  5.  
  6. class Robot{
  7. private String name;
  8. private int x;
  9. private int y;
  10.  
  11. public Robot(){
  12. name = " ";
  13. x = 0;
  14. y = 0;
  15. }
  16.  
  17. public Robot(String name, int x, int y){
  18. this.name = name;
  19. this.x = x;
  20. this.y = y;
  21. }
  22.  
  23. public void setName(String name){
  24. this.name = name;
  25. }
  26.  
  27. public void setX(int x){
  28. this.x = x;
  29. }
  30.  
  31. public void setY(int y){
  32. this.y = y;
  33. }
  34.  
  35. public String getName(){
  36. return name;
  37. }
  38.  
  39. public int getX(){
  40. return x;
  41. }
  42.  
  43. public int getY(){
  44. return y;
  45. }
  46.  
  47. public void moveCoordinateΧ(int x){
  48. setX(getX()+x);
  49. if (this.x<0){
  50. this.x=0;
  51. }
  52. if (this.x>768){
  53. System.out.println("Cannot move");
  54. setX(getX()-x);
  55. }
  56. }
  57.  
  58. public void moveCoordinateY(int y){
  59. setY(getY()+y);
  60. if (this.y<0){
  61. this.y=0;
  62. }
  63. if (this.y>768){
  64. System.out.println("Cannot move");
  65. setY(getY()-y);
  66. }
  67. }
  68.  
  69. public void moveCoordinateΧΥ(int x, int y){
  70. moveCoordinateΧ(x);
  71. moveCoordinateY(y);
  72. }
  73.  
  74. public String printCoordinates() {
  75. return "Robot{" + "name=" + name + ", x=" + x + ", y=" + y + '}';
  76. }
  77. }
  78.  
  79. public class Ask6 {
  80.  
  81. public static void main(String[] args) {
  82. ArrayList<Robot> Robots = new ArrayList<Robot>();
  83.  
  84. Scanner input = new Scanner(System.in);
  85. int answer = 0;
  86.  
  87. do{
  88. System.out.print("1. Add new Robot" + "\n" + "2. Moving Robot in Position" + "\n" + "3. Print Robots" + "\n" + "4. Exit" + "\n");
  89. answer = input.nextInt();
  90.  
  91. if (answer==1){
  92. System.out.println("Robot name: ");
  93. Scanner putin = new Scanner(System.in);
  94. String name = putin.nextLine();
  95. System.out.println("x coordinate: ");
  96. int x = input.nextInt();
  97. System.out.println("y coordinate: ");
  98. int y = input.nextInt();
  99. Robot newRobot = new Robot(name, x, y);
  100. Robots.add(newRobot);
  101. }
  102.  
  103. else if(answer==2){
  104. System.out.println("Position: ");
  105. int pos = input.nextInt();
  106.  
  107. if(Robots.get(pos)!=null){
  108. System.out.println("x coordinate: ");
  109. int x = input.nextInt();
  110. System.out.println("y coordinate: ");
  111. int y = input.nextInt();
  112. Robots.get(pos).moveCoordinateΧ(x);
  113. Robots.get(pos).moveCoordinateY(y);
  114. }
  115. }
  116.  
  117. else if(answer==3){
  118. for(Robot x: Robots){
  119. System.out.println(x.printCoordinates());
  120. }
  121. }
  122.  
  123. }while (answer!=4);
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement