Java Lab 24: Robot Delivery SimulationJava

华为OD机试真题 2026.11.4. 新系统 【河流水系流量评估】 华为OD机试真题 新系统【河流水系流量评估】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题 【河流水系流量评估】多语言题解。 阅读详情

Java Python Java Lab 24: Robot Delivery Simulation

This lab has been designed to give you practice using threads and synchronization.

Problem statement: This application will simulate a system of autonomous robots delivering packages. The central system, simulating a warehouse, will create Parcel objects and place them on a queue for delivery, where each parcel will have a pseudo-random delivery time to simulate the time it would take to deliver this package – that is, the distance from the warehouse to the parcel's destination. Each robot will take a parcel from the queue and "deliver" it by waiting the delivery time before returning to the queue for another parcel. Each robot will have a battery life (set at 100 time units): it can only operate for that long until it needs to be recharged (which this app won't simulate – once the robot's battery dies, that's it –   but to keep things simple, the robot will magically deliver the last package). When all the robots' batteries die, the simulation is over and the statistics of this run of the simulation are printed.

Figure 1 shows one run of the program: the user chose 2 robots. Each column shows the parcel # that a robot delivered and the delivery time for that parcel. Here, Robot 0 delivers parcels 0, 3, 5, 6, 7, 9, 12, and 13 before its battery dies. Note that it exceeded its battery life (100) by 11 units – that’s okay, because again, we’re assuming the last package (#13, requiring 20 time units) gets delivered. Robot 1 delivers parcels 1, 2, 4, 8, 10, 11, and 14 before its battery died. Again, Robot 1 exceeded its battery life by 17 units, delivering package #14 by magic. The interleaving should make sense: while Robot 0 was out delivering parcel 0 (which took 22 time units), Robot 1 delivered parcel 1 (which took 14) and picked up parcel 2 (12); while parcel 2 was in transit, Robot 0 picked up parcel 3, and so on. The warehouse thread placed those 15 packages, plus 10 more that did not get delivered, on the queue.

Figure 2 shows a run using 3 robots. After you code the missing parts, try running your code with 2 robots several times; of course, when you run your program, you’ll get different answers, because of the random parcel timing. The try it with 3, 4, and 5 robots.

Design: The main program will prompt the user for the number of robots to create, call setup() to create the data structures, the robots and their threads, and start the robot threads. Main will then create the warehouse simulation thread. That thread will new-up the data structures required for the simulation. To give you practice with four kinds of synchronization tools, you'll use the following. A thread-safe queue (BlockingQueue<>) will be used for parcel storage; the warehouse will create

Parcel objects (which contain a random time-to-deliver value) and place them on the queue.  Use offer() and take() to put and remove parcel objects. Robots will remove parcels from the queue and wait for the time-to-deliver amount to simulated deliver. This queue will be a shared structure among the warehouse and robots, so all will keep a reference to the same object. The synchronized Stats class (all of its methods are tagged with synchronized) will also be shared;&dai 写Java Lab 24: Robot Delivery SimulationJava nbsp;it will keep track of the number of parcels delivered by each robot, the total delivery time for the parcels it delivered, and the actual time used by the robot. You will need to make its methods synchronized to make this data thread-safe. A static AtomicInteger counter will be shared as a static global item. It will be initialized to the number of robots. As each robot's battery dies, the counter will be decremented. The warehouse will stop producing parcels when the counter reaches zero. To make sure that the robots share  the screen without problems, synchronize the code block that prints their delivery message on the stats object.

Design

Class Lab24Main:

main(): prompt for the number of robots; print a header for the robot output; call setup(), and create and start the warehouse thread. ALREADY CODED.

setup(): initialize the queue, stats, and counter objects; create the robot and thread arrays; create the robots and threads; start the robot threads. MOSTLY CODED – NEEDS FINAL LOOP.

run(): this is the warehouse thread. Until the counter reaches 0, it will create a new Parcel object every five time units (by calling Thread.sleep(5) before creating the next parcel) and place the parcel on the queue. When the loop is done (all robots have finished), join all the threads, then call printStats().  YOU MUST CODE THE RUN LOOP.

printStats(): displays a chart of the robot and warehouse statistics. See Figures 1 and 2. ALREADY CODED.

Class Parcel: ALREADY CODED.

Parcel( ): initializes the parcels' id and deliveryTime

getDeliveryTime(), getId(): getters.

Class Robot:

Robot(BlockingQueue<Parcel>, Stats): sets the references to the shared data and sets the id. ALREADY CODED.

run( ): initialize beginTime; then loop: while it's battery is still charged (greater than zero), do the following:

- get a parcel from the queue,

- Thread.sleep( ) for the parcel's delivery time,

- decrement the battery, print the parcel delivery information (see Figures 1 and 2),

- print the delivery message, synchronized on stats – this is ALREADY CODED.

- add to the stats for this robot.

When the battery loop is done – we'll assume that the last package actually was delivered, even if technically the battery ran out during delivery (say it has a bit of reserve power) – set endTime; set the stats' running time for this robot, then decrement the AtomicInteger counter by one.

Class Stats: ALREADY CODED.

Stats(int numberOfRobots) : new up the data stuctures.

synchronized putParcel(int robotNumber): increment the parcel counter for this robot.

synchronized putTime(int robotNumber, int time): increment the time counter for this robot by time (sums all parcel times for this robot)

synchronized putRobotTime(int robotNumber, long time): set the robot's running time

synchronized getParcel(int robotNumber), getTimes(int robotNumber), getRobotTimes(int robotNumber): getters for this robot's parcel count, total parcel delivery time, robot running time.

Deliverable: Zip up *all* the .java files as <your_andrew_id>_lab24.zip         

SpringBoot+Vue+MySQL 高校学科竞赛平台 完整可运行版(源码+文档) 本项目基于SpringBoot+Vue+MySQL构建高校学科竞赛管理平台,覆盖学生、教师、领队教师、管理员四大角色,实现竞赛发布、报名、刷题、评分、公示等全流程闭环管理。系统架构清晰,技术主流,配套完整源码、数据库脚本、万字论文、开题报告、答辩PPT及部署文档,支持一键运行,适用于毕业设计、课程设计及院系二次开发,真正实现“开箱即用”。 阅读详情

相关推荐

磁带库Scalar i6000界面基本操作步骤

(注:磁带库的前面的门不要打开,日常维护:主要就是简单看下磁带库的状态;绿色代表正常;黄色代表报警但不影响磁带库正常使用;红色代表报警,不能正常使用,及时和厂家联系报修。) 一、磁带库管理页面 1、首先,磁带库前面(具体作用如图) 2、查看状态和管理页面 3、管理页面登陆。用户名:service 密码:xxxxxx 4、磁带机背面 二、电脑连接磁带库进入管理页面 1.用网线把电脑和磁带库的维护接口(磁带库背面)进行连接 2.电脑环境:JAVA。把电脑的IP设置成自...

杀神lwz的博客 5359

InnoDB 内存结构总览:Change Buffer、AHI 与 Log Buffer

本文系统梳理InnoDB内存结构中的ChangeBuffer、Adaptive Hash Index(AHI)与LogBuffer。ChangeBuffer缓存二级索引页不在内存时的变更,减少随机IO,仅在页加载时合并;聚簇索引修改立即执行,因数据本身不可延后。AHI基于高频等值查询自动构建哈希索引,加速定位,仅支持等值操作。LogBuffer为redo log内存缓冲,配合磁盘日志实现崩溃恢复。三者均服务于性能优化,其中ChangeBuffer与AHI共享BufferPool空间,而LogBuffer独立

bksczm的博客 292

Comsol 基于局部谐振压电材料的可调水下低频吸声

