帅哥小C啊 发表于 2023-3-29 21:37:52

Java练习题

You have to make sure that a World can contain up to 10 entities and that all the methods are implemented. You have to implement this association as an array and a counter as seen in the lecture. We will work with ArrayLists after the consolidation week.

When implementing the methods you should take into account the following assumptions for each one:
addEntity: Add the entity if the world has not reached the maximum capacity, otherwise print “Middle Earth has reached the maximum capacity!” .
deleteEntity: Delete the entity if there is an entity in that position.
findEntity: If the entity with that name is found return the position, otherwise -1 is returned
moveEntity: Move the entity if there is an entity with that name in the world, otherwise print “The entity is not exist”.

Week 4 Exercise 6:
Implement a program that creates two worlds called “Middle Earth” and “Middle Ages”.
The program should add three entities to “Middle Earth” called “Frodo”, “Gandalf” and “Bilbo” in different positions on the world (user should input the coordinates of each entity). Display all of the entities of the two worlds.
Input one entity name, move this entity to one random position (range from 0 to 100) if found, display all of the entities of the two worlds, then add this entity to world “Middle Ages”, then delete the entity from the world “Middle Earth”, print “The entity was found, moved, added, and deleted.” and display all of the entities of the two worlds again.If this entity name was not found, print “The entity is not exit!” and display all of the entities of the two worlds once again.

以上为要求。
有简单注释的优先,谢谢。

isdkz 发表于 2023-3-29 21:37:53

该Java程序首先定义了Entity和World类,然后在主函数中创建了两个World对象(Middle Earth和Middle Ages),将三个实体(Frodo,Gandalf和Bilbo)添加到Middle Earth,并根据用户输入移动实体。

程序会在两个世界之间移动实体,如果实体存在,则将其添加到Middle Ages,然后从Middle Earth中删除。

在执行操作后,程序会显示两个世界的实体。如果实体不存在,则显示相应的错误消息。

import java.util.Scanner;
import java.util.Random;

class Entity {
    String name;
    int x;
    int y;

    Entity(String name, int x, int y) {
      this.name = name;
      this.x = x;
      this.y = y;
    }
}

class World {
    String name;
    Entity[] entities;
    int counter;

    World(String name, int capacity) {
      this.name = name;
      this.entities = new Entity;
      this.counter = 0;
    }

    // 添加实体
    void addEntity(Entity entity) {
      if (counter < entities.length) {
            entities = entity;
      } else {
            System.out.println("Middle Earth has reached the maximum capacity!");
      }
    }

    // 删除实体
    void deleteEntity(int index) {
      if (index >= 0 && index < counter) {
            System.arraycopy(entities, index + 1, entities, index, counter - index - 1);
            counter--;
      }
    }

    // 查找实体
    int findEntity(String name) {
      for (int i = 0; i < counter; i++) {
            if (entities.name.equals(name)) {
                return i;
            }
      }
      return -1;
    }

    // 移动实体
    void moveEntity(String name, int newX, int newY) {
      int index = findEntity(name);
      if (index != -1) {
            entities.x = newX;
            entities.y = newY;
      } else {
            System.out.println("The entity is not exist");
      }
    }

    // 显示实体
    void displayEntities() {
      System.out.println("#" + name);
      System.out.println("#The number of the entities: " + counter);
      for (int i = 0; i < counter; i++) {
            System.out.println("#Entity name: " + entities.name +
                ", the position is (" + entities.x + "," + entities.y + ")");
      }
    }
}

public class Main {
    public static void main(String[] args) {
      World middleEarth = new World("Middle Earth", 10);
      World middleAges = new World("Middle Ages", 10);

      Scanner scanner = new Scanner(System.in);
      Random random = new Random();

      // 添加三个实体到Middle Earth
      for (String name : new String[]{"Frodo", "Gandalf", "Bilbo"}) {
            System.out.print("Enter the coordinates of the entity " + name + " (x, y):");
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            middleEarth.addEntity(new Entity(name, x, y));
      }

      // 显示两个世界的实体
      middleEarth.displayEntities();
      middleAges.displayEntities();

      System.out.print("Enter one entity name and move this entity to one random position: ");
      String entityName = scanner.next();

      // 移动实体
      int index = middleEarth.findEntity(entityName);
      if (index != -1) {
            int newX = random.nextInt(101);
            int newY = random.nextInt(101);
            middleEarth.moveEntity(entityName, newX, newY);

            // 显示两个世界的实体
            middleEarth.displayEntities();
            middleAges.displayEntities();

            // 添加实体到Middle Ages
            middleAges.addEntity(new Entity(entityName, newX, newY));

            // 删除实体从Middle Earth
            middleEarth.deleteEntity(index);

            System.out.println(entityName + " found, moved, and deleted.");

            // 显示两个世界的实体
            middleEarth.displayEntities();
            middleAges.displayEntities();
    } else {
      System.out.println("The entity is not exist!");
      middleEarth.displayEntities();
      middleAges.displayEntities();
    }

    scanner.close();
}
}

帅哥小C啊 发表于 2023-3-29 21:38:37

下面的图片是要求的实现效果。

帅哥小C啊 发表于 2023-3-29 21:50:08

帅哥小C啊 发表于 2023-3-29 21:38
下面的图片是要求的实现效果。

还有一句补充
You have to implement this association as an array and a counter as seen in the lecture. We will work with ArrayLists after the consolidation week.
页: [1]
查看完整版本: Java练习题