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);
}
}
效果图: