Java宝藏实验资源库(4)对象数组

一、实验目的

  1. 学习面向对象程序设计的方法。
  2. 学习建立对象数组的方法。
  3. 学习在数组中存储和处理对象。 

二、实验内容过程及结果 

**10.7 (Game: ATM machine) Use the Account class created in Programming Exer
cise 9.7 to simulate an ATM machine. Create ten accounts in an array with id 0,1,.1.9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.

**10.7 (游戏:ATM机) 使用编程练习9.7中创建的Account类来模拟ATM机。在数组中创建十个账户,ID为0、1、2、3、9,初始余额为100美元。系统提示用户输入ID。如果ID输入错误,请用户输入正确的ID。一旦接受ID,就会显示与样本运行中所示相同的主菜单。您可以输入1查看当前余额,输入2提取现金,输入3存入现金,输入4退出主菜单。一旦退出,系统将再次提示输入ID。因此,一旦系统启动,就不会停止。

运行代码如下 : 

package chapter10;import java.util.Date;
import java.util.Scanner;class Code_07 {public static void main(String[] args) {Account[] accounts = new Account[10];for (int i = 0; i < 10; i++)accounts[i] = new Account(1, 100);System.out.print("Enter an id:");Scanner input = new Scanner(System.in);int id = input.nextInt();while (id < 0 || id > 9) {System.out.print("The if is nonExistent,please input again:");id = input.nextInt();}mainMenu();int choice = input.nextInt();boolean judge = choice == 1 || choice == 2 || choice == 3;while (judge) {switch (choice) {case 1:System.out.println("The balance is "+accounts[id].getBalance());break;case 2:System.out.print("Enter an amount to withdraw: ");double withdraw = input.nextDouble();accounts[id].withdraw(withdraw);break;case 3:System.out.print("Enter an amount to deposit:");double deposit=input.nextDouble();accounts[id].deposit(deposit);break;}mainMenu();choice=input.nextInt();judge = choice == 1 || choice == 2 || choice == 3;}Code_07.main(args);}public static void mainMenu(){System.out.println("Main menu");System.out.println("1: check balance ");System.out.println("2: withdraw ");System.out.println("3: deposit ");System.out.println("4: exit ");System.out.print("Enter a choice: ");}
}class Account {public Account() {dateCreated = new Date();}public Account(int id,double balance){this.id = id;this.balance = balance;dateCreated = new Date();}private int id;private double balance;private double annualInterestRate;public int getId() {return id;}public void setId(int id) {this.id = id;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}public double getAnnualInterestRate() {return annualInterestRate;}public void setAnnualInterestRate(double annualInterestRate) {this.annualInterestRate = annualInterestRate;}private Date dateCreated=new Date();public void Account() {}public double getMonthlyInterest() {return balance*(annualInterestRate/100/12);}public void withdraw(double reduce) {balance-=reduce;}public void deposit(double increase) {balance+=increase;}public Date getDateCreated() {return dateCreated;}
}

运行结果 

10.14(The MyDate class) Design a class named MyDate. The class contains:
The data fields year, month, and day that represent a date. month is
O-based, i.e., 0 is for January.
1Anoarg constructor that creates a MyDate object for the current date.■ A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in millise--conds.
A constructor that constructs a MyDate object with the specified year,
month, and day.
■ Three getter methods for the data fields year, month, and day, respectively.■ A method named setDate(long elapsedTime) that sets a new date for the object using the elapsed time.
Draw the UML diagram for the class and then implement the class. Write a test program that creates two MyDate objects (using new MyDate() and new MyDate(34355555133101L)) and displays their——year,monthandday.(Hint: The first two constructors will extract the year, month, and day from the elapsed time. For example, if the elapsed time is 561555550000 miliseconds, the year is 1987, the month is 9, and the day is 18. You may use the GregorianCalendar class discussed in Programming.Exercise 9.5 to simplify coding.)

**10.14(MyDate类) 设计一个名为MyDate的类。该类包含以下内容: 表示日期的字段year、month和day。month是基于O的,即0表示一月。 一个无参构造函数,用于创建表示当前日期的MyDate对象。 一个构造函数,用于使用自午夜起的毫秒数创建一个指定的过去时间的MyDate对象。 一个构造函数,用于使用指定的年、月和日创建一个MyDate对象。 分别用于获取数据字段year、month和day的三个getter方法。 一个名为setDate(long elapsedTime)的方法,用于使用elapsedTime为对象设置新的日期。 绘制该类的UML图,然后实现该类。编写一个测试程序,创建两个MyDate对象(使用new MyDate()和new MyDate(34355555133101L)),并显示它们的年、月和日。(提示:前两个构造函数将从elapsedTime中提取年、月和日。例如,如果elapsedTime是561555550000毫秒,年份是1987,月份是9,日期是18。您可以使用Programming.Exercise 9.5中讨论的GregorianCalendar类简化编码。)

 运行代码如下 : 

