2021-05-18

2021-05-18

2021-05-18

2021-05-18 

package 课堂练习;
import java.util.Date;
public class ch07a {
        public static void main(String[] args){
            Account account = new Account(1122,20000);
            if(account.withDraw(2500))
                System.out.println("Success");
            else
                System.out.println("Fail");

            if(account.deposit(2500))
                System.out.println("Success");
            else
                System.out.println("Fail");

            System.out.println("balance:" + account.getBalance());
            System.out.println("monthly interest:" + account.getMonthlyInterestRate() * account.getBalance());
            System.out.println(account.getDate().toString());
        }
    }
    class Account{
        private int id;
        private double balance;
        private double annualInterestRate;
        private Date dateCreated;

        public Account(){
            dateCreated = new Date();
            id = 0;
            balance = 0;
            annualInterestRate = 0;
        }

        public Account(int ID, double BALANCE){
            dateCreated = new Date();
            id = ID;
            balance = BALANCE;
            annualInterestRate = 0.045;
        }

        public int getId(){
            return id;
        }

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

        public double getBalance(){
            return balance;
        }

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

        public double getAnnualInterestRate(){
            return annualInterestRate;
        }

        public void setAnnualInterestRate(double annual){
            annualInterestRate = annual;
        }

        public Date getDate(){
            return dateCreated;
        }

        public double getMonthlyInterestRate(){
            return annualInterestRate / 12.0;
        }

        public boolean withDraw(double money){
            if(balance >= money){
                balance -= money;
                return true;
            }
            else
                return false;
        }

        public boolean deposit(double money){
            if(money >= 0){
                balance += money;
                return true;
            }
            else
                return false;
        }
    }

package 课堂练习;

import java.util.ArrayList;
import java.util.Date;

public class ch07b {
    public static void main(String[] args) {
        //用于为账户存储交易
        //ArrayList<Object> transactions=new ArrayList<>();

        NewAccount object = new NewAccount("George", 1122, 1000);
        object.setAnnualInterestRate(0.015);

        object.deposit(30);
        object.deposit(40);
        object.deposit(50);

        object.withDraw(5);
        object.withDraw(4);
        object.withDraw(2);

        System.out.println("账户持有者名字: " + object.getName()
                + "\n利率: " + object.getAnnualInterestRate()
                + "\n收支额: " + object.getBalance()
                + "\n所有的交易:" + object.toString());
    }
}

 class NewAccount {

    private String name;

    //用于为账户存储交易
    private ArrayList<Object> transactions = new ArrayList<>();

    public int id = 0;//用户名
    public double balance = 0;//余额
    public double annualInteresRate = 0;//当前利率
    public java.util.Date dateCreated;//存储开户日期

    //无参构造方法
    public NewAccount() {
        Date dateCreated = new Date();
        this.dateCreated = dateCreated;

    }

    //有参构造方法
    public NewAccount(int id, double balance) {
        Date dateCreated = new Date();
        this.dateCreated = dateCreated;
        this.id = id;
        this.balance = balance;
    }

    //一个新的有参构造方法
    public NewAccount(String name, int id, double balance) {
        Date dateCreated = new Date();
        this.dateCreated = dateCreated;
        this.id = id;
        this.balance = balance;
        this.name = name;
    }

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

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

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInteresRate = annualInterestRate;
    }

    public int getId() {
        return id;

    }

    public double getBalance() {
        return balance;

    }

    public String getName() {
        return name;

    }

    public double getAnnualInterestRate() {
        return annualInteresRate;

    }

    public double getMonthlyInterestRate() {
        return annualInteresRate / 12;

    }

    public String getDateCreated() {
        return dateCreated.toString();

    }


    //取钱
    public double withDraw(double withDarw) {

        //创建一笔取钱交易
        Transaction withDrawTransacte = new Transaction('W', withDarw, (this.balance = this.balance - withDarw), "取款:" + withDarw + "美元");
        transactions.add(withDrawTransacte.getDescription());

        return this.balance;

    }


    //存钱
    public double deposit(double deposit) {
        //创建一笔存钱交易
        Transaction depositTransacte = new Transaction('D', deposit, (this.balance = this.balance + deposit), "存款:" + deposit + "美元");
        transactions.add(depositTransacte.getDescription());

        return this.balance;
    }

    public String toString() {
        return "\n日期: " + getDateCreated() + "\n" + transactions.toString();
    }
}

 class Transaction {
    //交易日期
    private java.util.Date date;

    //交易类型,例如'W','D'
    private char type;

    //交易量
    private double amount;

    //交易后的新余额
    private double balance;

    //交易描述
    private String description;


    //一个有参构造方法
    public Transaction(char type, double amount, double balance, String description) {
        this.type=type;
        this.amount=amount;
        this.balance=balance;
        this.description=description;

        //创建一个交易日期
        date=new Date();
        this.date=date;
    }

    //获得交易日期
    public String getDate() {
        return date.toString();

    }

    //设置交易日期
    public void setDate(long eclapseTime) {
        date.setTime(eclapseTime);
    }

    //交易类型:‘W’——取款,‘D’——存款
    public char getType() {
        return type;

    }

    public void setType(char type) {
        this.type=type;
    }

    public double getAmount() {
        return amount;

    }

    public void setAmount(double amount) {
        this.amount=amount;
    }

    public double getBalance() {
        return balance;

    }

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

    public String getDescription() {
        return description;

    }

    public void setDescription(String description) {
        this.description=description;
    }
}

 

package 课堂练习;

public class ch07c {
    public static void main(String[] args)

    { int max=0;

        int i,j,sum1,sum2,sum3,sum4;//i和j是定义的变量,sum1是横向的计算,sum2是纵向的计算

//sum3是右下向的计算,sum4是左下向的计算,为什么我不算左上方和右下方?因为和右下向,左下向重合了

        int a[][]= {    {8, 2, 22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91,8},

                        {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62,0},

                        {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},

                        {52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},

                        {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},

                        {24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},

                        {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},

                        {67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},

                        {24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},

                        {21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},

                        {78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},

                        {16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},

                        {86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},

                        {19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},

                        { 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},

                        {88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},

                        { 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},

                        {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},

                        {20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},

                        { 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}

                };

        for(i=0;i<16;i++)

            for(j=0;j<16;j++)

            {sum1=a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];

                if(sum1>max)

                    max=sum1;

            }

        System.out.print("max1 is:");//横向

        System.out.println(max);

        for(i=0;i<16;i++)

            for(j=0;j<16;j++)

            {sum2=a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j];

                if(sum2>max)

                    max=sum2;

            }

        System.out.print("max2 is:");//纵向

        System.out.println(max);

        for(i=0;i<16;i++)

            for(j=0;j<16;j++)

            {sum3=a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];

                if(sum3>max)

                    max=sum3;

            }

        System.out.print("max3 is:");//斜向右下方

        System.out.println(max);

        for(i=3;i<20;i++)

            for(j=0;j<16;j++)

            {sum4=a[i][j]*a[i-1][j+1]*a[i-2][j+2]*a[i-3][j+3];

                if(sum4>max)

                    max=sum4;

            }

        System.out.print("max4 is:");//斜向左下方

        System.out.println(max);

    }

}

方法二

package 课堂练习;

public class ch07cc {
    //从左到右计算最大值
    public static long leftToRight(int arr[][]) {
        int max = 0;
        int value = 1;
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 17; j++) {
                value = arr[i][j] * arr[i][j + 1] * arr[i][j + 2] * arr[i][j + 3];
                if (value > max) max = value;
            }

        }
        return max;
    }

    //从上到下计算最大值
    public static long upToDown(int arr[][]) {
        int max = 0;
        int value = 1;
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 20; j++) {
                value = arr[i][j] * arr[i + 1][j] * arr[i + 2][j] * arr[i + 3][j];
                if (value > max) max = value;
            }

        }
        return max;

    }

    //从左上到右下计算最大值
    public static long left_uToRight_d(int arr[][]) {
        int max = 0;
        int value = 1;
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 17; j++) {
                value = arr[i][j] * arr[i + 1][j + 1] * arr[i + 2][j + 2] * arr[i + 3][j + 3];
                if (value > max) max = value;
            }

        }
        return max;
    }

    //从右上到左下计算最大值
    public static long right_uToLeft_d(int arr[][]) {
        int max = 0;
        int value = 1;
        for (int i = 3; i < 20; i++) {
            for (int j = 0; j < 17; j++) {
                value = arr[i][j] * arr[i - 1][j + 1] * arr[i - 2][j + 2] * arr[i - 3][j + 3];
                if (value > max) max = value;
            }

        }
        return max;
    }

    public static void main(String[] args) {
        /* 构建20*20整数数组 */
        int arr[][] = {{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
                {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0},
                {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
                {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
                {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
                {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
                {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
                {67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
                {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
                {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
                {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
                {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
                {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
                {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
                {4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
                {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
                {4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
                {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
                {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
                {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48},
        };
        System.out.println("max :" + max(leftToRight(arr), upToDown(arr), left_uToRight_d(arr), right_uToLeft_d(arr)));
    }

    private static long max(long a, long b,
                            long c, long d) {
        long max = a;
        if (b > max) max = b;
       if (c > max) max = c;
       if (d > max) max = d;
        return max;
    }
}

 

上一篇:软件构造——关于并发


下一篇:【力扣笔记22】——括号生成