java语言程序设计 第十一章(11.8 11.9)

来源:互联网 发布:无法解析域名错误105 编辑:程序博客网 时间:2024/06/05 18:41

程序小白,希望和大家多交流,共同学习
11.8
这里写图片描述

public 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 void setType(char type)    {        this.type = type;    }    public char getType()    {        return type;    }    public void setAmount(double amount)    {        this.amount = amount;    }    public double getAmount()    {        return amount;    }    public void setBalance(double balance)    {        this.balance = balance;    }    public double getBalance()    {        return balance;    }    public void setDescription(String description)    {        this.description = description;    }    public String getDescription()    {        return description;    }}
public class NewAccount{    private int id;    private double balance;    private double annualInterestRate;    private java.util.Date date;    private String name;    private java.util.ArrayList<Transaction> record = new java.util.ArrayList<>();    public NewAccount()    {        date = new java.util.Date();    }    public NewAccount(int id, double balance)    {        this.id = id;        this.balance = balance;    }    public NewAccount(String name, int id, double balance)    {        this.name = name;        this.id = id;        this.balance = balance;    }    public void setName(String name)    {        this.name = name;    }    public String getName()    {        return name;    }    public void setId(int id)    {        this.id = id;    }    public int getId()    {        return id;    }    public void setBalance(double balance)    {        this.balance = balance;    }    public double getBalance()    {        return balance;    }    public void setAnnualInterestRate(double annualInterestRate)    {        this.annualInterestRate = annualInterestRate;    }    public double getAnnualInterestRate()    {        return annualInterestRate;    }    public double getMontylyInterestRate()    {        return annualInterestRate / 12;    }    public java.util.ArrayList<Transaction> getRecord()    {        return record;    }    public void withdraw(double amount)    {        balance = balance - amount;        String description = "取款: " + amount + "\t剩余:" + balance;        record.add(new Transaction('W', amount, balance, description));    }    public void deposit(double amount)    {        balance = balance + amount;        String description = "存款:" + amount + "\t剩余:" + balance;        record.add(new Transaction('D', amount, balance, description));    }}
import java.util.Scanner;import java.util.ArrayList;public class TestNewAccount{    public static void main(String[] args)    {        NewAccount person1 = new NewAccount("George", 1122, 1000);        person1.setAnnualInterestRate(1.5);        person1.withdraw(30);        person1.withdraw(40);        person1.withdraw(50);        person1.deposit(5);        person1.deposit(4);        person1.deposit(2);        ArrayList<Transaction> person1Record = person1.getRecord();        System.out.println("Name: " + person1.getName() + "\nAnnual Interest Rate: "             + person1.getAnnualInterestRate() + "\nBalance: " + person1.getBalance());        System.out.println("\n交易记录:");        for (Transaction description : person1Record)        {            System.out.println(description.getDescription());        }    }}

11.9(最大的行和列)
编写程序,随机经0和1填入一个 n * n 的矩阵中,打印该矩阵,并且找到具有最多1的行和列。
提示:使用两个ArrayList来存储具有最多1的行和列的下表

import java.util.ArrayList;import java.util.Scanner;public class MaxRowColumn{    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);        System.out.print("Enter the array size n: ");        int size = input.nextInt();        int[][] array = creat(size);        printArray(array);        ArrayList<Integer> rowIndex = rowFind(array);        ArrayList<Integer> columnIndex = columnFind(array);        System.out.print("The largest row index: ");        for (int index : rowIndex)        {            System.out.print(index + ", ");        }        System.out.print("\nThe largest row index: ");        for (int index : columnIndex)        {            System.out.print(index + ", ");        }        System.out.println();    }    public static int[][] creat(int size)    {        int[][] array = new int[size][size];        for (int row = 0; row < size; row++)        {            for (int column = 0; column < size; column++)            {                array[row][column] = (int)(Math.random() * 2);            }        }        return array;    }    public static void printArray(int[][] array)    {        for (int i = 0; i < array.length; i++)        {            for (int j = 0; j < array[0].length; j++)            {                System.out.print(array[i][j]);            }            System.out.println();        }    }    public static ArrayList<Integer> rowFind(int[][] array)    {        ArrayList<Integer> rowMax = new ArrayList<>();        rowMax.add(new Integer(0));        int max = 0;        for (int row = 0; row < array.length; row++)        {            int count = 0;            for ( int column = 0; column < array[0].length; column++)            {                if (array[row][column] == 1)                {                    count++;                }            }            if (count > max)            {                rowMax.clear();                rowMax.add(new Integer(row));            }            else if (count == max)            {                rowMax.add(new Integer(row));            }        }        return rowMax;    }    public static ArrayList<Integer> columnFind(int[][] array)    {        ArrayList<Integer> columnMax = new ArrayList<>();        columnMax.add(new Integer(0));        int max = 0;        for (int column = 0; column < array[0].length; column++)        {            int count = 0;            for ( int row = 0; row < array.length; row++)            {                if (array[row][column] == 1)                {                    count++;                }            }            if (count > max)            {                columnMax.clear();                columnMax.add(new Integer(column));            }            else if (count == max)            {                columnMax.add(new Integer(column));            }        }        return columnMax;    }}
原创粉丝点击