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; }}
阅读全文
0 0
- java语言程序设计 第十一章(11.8 11.9)
- java语言程序设计 第十一章 11.3
- 《java语言程序设计》第十一章学习总结
- java语言程序设计基础 第十一章 继承11.2
- Java语言程序设计(基础篇)原书第十版第十一章答案
- java语言程序设计基础篇第十一章编程练习题
- java语言程序设计 第八章(8.35、8.36)
- java语言程序设计 第七章(7.21、7.35)
- java语言程序设计 第十三章 (13.6、13.7)
- java语言程序设计 第十三章 (13.17、13.18)
- chapter1(JAVA语言程序设计)
- Java语言程序设计(序)
- java编程思想(第十一章)
- java第十一章
- JAVA第十一章复习
- Java语言程序设计 第13章
- java 语言程序设计 第十三章 13.5
- Java语言程序设计2(20)
- 数据结构-栈的顺序存储
- banner的使用
- 线性顺序表
- C++模板的具体化和实例化
- 自定义view随机数点击事件
- java语言程序设计 第十一章(11.8 11.9)
- 【JSON】生成JSON数据的方式
- Visual Studio 下的第一个 C++ 项目
- bzoj1016 [JSOI2008]最小生成树计数
- OkHttp
- bzoj1257[CQOI2007]余数之和sum 数论
- vue跨域解决方法
- 如何实现windows10与virtual box虚拟机中的ubuntu之间的文件共享(如何把windows的文件送给虚拟机中的Linux)
- 启动Eclipse报Initializing Java Tooling错误解决方法