它通过建立基于LRPM的水下吸声理论模型,从有效材料的角度分析可调吸声特性和吸声机理,进一步提高吸声性能。该研究发现,薄的LRPM层可以在目标低频范围内实现出色的吸声效果,而这个目标低频可以通过操纵谐振分流电路进行主动调谐。通过引入局部谐振压电超材料和可调谐的谐振分流电路,研究人员能够有效地控制水下低频吸声特性,并且在吸声带宽方面取得了显著的改进。通过利用薄的活性超材料层,LRPM在水声吸声方面展现出潜力,并且通过操纵谐振分流电路,可以实现对吸声特性的精细控制。

weixin_45382232的博客 786

V8 机制、手写核心函数与 TypeScript 类型体操

或固定初始化(如数组先定长、对象先给全字段)保持类型与形状稳定;这些都比"换框架"更基础。只能处理纯数据(丢函数、Date、RegExp、循环引用报错)。生产级深拷贝要处理:原始值短路、循环引用、V8(Chrome / Node 的 JS 引擎)不是"解释一行跑一行"那么简单,它用。图:V8 执行流程——解释执行 + 热点优化,优化失败会 Deopt 回退。JS 内存自动管理,但"自动"不等于"不会漏"。是"固定间隔最多执行一次"。手写 Promise 是"懂异步"的试金石。是"停下后才执行最后一次",

qq_38468358的博客 349

云计算系统等保测评对象和指标选取

参考GBT22239-2019《基本要求》附录D 云计算应用场景说明。简要理解下图,主要是云计算系统安全保护责任分担原则和云服务模式适用性原则,指导后续的测评对象和指标选取。

ryanzzzzz的博客 4682

Android 默认授予所有应用权限以及特殊权限处理

PackageManagerService作为android的主要系统服务之一,包含处理应用权限相关的职责。源码地址如下:在PMS成功启动时并收到回掉systemReady回进行应用权限授予。DefaultPermissionGrantPolicy为权限处理策略管理类。这里并没有赋予所有系统应用权限,所以当进行应用时还是会显示权限检查的弹框。可通过此方法进行所有应用权限赋予:如果不想所有应用都授予权限,可考虑在system/etc下的权限相关xml文件添加应用包名和所需权限。

Jimmy的专栏 1510

Spring Boot 数据源与 MyBatis(-Plus) 自定义配置详解

本文详解 Spring Boot 中单数据源环境下 HikariCP 与 MyBatis-Plus 的自定义配置。通过手动声明 SqlSessionFactory,实现下划线转驼峰、日志输出、一级缓存策略等精细化控制。结合 HikariCP 核心参数调优(如最大连接数、连接生命周期),确保高性能与稳定性。适用于仅需访问单一数据库的场景,避免多数据源复杂性,兼顾性能与可维护性。

BADAO_LIUMANG_QIZHI的博客 206

Agent + 监控平台:自动诊断告警根因

根因分析、智能监控、AI Agent、告警风暴、异常检测、可观测性、故障诊断在现代分布式系统中,快速定位和解决问题是维持系统稳定性的关键。传统的监控系统虽然能够生成大量告警,但往往缺乏对告警根因的自动诊断能力,导致运维团队在告警风暴中难以快速定位问题。本文深入探讨了将AI Agent技术与监控平台结合,实现告警根因自动诊断的技术架构、理论基础和实现方法。我们将从第一性原理出发,分析根因诊断问题的本质,介绍基于Agent的智能监控系统设计,并通过实际案例展示其应用价值。文章同时涵盖了从理论到实践的全链路,包括

AIGC应用创新大全的博客 304

Java EE 进阶:8.MyBatis 操作数据库(入门)(第一弹)

Java EE 进阶:8.MyBatis 操作数据库(入门)(第一弹)

要努力去发光,而不是被照亮~ 145

linux cma alloc fail 问题分析

