用户类account与四个数字乘积最大值(JAVA)

这篇博客展示了两个Java代码示例。第一个涉及`Account`类,用于处理银行账户的存款、取款、利息计算等操作。第二个示例扩展了账户类,增加了交易记录,并实现了矩阵中四个数字乘积的最大值计算。代码包括了对二维数组的遍历和最大值查找,以及账户交易历史的记录和展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2020.05.17上机题
题目描述:
在这里插入图片描述
代码:

package Practice;

public class Ch09Q07 {
    public static void main (String[] args) {
        Account account = new Account(1122, 20000);
        Account.setAnnualInterestRate(4.5);

        account.withdraw(2500);
        account.deposit(3000);
        System.out.println("Balance is " + account.getBalance());
        System.out.println("Monthly interest is " +
                account.getMonthlyInterest());
        System.out.println("This account was created at " +
                account.getDateCreated());
    }
}

class Account {
    private int id;
    private double balance;
    private static double annualInterestRate;
    private java.util.Date dateCreated;

    public Account() {
        dateCreated = new java.util.Date();
    }

    public Account(int newId, double newBalance) {
        id = newId;
        balance = newBalance;
        dateCreated = new java.util.Date();
    }

    public int getId() {
        return this.id;
    }

    public double getBalance() {
        return balance;
    }

    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setId(int newId) {
        id = newId;
    }

    public void setBalance(double newBalance) {
        balance = newBalance;
    }

    public static void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }

    public double getMonthlyInterest() {
        return balance * (annualInterestRate / 1200);
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public void withdraw(double amount) {
        balance -= amount;
    }

    public void deposit(double amount) {
        balance += amount;
    }
}

效果图:
在这里插入图片描述

题目描述:
在这里插入图片描述
代码:

package Practice;

public class Ch11Q08 {
    public static void main (String[] args) {
        Account1.setAnnualInterestRate(1.5);

        Account1 account = new Account1("George", 1122, 1000);
        account.deposit(30);
        account.deposit(40);
        account.deposit(50);

        account.withdraw(5);
        account.withdraw(4);
        account.withdraw(2);

        System.out.println("Name: " + account.getName());
        System.out.println("Annual interest rate: " + Account1.getAnnualInterestRate());
        System.out.println("Balance: " + account.getBalance());

        java.util.ArrayList list = account.getTransactions();

        System.out.printf("%-35s%-15s%-15s%-15s\n", "Date", "Type", "Amount", "Balance");

        for (int i = 0; i < list.size(); i++) {
            Transaction transaction = (Transaction)(list.get(i));
            System.out.printf("%-35s%-15s%-15s%-15s\n", transaction.getDate(),
                    transaction.getType(), transaction.getAmount(), transaction.getBalance());
        }
    }
}

class Account1 {
    private int id;
    private String name;
    private double balance;
    private static double annualInterestRate;
    private java.util.Date dateCreated;
    private java.util.ArrayList transactions = new java.util.ArrayList();

    public Account1() {
        dateCreated = new java.util.Date();
    }

    public Account1(String name, int id, double balance) {
        this.id = id;
        this.name = name;
        this.balance = balance;
        dateCreated = new java.util.Date();
    }

    public int getId() {
        return this.id;
    }

    public double getBalance() {
        return balance;
    }

    public java.util.ArrayList getTransactions() {
        return transactions;
    }

    public String getName() {
        return name;
    }

    public static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setId(int id) {
        this.id =id;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public static void setAnnualInterestRate(double annualInterestRate) {
        Account1.annualInterestRate = annualInterestRate;
    }

    public double getMonthlyInterest() {
        return balance * (annualInterestRate / 1200);
    }

    public java.util.Date getDateCreated() {
        return dateCreated;
    }

    public void withdraw(double amount) {
        balance -= amount;
        transactions.add(new Transaction('W', amount, balance, ""));
    }

    public void deposit(double amount) {
        balance += amount;
        transactions.add(new Transaction('D', amount, balance, ""));
    }
}

class Transaction {
    private java.util.Date date;
    private char type;
    private double amount;
    private double balance;
    private String description;

    public Transaction(char type, double amount, double balance,
                       String description) {
        date = new java.util.Date();
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.description = description;
    }

    public java.util.Date getDate() {
        return date;
    }

    public char getType() {
        return type;
    }

    public double getAmount() {
        return amount;
    }

    public double getBalance() {
        return balance;
    }

    public String getDescription() {
        return description;
    }
}

效果图:
在这里插入图片描述

四个数字乘积最大值题目描述:
在这里插入图片描述
代码:

package Practice;

import java.io.File;
import java.util.Scanner;

public class CalcMaxValue {
    public static int n = 20;
    public static int[][] array = new int[n][n];


    public static void main(String[] args) {
        try{
            Scanner scanner = new Scanner(new File("C:\\Users\\d\\Desktop\\test1\\src\\Practice\\11.txt"));
            for(int i = 0; i < n; i ++ )
                for(int j = 0; j < n; j ++ )
                {
                    int x = scanner.nextInt();
                    array[i][j] = x;
                }

        }
        catch (Exception e){
            e.printStackTrace();
        }

//        for(int i = 0; i < n; i ++ )
//        {
//            for(int j = 0; j < n; j ++ ) System.out.print(array[i][j] + " ");
//            System.out.println();
//        }
        int max = -1;
        // 横向,从左到右
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 17; j++) {
                int multi = 1;
                for (int k = 0; k < 4; k++) {
                    multi *= array[i][j + k];
                }
                max = multi > max ? multi : max;
            }
        }
        // 竖向,从上到下
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 20; j++) {
                int multi = 1;
                for (int k = 0; k < 4; k++) {
                    multi *= array[i + k][j];
                }
                max = multi > max ? multi : max;
            }
        }
        // 右对角线,从左下到右上
        for (int i = 3; i < 20; i++) {
            for (int j = 0; j < 17; j++) {
                int multi = 1;
                for (int k = 0; k < 4; k++) {
                    multi *= array[i - k][j + k];
                }
                max = multi > max ? multi : max;
            }
        }
        // 左对角线,从左上到右下
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 17; j++) {
                int multi = 1;
                for (int k = 0; k < 4; k++) {
                    multi *= array[i + k][j + k];
                }
                max = multi > max ? multi : max;
            }
        }
        // 输出最大值
        System.out.println(max);
    }
}

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值