Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ask6;
- import java.util.Scanner;
- import java.util.ArrayList;
- class Robot{
- private String name;
- private int x;
- private int y;
- public Robot(){
- name = " ";
- x = 0;
- y = 0;
- }
- public Robot(String name, int x, int y){
- this.name = name;
- this.x = x;
- this.y = y;
- }
- public void setName(String name){
- this.name = name;
- }
- public void setX(int x){
- this.x = x;
- }
- public void setY(int y){
- this.y = y;
- }
- public String getName(){
- return name;
- }
- public int getX(){
- return x;
- }
- public int getY(){
- return y;
- }
- public void moveCoordinateΧ(int x){
- setX(getX()+x);
- if (this.x<0){
- this.x=0;
- }
- if (this.x>768){
- System.out.println("Cannot move");
- setX(getX()-x);
- }
- }
- public void moveCoordinateY(int y){
- setY(getY()+y);
- if (this.y<0){
- this.y=0;
- }
- if (this.y>768){
- System.out.println("Cannot move");
- setY(getY()-y);
- }
- }
- public void moveCoordinateΧΥ(int x, int y){
- moveCoordinateΧ(x);
- moveCoordinateY(y);
- }
- public String printCoordinates() {
- return "Robot{" + "name=" + name + ", x=" + x + ", y=" + y + '}';
- }
- }
- public class Ask6 {
- public static void main(String[] args) {
- ArrayList<Robot> Robots = new ArrayList<Robot>();
- Scanner input = new Scanner(System.in);
- int answer = 0;
- do{
- System.out.print("1. Add new Robot" + "\n" + "2. Moving Robot in Position" + "\n" + "3. Print Robots" + "\n" + "4. Exit" + "\n");
- answer = input.nextInt();
- if (answer==1){
- System.out.println("Robot name: ");
- Scanner putin = new Scanner(System.in);
- String name = putin.nextLine();
- System.out.println("x coordinate: ");
- int x = input.nextInt();
- System.out.println("y coordinate: ");
- int y = input.nextInt();
- Robot newRobot = new Robot(name, x, y);
- Robots.add(newRobot);
- }
- else if(answer==2){
- System.out.println("Position: ");
- int pos = input.nextInt();
- if(Robots.get(pos)!=null){
- System.out.println("x coordinate: ");
- int x = input.nextInt();
- System.out.println("y coordinate: ");
- int y = input.nextInt();
- Robots.get(pos).moveCoordinateΧ(x);
- Robots.get(pos).moveCoordinateY(y);
- }
- }
- else if(answer==3){
- for(Robot x: Robots){
- System.out.println(x.printCoordinates());
- }
- }
- }while (answer!=4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement