JAVA 第三次习题集-设计与分析
- 7-1 创建账户类Account
题目分析:
设计一个名称为Account的类,具体包括:
- id:账号,私有属性,整型,默认值为0;
- balance:余额,私有属性,实型,默认值为0;
- annualInterestRate:当前利率,私有属性,实型,默认值为0,假设所有帐户均有相同的利率;
- dateCreated:账户开户时间,私有属性,LocalDate类型,默认为2020年7月31日;
- 一个能创建默认账户的无参构造方法;
- 一个能创建带特定id和初始余额的账户的构造方法;
- id、balance、annualInterstRate的getter及setter方法;
- dateCreated的getter方法;
- 一个名为getMonthlyInterestRate()的方法返回月利率(月利率计算公式:余额*(年利率/1200));
- 一个名为withDraw的方法从账户提取特定数额,当提取数额大于余额或为负数系统返回
WithDraw Amount Wrong提示; - 一个名为deposit的方法向账户存储特定数额,当存储数额大于20000元或为负数系统返回
Deposit Amount Wrong提示。
编写一个测试程序:
- 创建一个账户,其账户id、余额及利率分别有键盘输入,账户开户时间取系统当前时间;
- 输入取钱金额,系统进行取钱操作,如果取钱金额有误,则输出提示信息后系统继续运行;
- 输入存钱金额,系统进行存钱操作,如果存钱金额有误,则输出提示信息后系统继续运行;
- 系统输出,以如下格式分别输出该账户余额、月利息以及开户日期(输出实型数均保留两位小数)
我提交的源码:
1 import java.util.*; 2 import java.time.LocalDate; 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 int id = input.nextInt(); 8 double balance =input.nextDouble(); 9 double annualInterestRate =input.nextDouble(); 10 double tiqu = input.nextDouble(); 11 double cunqu = input.nextDouble(); 12 Account account = new Account(id, balance,annualInterestRate); 13 14 account.withDraw(tiqu); 15 account.deposit(cunqu); 16 System.out.println("The Account'balance:"+String.format("%.2f",account.getBalance())); 17 System.out.println("The Monthly interest:"+String.format("%.2f",account.getMonthlyInterestRate())); 18 19 System.out.println("The Account'dateCreated:"+account.getDateCreated()); 20 } 21 22 } 23 class Account { 24 25 private int id = 0; 26 private double balance = 0 ; 27 private double annualInterestRate = 0; 28 private LocalDate dateCreratde; 29 public Account(int Id,double Balance,double a) { 30 this.id = Id; 31 this.balance = Balance; 32 this.annualInterestRate =a ; 33 } 34 public int getId() { 35 return id; 36 } 37 public void setId(int id) { 38 this.id = id; 39 } 40 public double getBalance() { 41 return balance; 42 } 43 public void setBalance(double balance) { 44 this.balance = balance; 45 } 46 47 48 public double getAnnualInterestRate() { 49 return annualInterestRate; 50 } 51 52 53 public void setAnnualInterestRate(double annualInterestRate) { 54 this.annualInterestRate = annualInterestRate; 55 } 56 public double getMonthlyInterestRate(){ 57 return (balance*(annualInterestRate / 1200)); 58 } 59 60 public LocalDate getDateCreated() { 61 62 dateCreratde = LocalDate.of(2020,07,31); 63 return dateCreratde; 64 65 } 66 67 public void withDraw(double a ) { 68 69 if(a>balance||a<0){ 70 System.out.println("WithDraw Amount Wrong"); 71 72 } 73 74 else{ 75 this.balance -=a; 76 } 77 } 78 public void deposit(double a ) { 79 80 if(a>20000||a<0){ 81 System.out.println("Deposit Amount Wrong"); 82 } 83 else{ 84 this.balance +=a; 85 } 86 } 87 88 89 }

设计分析与心得:
这个题,更加让类的概念深入我的编程思想,创建一个账户类,在这个类中实现各个功能,包括定义用户名,设置年利率和,以及计算账户的余额,让我对面向对象程序设计产生了浓厚的兴趣,越来类可以实现丰富的功能,我也明白了老师上课反复强调的复杂的软件工程问题,也是这个程序设计题让我对面向对象程序设计产生了浓厚的i兴趣,把功能封装在一个类里面,再去使用,这是C语言无法实现的一点。
- 7-2 定义日期类 (28 分)
题目分析:
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:
我提交的源码:
1 import java.util.*; 2 import java.text.DecimalFormat; 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 int year = input.nextInt(); 8 int month =input.nextInt(); 9 int day =input.nextInt(); 10 Account account = new Account(year,month,day); 11 12 if(account.checkinoutValidity()) 13 { 14 DecimalFormat df = new DecimalFormat("0"); 15 16 account.getNextDate(); 17 System.out.println("Next day is:"+account.getYear()+"-"+account.getMonth()+"-"+df.format(account.getDay())); 18 // System.out.println(account.getYear()); 19 // System.out.println(account.getMonth()); 20 // System.out.println(df.format(account.getDay())); 21 22 } 23 } 24 25 } 26 class Account { 27 28 private int year = 0; 29 private int month = 0 ; 30 private int day = 0; 31 private int panduan = 0; 32 public boolean checkinoutValidity() { 33 if(month>0&&month<=12){ 34 35 if(isLeapYear(year)) 36 { 37 if(day<=month1[month]&&day>0) 38 panduan=1; 39 } 40 else 41 { 42 if(day<=month2[month]&&day>0) 43 panduan=1; 44 45 } 46 } 47 48 49 if(year<1900||year>2000||month<1||month>12||day<1||day>31||panduan==0) { 50 System.out.println("Date Format is Wrong"); 51 return false; 52 } 53 return true; 54 55 } 56 public static boolean isLeapYear(int year){ 57 if((year%4==0&&year%100!=0)||year%400==0) { 58 return true; 59 } 60 else 61 return false; 62 } 63 int[] month2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; 64 int[] month1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31}; 65 66 public Account(int a ,int b,int c) { 67 this.year = a; 68 this.month = b; 69 this.day = c; 70 } 71 public int getYear() { 72 return year; 73 } 74 public void setYear(int year) { 75 this.year = year; 76 } 77 public int getMonth() { 78 return month; 79 } 80 public void setMonth(int month) { 81 this.month = month; 82 } 83 public double getDay() { 84 return day; 85 } 86 public void setDay(int day) { 87 this.day = day; 88 } 89 public void getNextDate() { 90 if(isLeapYear(this.year)==true ) { 91 this.day+=1; 92 if(day>month1[month]) { 93 this.day=this.day-month1[month]; 94 this.month+=1; 95 if(this.month>12) { 96 this.month -=12; 97 this.year+=1; 98 } 99 } 100 } 101 else { 102 this.day+=1; 103 if(day>month2[month]) { 104 this.day=this.day-month2[month]; 105 this.month+=1; 106 if(this.month>12) { 107 this.month -=12; 108 this.year+=1; 109 } 110 } 111 112 } 113 } 114 115 }

设计分析与心得:
这个题在习题集二中已经出现过了一次,唯一不同的是添加了一个类的使用方法,这个题的全部测试点都通过了,将求下一天的功能封装在一个类中,然后在类里面实现对下一天的求解
- 7-3 一元多项式求导(类设计)
题目分析:
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书
我提交的源码:
1 import java.util.*; 2 public class Main { 3 4 public static void main(String[] args) { 5 6 Scanner input = new Scanner(System.in); 7 char zm[]= new char[30]; 8 String string=input.nextLine(); 9 String th = string.replaceAll("[ ]", ""); 10 Account account = new Account(th); 11 int zm1[] = new int[30]; 12 String PowerFunction = "((([-+]?([1-9][0-9]*)?(\\*x(\\^[+-]?([1-9][0-9]*))?)))*"; //幂函数正则 13 String C = "(([1-9][0-9]*|[-+]([1-9][0-9]*)))*"; //常数正则 14 String Term = ""; // 15 if(account.trueorFalse(th)) { 16 if(th.matches(C)) { 17 System.out.println("0"); 18 } 19 20 21 } 22 else { 23 System.out.println("Wrong Format"); 24 } 25 26 27 } 28 29 } 30 class Account { 31 32 private String s; 33 34 public Account(String zf) { 35 this.s = zf; 36 } 37 public boolean trueorFalse(String s) {//判断是否为合法多项式 38 if (s.matches("^(([-+]([1-9][0-9]*)(\\*x(\\^[+-]?([1-9][0-9]*))?))|(([1-9][0-9]*)\\*(x(\\^[+-]?([1-9][0-9]*))?))|([-+](x(\\^[+-]?([1-9][0-9]*))?))|([-+]([1-9][0-9]*))|(([1-9][0-9]*))|((x(\\^[+-]?([1-9][0-9]*))?)))+$")) 39 return true; 40 else 41 return false; 42 } 43 44 }

设计分析与心得:
这个题是三次题目集中最难的一个题,涉及到了正则表达式,这个题满分有50 分,我只得到了15分原因有以下原因:
原因有以下原因:
1.正则表达式没有完全掌握;
2.有关将每一项的系数与指数提取出来的算法没有掌握
3.怎样在字符串处理的同时匹配,幂函数的正则表达式再进行运算没有实现
4.耐心恒心不够,没有坚持下来,一开始我就觉得特别难,然后就放弃了,没有意识到老师说的困难要突破,
5.学习Java的主要是应该坚持下来,我一遇到困难就退缩,怎么能学好Java,我应该在每次突破难题中寻找快感,而不是遇到难题就放弃,我应该好好反思一i下自己
今后改进:
1.遇到困难迎难而上,不退缩,相信自己能够战胜
2.学习正则表达式,将这道题突破。
3.Java 一定要提前学这样才能跟上老师,课后将老师上课讲的内容好好的回顾一下,这样才能做到,温故而知新。
4.遇到困难,敢于挑战。
浙公网安备 33010602011771号