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.mfbz.cn/a/729551.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MAC地址解析工具:ARP命令

网络中每台设备都有一个唯一的网络标识&#xff0c;这个地址叫MAC地址或网卡地址&#xff0c;由网络设备制造商生产时写在硬件内部。形象地说&#xff0c;MAC地址就如同身份证上的身份证号码&#xff0c;具有唯一性。 无论是局域网&#xff0c;还是广域网中的计算机之间进行通信…

Windows系统下安装RabbitMQ详细步骤

声明&#xff1a;原文参考链接出自&#xff1a; 如何在Windows系统下安装RabbitMQ_rabbitmq windows安装-CSDN博客 https://zhuanlan.zhihu.com/p/693160757 一、RabbitMQ安装软件资源准备 因为RabbitMQ是Erlang语言开发的&#xff0c;因此安装Erlang环境在进行安装RbbitMQ的…

小程序大作为|小程序开发详细流程,新手也能轻松掌握

随着移动互联网的快速发展&#xff0c;小程序作为一种轻量级应用&#xff0c;因其无需下载安装、即点即用、用完即走的特点&#xff0c;受到了广大用户的青睐。那么开发小程序都有哪些开发流程呢&#xff1f;可以用哪种方式开发&#xff1f;选择合适的开发方式&#xff0c;一起…

java连接mysql报错

1.背景&#xff0c;直接升级操作系统从centos-》国产化操作系统&#xff0c;mysql也升级到5.7.44 2&#xff0c;报错 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconn…

如何使用 ArcGIS Pro 和 Landsat 8 影像计算叶绿素指数和全球环境监测指数

GIS 工具和技术的出现极大地帮助了识别、量化和解决问题。GIS 还通过研究可能的情况并实施预防方案提供了一种主动的解决方案。多年来&#xff0c;GIS 通过电信和网络服务、事故/事件分析、城市规划、交通规划、环境影响评估、洪水损失估计、自然资源管理、环境健康和安全、植被…

【STM32-DAP 仿真器】

STM32-DAP 仿真器 ■ STM32-DAP仿真器介绍■ STM32-DAP仿真特点■ STM32-DAP仿真器实物图■ STM32-DAP高速 DAP 仿真器实物图■ STM32-DAP高速无线调试器 实物图■ STM32-DAP高速无线调试器示意图■ STM32-DAP高速无线调试器接线图■ STM32-DAP高速无线调试器接收端示意图 ■ S…

oracle开放某些视图给特定用户,查询报视图不存在问题

以sysdba身份登录到Oracle数据库。 创建新用户。例如&#xff0c;创建一个名为new_user的用户&#xff0c;密码为password&#xff1a; CREATE USER new_user IDENTIFIED BY password;为新用户分配表空间和临时表空间。例如&#xff0c;将表空间users和临时表空间temp分配给新…

循环的三种写法

一、for(i): for (int i0;i< arrayList.size();i){System.out.println(arrayList.get(i));} 最基本的循环方法。 二、for-each: 又称加强for &#xff0c;更简单的遍历集合。 三、迭代器: 迭代器是调用Java中的Iterator接口&#xff0c;该接口定义了三个方法分别是hasNex…

阿里云PAI主机网页访问测试

笔者使用的阿里云平台PAI主机(首次使用免费三个月额度)&#xff0c;由于其默认不设置公网IP&#xff0c;所以在该主机上启动HTTP服务后无法访问测试。 这里使用ssh来作隧道穿透&#xff0c;首先需要配置ssh。 云主机配置ssh 1. 修改root账号密码 在云主机上执行 passwd ro…

写一个可以批量修改图片分辨率的工具

说在前面 &#x1f388;在视觉内容至关重要的今天&#xff0c;图片尺寸的调整对于网站加载速度和用户体验有着直接影响。本文介绍的Node.js工具&#xff0c;通过简单的命令行操作&#xff0c;允许用户批量调整图片尺寸&#xff0c;支持单张图片和整个目录的操作&#xff0c;提供…

