Java上机实验报告(3)

实验 (3) 项目名称:类与对象-类封装对象的属性和功能

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

  1. 实验项目名称 实验3 类与对象-类封装对象的属性和功能
  2. 实验目的和要求
    本实验的目的:
    (1) 学会使用类来封装对象的属性和功能
    (2)掌握类变量与实例变量,以及类方法与实例方法的区别
    (3)掌握构造方法和一般的方法的区别
    实验具体要求:
    定义一个圆柱体类Cylinder,类中含有方法area()求面积,方法volume ()计算体积。然后在主类E中用Cylinder创建相应的对象cylinder调用类中的成员变量和方法,然后计算圆柱体的底面积与体积。
    请用3个不同的方法求解该问题:(分别写3个不同的程序)
    方法1:主类中第一行代码是 (程序的主类E1)
    Cylinder cylinder=new Cylinder();
    方法2:主类中第一行代码是(程序的主类E2)
    Cylinder cylinder=new Cylinder(a,b); //a是输入的参数半径radius,b是输入的高height.
    方法3:要求增加两个类Area,和Volumn分别用来计算面积和体积。在主类中用Cylinder调用Area和Volumn分别计算面积和体积。(程序的主类E3)
  3. 实验原理
  4. 主要仪器设备
    (1)学生每人一台PC机;
    (2)互联网环境。

实验解答:
Cylinder.java

package data20240314;

public class Cylinder
{
    double radius;
    double height;
    Cylinder() {
        radius = 1.0;
        height = 1.0;
    }
    Cylinder(double radius,double height) {
        this.radius = radius;
        this.height = height;
    }
    double area() {
        return Math.PI * Math.pow(radius,2);
    }
    double volume() {
        return Math.PI * Math.pow(radius,2) * height;
    }
    double area1() {
        Area area = new Area();
        return area.area(radius);
    }
    double volumn() {
        Volumn volumn = new Volumn();
        return volumn.volumn(radius,height);
    }
}

E1.java

package data20240314;

public class E1 {
    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder();
        System.out.printf("圆柱体的底面积为:%.2f\n",cylinder.area());
        System.out.printf("圆柱体的体积为:%.2f\n",cylinder.volume());
    }
}

E2.java

package data20240314;

public class E2 {
    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder(2.0,2.0);
        System.out.printf("圆柱体的底面积为:%.2f\n",cylinder.area());
        System.out.printf("圆柱体的体积为:%.2f\n",cylinder.volume());
    }
}

Area.java

package data20240314;

public class Area {
    double area(double radius)
    {
        return Math.PI * Math.pow(radius,2);
    }
}

Volumn.java

package data20240314;

public class Volumn {
    double volumn(double radius,double height)
    {
        return Math.PI * Math.pow(radius,2) * height;
    }
}

E3.java

package data20240314;

public class E3 {
    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder();
        System.out.printf("圆柱体的底面积为:%.2f\n",cylinder.area1());
        System.out.printf("圆柱体的体积为:%.2f\n",cylinder.volumn());
    }
}
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值