鱼C论坛

 找回密码
 立即注册
查看: 521|回复: 3

[已解决]Java练习题

[复制链接]
发表于 2023-3-29 21:37:52 | 显示全部楼层 |阅读模式
60鱼币
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.

以上为要求。
有简单注释的优先,谢谢。
最佳答案
2023-3-29 21:37:53
该Java程序首先定义了Entity和World类,然后在主函数中创建了两个World对象(Middle Earth和Middle Ages),将三个实体(Frodo,Gandalf和Bilbo)添加到Middle Earth,并根据用户输入移动实体。

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

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

  1. import java.util.Scanner;
  2. import java.util.Random;

  3. class Entity {
  4.     String name;
  5.     int x;
  6.     int y;

  7.     Entity(String name, int x, int y) {
  8.         this.name = name;
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12. }

  13. class World {
  14.     String name;
  15.     Entity[] entities;
  16.     int counter;

  17.     World(String name, int capacity) {
  18.         this.name = name;
  19.         this.entities = new Entity[capacity];
  20.         this.counter = 0;
  21.     }

  22.     // 添加实体
  23.     void addEntity(Entity entity) {
  24.         if (counter < entities.length) {
  25.             entities[counter++] = entity;
  26.         } else {
  27.             System.out.println("Middle Earth has reached the maximum capacity!");
  28.         }
  29.     }

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

  37.     // 查找实体
  38.     int findEntity(String name) {
  39.         for (int i = 0; i < counter; i++) {
  40.             if (entities[i].name.equals(name)) {
  41.                 return i;
  42.             }
  43.         }
  44.         return -1;
  45.     }

  46.     // 移动实体
  47.     void moveEntity(String name, int newX, int newY) {
  48.         int index = findEntity(name);
  49.         if (index != -1) {
  50.             entities[index].x = newX;
  51.             entities[index].y = newY;
  52.         } else {
  53.             System.out.println("The entity is not exist");
  54.         }
  55.     }

  56.     // 显示实体
  57.     void displayEntities() {
  58.         System.out.println("#" + name);
  59.         System.out.println("#The number of the entities: " + counter);
  60.         for (int i = 0; i < counter; i++) {
  61.             System.out.println("#Entity name: " + entities[i].name +
  62.                 ", the position is (" + entities[i].x + "," + entities[i].y + ")");
  63.         }
  64.     }
  65. }

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

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

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

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

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

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

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

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

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

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

  98.             // 显示两个世界的实体
  99.             middleEarth.displayEntities();
  100.             middleAges.displayEntities();
  101.     } else {
  102.         System.out.println("The entity is not exist!");
  103.         middleEarth.displayEntities();
  104.         middleAges.displayEntities();
  105.     }

  106.     scanner.close();
  107. }
  108. }
复制代码
搜狗高速浏览器截图20230329213611.png

最佳答案

查看完整内容

该Java程序首先定义了Entity和World类,然后在主函数中创建了两个World对象(Middle Earth和Middle Ages),将三个实体(Frodo,Gandalf和Bilbo)添加到Middle Earth,并根据用户输入移动实体。 程序会在两个世界之间移动实体,如果实体存在,则将其添加到Middle Ages,然后从Middle Earth中删除。 在执行操作后,程序会显示两个世界的实体。如果实体不存在,则显示相应的错误消息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-3-29 21:37:53 | 显示全部楼层    本楼为最佳答案   
该Java程序首先定义了Entity和World类,然后在主函数中创建了两个World对象(Middle Earth和Middle Ages),将三个实体(Frodo,Gandalf和Bilbo)添加到Middle Earth,并根据用户输入移动实体。

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

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

  1. import java.util.Scanner;
  2. import java.util.Random;

  3. class Entity {
  4.     String name;
  5.     int x;
  6.     int y;

  7.     Entity(String name, int x, int y) {
  8.         this.name = name;
  9.         this.x = x;
  10.         this.y = y;
  11.     }
  12. }

  13. class World {
  14.     String name;
  15.     Entity[] entities;
  16.     int counter;

  17.     World(String name, int capacity) {
  18.         this.name = name;
  19.         this.entities = new Entity[capacity];
  20.         this.counter = 0;
  21.     }

  22.     // 添加实体
  23.     void addEntity(Entity entity) {
  24.         if (counter < entities.length) {
  25.             entities[counter++] = entity;
  26.         } else {
  27.             System.out.println("Middle Earth has reached the maximum capacity!");
  28.         }
  29.     }

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

  37.     // 查找实体
  38.     int findEntity(String name) {
  39.         for (int i = 0; i < counter; i++) {
  40.             if (entities[i].name.equals(name)) {
  41.                 return i;
  42.             }
  43.         }
  44.         return -1;
  45.     }

  46.     // 移动实体
  47.     void moveEntity(String name, int newX, int newY) {
  48.         int index = findEntity(name);
  49.         if (index != -1) {
  50.             entities[index].x = newX;
  51.             entities[index].y = newY;
  52.         } else {
  53.             System.out.println("The entity is not exist");
  54.         }
  55.     }

  56.     // 显示实体
  57.     void displayEntities() {
  58.         System.out.println("#" + name);
  59.         System.out.println("#The number of the entities: " + counter);
  60.         for (int i = 0; i < counter; i++) {
  61.             System.out.println("#Entity name: " + entities[i].name +
  62.                 ", the position is (" + entities[i].x + "," + entities[i].y + ")");
  63.         }
  64.     }
  65. }

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

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

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

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

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

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

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

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

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

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

  98.             // 显示两个世界的实体
  99.             middleEarth.displayEntities();
  100.             middleAges.displayEntities();
  101.     } else {
  102.         System.out.println("The entity is not exist!");
  103.         middleEarth.displayEntities();
  104.         middleAges.displayEntities();
  105.     }

  106.     scanner.close();
  107. }
  108. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-29 21:38:37 | 显示全部楼层
下面的图片是要求的实现效果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 08:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表