ARM32开发--FreeRTOS-事件组

系列文章目录 知不足而奋进 望远山而前行 目录 系列文章目录 文章目录 前言 目标 内容 概念 事件标志位 开发流程 功能介绍 创建事件组 触发事件 等待事件触发 同步 清理事件 案例 总结 前言 在嵌入式系统开发中&#xff0c;任务之间的同步和通信是至关重要的…

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

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

测试的基础知识大全【测试概念、分类、模型、流程、测试用例书写、用例设计、Bug、基础功能测试实战】

测试基础笔记 Day01阶段⽬标⼀、测试介绍⼆、测试常⽤分类2.1 阶段划分单元测试集成测试系统测试验收测试 2.2 代码可⻅度划分⿊盒测试&#xff1a;主要针对功能&#xff08;阶段划分->系统测试&#xff09;灰盒测试&#xff1a;针对接⼝测试&#xff08;阶段划分->集成测…

海思NNIE精度对比详细操作指南

海思NNIE部署推理经常会遇到精度下降问题,但是又摸不着头脑究竟是什么原因,因此需要做精度分析来排查是不是算子问题或者是具体哪个算子问题。本文撰写详细操作说明文档,具体可以参考资料:海思NNIE之Mobilefacenet量化部署-腾讯云开发者社区-腾讯云 1.打开日志等级 不知道…

OpenAI 联合创始人 Ilya Sutskever 的新初创公司致力于“安全超级智能”

OpenAI 前首席科学家伊利亚-苏茨克沃尔&#xff08;Ilya Sutskever&#xff09;在今年 5 月离开了他共同创立的人工智能研究公司后&#xff0c;透露了他的下一个重要项目。 相关阅读&#xff1a;GPT-4o通过整合文本、音频和视觉实现人性化的AI交互&#xff0c;OpenAI推出了其新…

STM32 I2C总线锁死原因及解决方法

本文介绍STM32 I2C总线锁死原因及解决方法。 在使用STM32 I2C总线操作外设时&#xff0c;有时会遇到I2C总线锁死&#xff08;I2C总线为Busy状态&#xff09;的问题&#xff0c;即便复位MCU也无法解决&#xff0c;本文介绍其锁死的原因和解决方法&#xff0c;并给出相应的参考代…

融资融券有哪些优势和风险,融资融券利息怎么算,利率最低是?4.0

融资融券的优势 1. 提高资金利用率&#xff1a;获得额外的资金或股票交易&#xff0c;提高资金利用率&#xff0c;扩大投资规模。 2. 降低投资风险&#xff1a;通过融资融券买入多只股票分散风险&#xff0c;降低单一股票持仓风险。 3. 增加投资收益&#xff1a;提供更多的交…

CVPR2024|UniPAD:一种自动驾驶的统一的预训练范式

本文章仅用于学术分享 论文标题丨 UniPAD: A Universal Pre-training Paradigm for Autonomous Driving 论文地址丨 https://arxiv.org/abs/2310.08370 代码地址 | https://github.com/Nightmare-n/UniPAD 关注「AI前沿速递」公众号&#xff0c;获取更多前沿资讯 01总览 这…

Android下QVideoFrame转QImage不成功记录

1.由于QVideoFrame::image() const : unsupported pixel format Format_ABGR32 &#xff0c;在转换时需要做个特殊处理如下,增加了android手机下的特殊格式处理: if(frame.pixelFormat() QVideoFrame::Format_ABGR32) 此部分代码 QImage imageFromVideoFrame(QVideoFrame &…

uniapp 打包 H5 实现在 uniapp 打包 APP 的 webview 通信

一、前言 遇到 uniapp 打包的 APP 在 webview 内嵌入 uniapp 打包的 H5 页面的需求&#xff0c;并实现通信。本篇主要总结了如何实现并总结遇到的问题&#xff0c;希望可以帮助大家减少负担。 实现需求主要有三个地方需要处理&#xff1a; index.html 的打包配置导入 uni.we…