摘要:日志显示CMA连续内存分配失败(错误码-ENOMEM),请求128页(512KB)内存时因CMA区域不足或内存碎片导致失败。分析发现系统CMA总内存131072KB,空闲65536KB。解决方案包括:1)手动触发内存碎片整理;2)修改设备树或内核配置增大CMA区域(如从16MB增至20MB)。通过调整CONFIG_CMA_SIZE_MBYTES或设备树中的size参数可解决该问题。

陌上花开缓缓归以的博客 799

Python与ArcGIS系列(二)获取地图文档

在ArcPy中,要引用当前的地图文档(Map Document),可以使用 mapping 模块中的 MapDocument 类。值得注意的是,在arcgis内部python窗口运行该代码才能访问已加载进arcgis的文档,如果通过其他idle则无法访问。要引用磁盘上的地图文档并对其进行一些设置,同样可以使用ArcPy中的 mapping 模块和 MapDocument 类。对于我们平常的工作,接触最多的就是图层数据处理,获取图层后就可以方便地进行下一步处理。

weixin_43894929的博客 2200

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 368

Qt C++ 卫星数据接收与处理界面

打开 `MainWindow.ui`,你会发现我们完全是在代码中 (`setupUI`) 创建的 UI。* 访问 [QCustomPlot 官网](https://www.qcustomplot.com/) 下载最新版本的 `qcustomplot.h` 和 `qcustomplot.cpp` 文件。* 对于 FFT,可以使用专门的库如 [FFTW](http://www.fftw.org/),或者 Qt 5.15 及以上版本中提供的 `QFFT` 和 `QComplex` 类。

寒寒豆豆软件公司的博客 150

【从零理解 Spring AI · 12】MCP 的两种角色:做客户端还是做服务端

上一篇讲了 MCP 的概念:标准协议、三大角色、M+N 替代 M×N。

nice_lcj520的博客 512

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

JDK 下载安装:装完 Java 提示「不是内部或外部命令」?环境变量没配

JDK 是写 Java 程序必须装的东西。它包含运行 Java 的虚拟机(JRE)和编译 Java 的编译器(javac)——装了它,能跑、.java文件能编译成可执行程序。免费,学习和小规模生产都不花钱。

科小怪的博客 638

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Java 集合容器高频面试题全解:HashMap 树化阈值、1.7 死循环与 ConcurrentHashMap 演进(2026 招聘 JD 真题复盘)

HashMap 树化阈值 8 的泊松分布依据、退化阈值 6 的防抖设计、JDK 1.7 头插成环死循环的排查与修复、1.8 尾插后为何仍线程不安全、ConcurrentHashMap 放弃分段锁的原因与 ForwardingNode 协作扩容、computeIfAbsent 死锁陷阱、大集合找重复的场景题选型,附三层追问链自测。题目来源为 38 期公开招聘 JD 样本与字节、拼多多、建行、美团、快手公开面经。

蒙眼过河 349

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

java框架大全

java框架大全

qq_39326137的博客 184

Java 多态 学习笔记

多态是面向对象编程的重要特性,指同一操作作用于不同对象时产生不同行为。其核心在于“父类引用指向子类对象”,结合方法重写实现运行时动态绑定。多态使代码更灵活、可扩展,如用Person p = new Student();统一处理各类人员注册,无需修改调用逻辑。但多态无法直接调用子类特有方法,需通过instanceof判断并强制转换。编译时看左边(类型),运行时看右边(实际对象),实现解耦与维护便利。

AA_AA114514的博客 365

SecureRandom做随机源,这个Java随机字符串工具类有3个方法

本文分享一款JDK17环境下的Java随机字符串工具类,内置3个开箱即用的方法,全部采用SecureRandom加密安全随机源,不存在弱随机、可预测、取模偏差等问题。工具类支持默认62字符集随机串、纯数字随机串、自定义字符集随机串三种生成模式,支持Unicode码点级别的字符集处理,兼容Emoji等增补字符,边界处理严格,异常信息明确,适合验证码、邀请码、盐值、随机口令、短链码等场景,源码可直接复制使用。

Java后端 | AI智能体应用实战派 | 电脑技能行家 300

IDEA 简介:从零认识 IntelliJ IDEA

IDEA 全称,是由 JetBrains 公司开发的一款主流 Java 集成开发环境(IDE)。它集代码编辑、编译、调试、测试、版本控制、构建管理等功能于一体,被大量 Java 开发者视为日常开发的首选工具。所谓 IDE(Integrated Development Environment,集成开发环境),就是把写代码所需的各类工具整合到同一个界面中。相比只用文本编辑器加命令行编译,IDE 能显著减少重复操作,让开发者把精力集中在业务逻辑上。

ztI10的博客 161

Java实现批量线段相交检测(跨积判定+扫描线优化与踩坑复盘)

【代码】Java实现批量线段相交检测(跨积判定+扫描线优化与踩坑复盘)

m0_64476639的博客 332

TRingBuffer两端都能增删(快递站版)

TRingBuffer 是一个支持两端增删、下标访问且容量动态增长的环形双端队列。它通过无符号索引的永不归零设计,利用减法自动模运算实现空满判断与元素计数,避免了传统环形缓冲浪费格子的问题。内部以票号(索引)标记首尾,扩容时重排元素为连续存储,保证 O(1) 操作与高缓存效率。虽不线程安全,但其高效性使其成为游戏开发中事件队列、命令缓冲等场景的理想选择。

u013768914的博客 301

ubuntu安装hadoop系统

本文详细介绍了基于VMware克隆虚拟机搭建三节点Hadoop分布式集群的全过程:使用同一Ubuntu镜像克隆出master、worker1、worker2三台虚拟机,依次配置主机名、固定IP(如192.168.82.10/11/12)、SSH服务及免密登录,确保节点间网络互通。随后安装Java,下载配置Hadoop并分发至各节点,最终初始化启动集群,验证其存储与计算功能,实现高效的大数据环境部署。

qq_43592674的博客 345

Mac上装了最新版本的Android Studio,但是没找到jdk的设置选项也没找到jbr的目录

新版Android Studio(2025.3.1+)因三项变更导致常见问题:一、JDK设置移至“DaemonJVM criteria”,新项目默认自动探测或下载JDK;二、JBR目录改名为带版本号的jbr-21等,需进入.app/Contents/查看,或通过About窗口确认运行时;三、命令行缺adb因platform-tools未加入PATH,需在SDK Manager中勾选并配置~/.zshrc中的ANDROID_HOME及PATH。重装或清理残留可解决缺失问题,切勿手动设JAVA_HOME或安装冲

Programming is a highly artistic endeavor 362

Spring事务和事务传播机制

3. 可重复读(REPEATABLE READ): 事务不会读到其他事务对已有数据的修改, 即使其他事务已提交. 也就可以确保同⼀事务多次查询的结果⼀致, 但是其他事务新插⼊的数据, 是可以感知到的. 这也就引发了幻读问题. 可重复读, 是 MySQL 的默认事务隔离级别.p1事务可以认为是⽗事务, 嵌套事务是⼦事务. ⽗事务出现异常, ⼦事务也会回滚, ⼦事务出现异常, 如果不进⾏处理, 也会导致⽗事务回滚.

txxzjmzlh的博客 207

Java Arrays.asList 常见坑详解|UnsupportedOperationException、数组联动、基本类型 bug

解析 Arrays.asList 常见开发陷阱,演示 UnsupportedOperationException、底层数组引用共享、基本类型数组问题,区分 Arrays 内部 ArrayList 与 java.util.ArrayList,给出数组转 List 的各种场景正确代码。

weixin_42617033的博客 487
上一篇: Principles of Health Data Analytics R
下一篇: EIE2105 Digital and Computer Systems Tutorial 7: Microprocessor Design Basics and Instruction Set Ar
WX :  codinghelp
博客等级 码龄2年 349粉丝 72原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值