Java上机实验报告(12)

实验 (12) 项目名称: 泛型与集合框架

一、 实验报告内容一般包括以下几个内容:

  1. 实验项目名称 实验12 泛型与集合框架
  2. 实验目的和要求
    目的:
    本实验的目的是让学生掌握LinkedList泛型类和HashMap类的使用。

要求:
编写程序:
(1).编写类Ex_1,有下面的LinkedList,分别用使用方法1:foreach,方法2:迭代器和方法3:链表的get()方法遍历链表。
遍历此链表。

Javapythoncc++phpoc

(2).编写一个主类Ex_2,在Java中使用HashMap来存储包含序号和姓名的人的信息,并且通过姓名来随机点名,用序号(或两位数的学号)作为键(Key),姓名作为值(Value)。然后,使用HashMap的键集(keySet)来获取所有的姓名,并使用Random类来随机选择一个姓名。(10个人的名单自己杜撰)
3. 要求学生把源代码截图和结果截图提交给老师。
4. 实验原理

  1. 主要仪器设备
    (1)学生每人一台PC机;
    (2)互联网环境。
  2. 实验内容及步骤
    Ex_1.java
package data20240523;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Ex_1 {
    public static void main(String[] args) {
        String[] s = new String[]{"java","python","c","c++","php","oc"};
        List<String> list = new LinkedList<>(Arrays.asList(s).subList(0, 6));
        System.out.println("for-each输出");
        for(String s1 : list){
            System.out.printf("%s ",s1);
        }
        System.out.println("\n迭代器输出");
        Iterator<String> iter = list.iterator();
        while(iter.hasNext()){
            System.out.printf("%s ",iter.next());
        }
        System.out.println("\nget()输出");
        for(int i=0;i<list.size();++i){
            System.out.printf("%s ",list.get(i));
        }
    }
}

Ex_2.java

package data20240523;

import java.util.HashMap;
import java.util.Random;

public class Ex_2 {
    public static void main(String[] args) {
        HashMap<String,String> hashMap = new HashMap<>();
        String[] s1 = new String[]{"1","2","3","4","5","6","7","8","9","10"};
        String[] s2 = new String[]{"A哥","B哥","C哥","D哥","E哥","F哥","G哥","H哥","I哥","J哥"};
        for(int i=0;i<10;++i)
        {
            hashMap.put(s1[i],s2[i]);
        }
        String[] s3= hashMap.keySet().toArray(new String[0]);
        Random random = new Random();
        int re=random.nextInt(10);
        System.out.printf("点名结果为 %02d: %s",re+1,hashMap.get(s3[re]));
    }
}

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java上机实验五:银行账户管理系统 本实验要求使用Java面向对象的编程思想,实现一个简单的银行账户管理系统。在该系统中,每个账户都有一个账户号、账户余额、账户利率等信息,可以进行存款、取款、查询余额、计算利息、修改利率等基本操作。 实验要求: 1. 编写一个Account类,该类包含以下成员变量: - 账户号(id) - 账户余额(balance) - 年利率(annualInterestRate) 2. Account类还包含以下方法: - 无参构造方法 - 带参构造方法,可以为所有成员变量赋值 - 存款(deposit)方法,将指定金额加到账户余额中 - 取款(withdraw)方法,从账户余额中减去指定金额 - 查询余额(getBalance)方法 - 计算利息(getMonthlyInterest)方法,返回当前账户月利息 - 修改年利率(setAnnualInterestRate)方法 3. 编写一个测试类,测试Account类中的各个方法是否正确。 4. 扩展Account类,实现两个子类: - CheckingAccount类,该类继承Account类,代表一个支票账户,每次取款都会收取一定手续费。 - SavingsAccount类,该类继承Account类,代表一个储蓄账户,每年会发生一次利息结算。 5. 编写一个Bank类,该类用于管理多个账户,实现以下方法: - addAccount方法,添加一个账户 - removeAccount方法,删除一个账户 - getTotalBalance方法,返回所有账户的总余额 - getAverageBalance方法,返回所有账户的平均余额 - getNumberOfAccounts方法,返回账户的总数 6. 编写一个测试类,测试Bank类中的各个方法是否正确。 提示: - 可以使用ArrayList来存储所有的账户。 - 在子类中可以使用super关键字来调用父类的构造方法或方法。 - 在SavingsAccount类中,可以使用Calendar类来获取当前的日期和时间,然后判断是否需要进行利息结算。 参考代码:(仅供参考,学生应自主完成实验) Account类: ```java public class Account { private int id; private double balance; private double annualInterestRate; public Account() { } public Account(int id, double balance, double annualInterestRate) { this.id = id; this.balance = balance; this.annualInterestRate = 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; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } public double getMonthlyInterest() { return balance * annualInterestRate / 12; } } ``` CheckingAccount类: ```java public class CheckingAccount extends Account { private double fee; public CheckingAccount() { } public CheckingAccount(int id, double balance, double annualInterestRate, double fee) { super(id, balance, annualInterestRate); this.fee = fee; } public double getFee() { return fee; } public void setFee(double fee) { this.fee = fee; } @Override public void withdraw(double amount) { super.withdraw(amount); balance -= fee; } } ``` SavingsAccount类: ```java import java.util.Calendar; public class SavingsAccount extends Account { private Calendar lastInterestDate; public SavingsAccount() { } public SavingsAccount(int id, double balance, double annualInterestRate) { super(id, balance, annualInterestRate); lastInterestDate = Calendar.getInstance(); } public void addInterest() { Calendar now = Calendar.getInstance(); if (now.get(Calendar.MONTH) != lastInterestDate.get(Calendar.MONTH)) { double interest = getMonthlyInterest() * 12; balance += interest; lastInterestDate = now; } } } ``` Bank类: ```java import java.util.ArrayList; public class Bank { private ArrayList<Account> accounts; public Bank() { accounts = new ArrayList<>(); } public void addAccount(Account account) { accounts.add(account); } public void removeAccount(Account account) { accounts.remove(account); } public double getTotalBalance() { double total = 0; for (Account account : accounts) { total += account.getBalance(); } return total; } public double getAverageBalance() { double total = getTotalBalance(); return total / accounts.size(); } public int getNumberOfAccounts() { return accounts.size(); } } ``` 测试类: ```java public class Test { public static void main(String[] args) { Account account1 = new Account(1, 1000, 0.01); account1.deposit(100); account1.withdraw(200); System.out.println("Account 1 balance: " + account1.getBalance()); System.out.println("Account 1 monthly interest: " + account1.getMonthlyInterest()); CheckingAccount account2 = new CheckingAccount(2, 1000, 0.01, 10); account2.withdraw(200); System.out.println("Account 2 balance: " + account2.getBalance()); System.out.println("Account 2 monthly interest: " + account2.getMonthlyInterest()); SavingsAccount account3 = new SavingsAccount(3, 1000, 0.01); account3.addInterest(); System.out.println("Account 3 balance: " + account3.getBalance()); System.out.println("Account 3 monthly interest: " + account3.getMonthlyInterest()); Bank bank = new Bank(); bank.addAccount(account1); bank.addAccount(account2); bank.addAccount(account3); System.out.println("Total balance: " + bank.getTotalBalance()); System.out.println("Average balance: " + bank.getAverageBalance()); System.out.println("Number of accounts: " + bank.getNumberOfAccounts()); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值