package pack2;import java.util.GregorianCalendar;class MyDate {private int year, month, day;   //年、月、日/**当前日期的无参构造方法*/public MyDate() {setDate(System.currentTimeMillis());}/**以流逝的毫秒数为时间的构造方法*/public MyDate(long elapsedTime) {setDate(elapsedTime);}/**带指定年、月、日的构造方法*/public MyDate(int year, int month, int day) {this.year = year;this.month = month;this.day = day;}/**使用流逝的时间设置新日期*/public void setDate(long elapsedTime) {GregorianCalendar calendar = new GregorianCalendar();calendar.setTimeInMillis(elapsedTime);year = calendar.get(GregorianCalendar.YEAR);month = calendar.get(GregorianCalendar.MONTH);day = calendar.get(GregorianCalendar.DAY_OF_MONTH);}@Override   /**返回年、public int getMonth() {return month;}public int getDay() {return day;}//————————————————————————————————————————————————————public static void main(String[] args) {MyDate date1 = new MyDate();MyDate date2 = new MyDate(34355555133101L);System.out.println("date1: \n" + date1);System.out.println("\ndate2: \n" + date2);date2.setDate(561555550000L);System.out.println("\ndate2: \n" + date2);}}月、日的字符串*/public String toString() {return "Year: " + year + "\nMonth: " + month + "\nDay: " + day;}public int getYear() {return year;}

运行结果   

三、实验结论  

       通过本次实验实践了ATM机知识和操作,趣味十足,得到了编程与数学,逻辑思维息息相关的感悟,要想写优秀的代码,头脑必须清醒,思维必须井井有条,敲写代码时必须心无旁骛。

 结语   

优秀是一种习惯

毅力里藏着帝王志气

!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/31471.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大数据存储技术笔记

目录 大数据的特性 HDFS 读流程的基本步骤 HDFS 写流程的基本步骤 Mapreduce的执行过程 MapReduce 中 combiner 作用 hadoop 调度器及其工作方法 Hive 中内部表与外部表区别(创建删除角度) Hadoop 的 2 个主要组件及其功能 Hadoop MapReduce 的工作流程 正常工作的 ha…

AWS Lambda + Flask 应用示例

前言 AWS Lambda 本身是一个以事件驱动的 Serverless 服务, 最简单的应用就是在入口函数中对接收到的事件/请求进行处理并返回响应. 对于像 Flask 这样的 Web 框架, 并不能直接在 Lambda 上提供服务, 不过我们可以借助 AWS Lambda Web Adapter 实现一个基于 Flask 框架的 Web …

IDEA、PyCharm等基于IntelliJ平台的IDE汉化方式

PyCharm 或者 IDEA 等编辑器是比较常用的&#xff0c;默认是英文界面&#xff0c;有些同学用着不方便&#xff0c;想要汉化版本的&#xff0c;但官方没有这个设置项&#xff0c;不过可以通过插件的方式进行设置。 方式1&#xff1a;插件安装 1、打开设置 File->Settings&a…

iptables(4)规则匹配条件

简介 前面我们已经介绍了iptables的基本原理,表、链,数据包处理流程。如何查询各种表的信息。还有基本的增、删、改、保存的基础操作。 经过前文介绍,我们已经能够熟练的管理规则了,但是我们只使用过一种匹配条件,就是将”源地址”作为匹配条件。那么这篇文章中,我们就来…

【Java】已解决java.net.MalformedURLException异常

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决java.net.MalformedURLException异常 在Java的网络编程中&#xff0c;java.net.MalformedURLException是一个常见的异常&#xff0c;它通常表明URL&#xff08;统一资源定位符…

「五度易链」企业大数据API接口开放平台上线啦!

“五度易链”企业大数据API接口开放平台现已正式上线&#xff0c;旨在为广大企业、开发者及个人提供标准、安全、高效、便捷的企业数据API接口服务&#xff0c;帮您更轻松地构建应用、扩展功能&#xff0c;并基于用户应用场景提供专属接口定制服务&#xff0c;助力企业提升研发…

数据分析必备:一步步教你如何用matplotlib做数据可视化(8)

1、Matplotlib 条形图 条形图或条状图是一种图表或图形&#xff0c;它显示带有矩形条的分类数据&#xff0c;其高度或长度与它们所代表的值成比例。可以垂直或水平绘制条形。 条形图显示了离散类别之间的比较。图表的一个轴显示要比较的特定类别&#xff0c;另一个轴表示测量值…

Stable Diffusion WebUI 使用ControlNet:IP-Adapter保持生图的角色一致性

IP-Adapter-FaceID可以在保持人脸一致的条件下生成各种风格的图像。 下载 IP Adapter 需要的 Face ID 模型和 Lora 下载地址&#xff1a;https://huggingface.co/h94/IP-Adapter-FaceID/ 下载 ip-adapter-faceid-plusv2_sd15.bin 和 ip-adapter-faceid-plusv2_sd15_lora.saf…

【MySQL进阶之路 | 高级篇】常见索引(聚簇索引, 二级索引)

1. 常见索引概念 索引按照物理实现方式&#xff0c;可以分为两种&#xff0c;聚簇索引和非聚簇索引.我们也把非聚簇索引称为二级索引或辅助索引. (1). 聚簇索引 聚簇索引并不是一种单独的索引类型&#xff0c;而是一种数据存储方式(所有的数据记录都存储在了叶子节点)&#…

js如何使得四舍五入的百分比之和为100%

在JavaScript中&#xff0c;如果你想要确保一组四舍五入后的百分比之和严格等于100%&#xff0c;那么你不能直接对每个百分比进行四舍五入&#xff0c;因为四舍五入会引入误差。但是&#xff0c;你可以采用一种策略&#xff0c;即先对所有的百分比进行常规的四舍五入&#xff0…

C# WPF入门学习主线篇(二十九)—— 绑定到对象和集合

C# WPF入门学习主线篇&#xff08;二十九&#xff09;—— 绑定到对象和集合 在WPF中&#xff0c;数据绑定是开发动态和交互性用户界面的核心技术。通过数据绑定&#xff0c;我们可以轻松地将UI控件与后台的数据源连接起来&#xff0c;实现数据的自动更新和显示。在本篇文章中&…

wordpress 导航主题 有批量从源码导入功能

下载地址&#xff1a;wordpress导航主题 可以批量导入

ardupilot开发 --- Jetson Orin Nano 后篇

我拼命加速&#xff0c;但贫穷始终快我一步 0~1920. visp-d455&#xff1a;基于IBVS的Pixhawk无人机视觉伺服20.1 基础关于连接、通讯、UDP forward服务&#xff1a;一些相关的、有用的例程Linux C程序的gdb断点调试搭建仿真解决【testPixhawkDroneTakeoff.cpp例程能解锁但起飞…

WIFI6E中的MESH组网功能

什么是WIFI6E和MESH组网&#xff1f; WIFI 6E 是扩展到6GHz 频段的WIFI 6无线通信技术&#xff0c;而“WIFI 6E”中的“6”是指WIFI技术的“第6代”&#xff0c;“E”则是指使用新频段的标准的最新扩展。WIFI 6E通过增加6GHz频段&#xff0c;提供更高的带宽、更低的延迟和更大…

VMware虚拟机下载安装Windows Server 2016

「作者简介」&#xff1a;2022年北京冬奥会网络安全中国代表队&#xff0c;CSDN Top100&#xff0c;就职奇安信多年&#xff0c;以实战工作为基础对安全知识体系进行总结与归纳&#xff0c;著作适用于快速入门的 《网络安全自学教程》&#xff0c;内容涵盖系统安全、信息收集等…

从新手小白到红酒大咖:解锁红酒品鉴的终极秘籍,升级之路全攻略

在五彩斑斓的饮品世界中&#xff0c;红酒以其深邃的色泽、丰富的口感和悠久的历史&#xff0c;吸引了无数人的目光。对于红酒的初学者来说&#xff0c;从小白到品鉴师的道路或许充满了未知与挑战&#xff0c;但只要掌握了正确的知识和方法&#xff0c;就能够轻松踏入这个美妙的…

用群辉NAS打造影视墙(Jellyfin篇)

目录 1、安装Jellyfin媒体服务器 2、配置 (1)语言 (2)管理员账户 (3)添加媒体库 (4)指定元数据语言 (5)远程访问设置 (6)修改文件夹权限 (7)刷新电影 (8)启用硬件加速 3、PC浏览器访问 4、手机客户端 5、智能TV客户端 6、解决演员不能显示中文的问…

Jenkins+gitee流水线部署springboot项目

目录 前言 一、软件版本/仓库 二、准备工作 2.1 安装jdk 11 2.2 安装maven3.9.7 2.3 安装docker 2.4 docker部署jenkins容器 三、jenkins入门使用 3.1 新手入门 3.2 jenkins设置环境变量JDK、MAVEN、全局变量 3.2.1 jenkins页面 3.2.2 jenkins容器内部终端 3.2.3 全…

python-赏月

[题目描述] 在某个星球上看到的月亮大小有一个规律&#xff0c;月亮为每30天一个周期&#xff0c;在这30天的周期里&#xff0c;月亮的大小分别为 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1。 虽然天气很冷&#xff0c;但这个星球上的某个居民今…

MyBatis 源码分析--SqlSessionFactory

前言&#xff1a; 前文我们简单的回顾了 MyBatis 的基本概念&#xff0c;有聊到核心组件&#xff0c;工作流程等&#xff0c;本篇我们开始深入剖析 MyBatis 的核心源码&#xff0c;欢迎大家持续关注。 Mybatis 知识传送门 初识 MyBatis 【MyBatis 核心概念】 MyBatis 源码解…