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,一经查实,立即删除!

相关文章

python从入门到精通8:数字随机数

在Python中&#xff0c;随机数的生成和处理是编程中常见的需求之一。Python的标准库提供了random模块&#xff0c;该模块包含了生成各种类型随机数的函数。本教程将深入探讨Python中random模块的使用&#xff0c;包括生成随机数、随机选择、随机打乱顺序等高级用法。 1. 导入r…

大数据存储技术笔记

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

解决Ubuntu修改完root密码还是不能登录

ubuntu使用Xftp、Xshell连接时&#xff0c;ssh服务器拒绝访问。一直显示输入密码&#xff0c;这是由于sshd的设置不允许root用户使用密码进行远程登录。 解决办法&#xff1a;修改sshd_config配置文件。如果不存在&#xff0c;检查是否安装了openssh openssh安装命令&#xf…

Qt C++中使用QFutureWatcher 等待多个异步任务 执行完成

在Qt开发中,处理耗时任务而不会阻塞主线程(通常负责图形用户界面的更新)是非常重要的。Qt提供了多种机制来实现这一点,其中QFutureWatcher结合QtConcurrent是处理并等待多个异步任务完成的一种强大方法。下面我们将详细解析一段代码,该代码演示了如何使用QFutureWatcher来…

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…

python基础大全【13万字】

python基础大全【13万字】 Day011、Python概述2、Python解释器和pycharmIDE工具3、Python中的注释4、变量5、标识符的命名规范 ★6、变量的使用7、Python中的数据类型 ★8、Python中的bug和调试9、字符串的格式化及输出 ★10、转译字符 day021、转译字符2、f-string ★3、数据类…

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;实现数据的自动更新和显示。在本篇文章中&…

深度学习跨平台环境问题

在深度学习过程中&#xff0c;不可避免的是在win上跑不通的代码在ubuntu中就可以跑通&#xff0c;这里建议使用autodl。 首先在ubuntu上导出环境&#xff08;如果你的环境是base的话使用base&#xff09; conda env export --name base --no-builds > environment.yaml 这…

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例程能解锁但起飞…

物联网LORA技术-SX1276/SX1277/SX1278-137MHz至1020MHz低功耗远距离收发器

概述&#xff1a; SX1276/SX1277/SX1278 收发器主要采用LORATM 远程调制解调器&#xff0c;用于超长距离扩频通信&#xff0c;抗干扰性强&#xff0c;能够最大限度降低电流消耗。借助升特的LoRaTM 专利调制技术&#xff0c;SX1276/SX1277/SX1278 采用低成本的晶体和物料即可获得…

SHELL脚本学习(十)初识 sed编辑器

一、sed 编辑器 sed 编辑器被称作流编辑器&#xff0c;根据命令来处理数据流中的数据。 这些数据要么从命令行输入&#xff0c;要么保存在命令文本文件中。 sed编辑器可以执行下列操作&#xff1a; 从输入中读取一行数据根据所提供的命令匹配数据按照命令修改数据流中的数据将…