题目1-3总结
一.前言
java1-3题目集包含了大量的知识点,包括类的创建,正则表达式的使用,对象的创建,还有构造方法的使用(包含无参构造和有参构造),方法的调用和设计一个输出给定日期的下一天的方法,无参构造器和有参构造器都是一样的,只不过一个有参数一个没有而已。
一般创建类的时候,如果不写构造器,那么这个类有一个默认的无参构造器。如果你写了一个有参的构造器,那么那个无参的构造器就被覆盖了。
本次题目集的题量不算太大,但是完成这些题目的难度稍微有些大,花费了我大量的时间和精力,比如题目集3的第三题,难度偏大,需要使用正则来判断输入的求导式子是否合法。
二.设计与分析
1.Account
我在Account类里面创建了一个账户对象,写了一个无参构造方法和有参构造方法,在main类里面可以调用该方法new一个新的对象。Account里面包括了getId(),setId(),getbalance(),setbalance(),getAnnualInterestRate(),setAnnualInterestRate()方法。
这些方法能获取用户的信息和设置用户的信息,然后Accouunt类里面还有取钱方法和存钱方法WithDraw(Double)和Deposit(Double),该方法可以判断取钱金额和存钱金额是否合法,如果不合法则返回错误提示,合法则返回 true。
然后进行相应的余额扣除或余额增加操作。Account类里面的Getinterestrate( )方法可以计算月利率。然后在Main类里面调用Account里的方法,实现了所要求的功能。
2.定义日期类
在Data类里面写了获取信息方法和设置信息方法,然后还要判断输入日期是否合法的方法Check(),还要一个获取下一天的方法getNextData(),在Main类里面调用Data类的方法,实现了获取输入日期下一天的功能。
三.心得
1.Account
(1)Account类
import java.time.LocalDate;
import java.util.Scanner;
public class Account {
private int id;
private double balabce;
private double annualInterestRate;
private LocalDate dateCreated;
public Account(){
id = 0;
balabce = 0;
annualInterestRate = 0;
dateCreated = LocalDate.of(2020,7,31);
}
public Account(int id,double balabce){
this.id=id;
this.balabce=balabce;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public double getBalance(){
return balabce;
}
public void setBalabce(double balabce){
this.balabce=balabce;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate=annualInterestRate;
}
public LocalDate getDateCreated()
{
return dateCreated=LocalDate.now();
}
public boolean WithDraw(double getmoney){
if(getmoney>this.balabce||getmoney<0){
System.out.println("WithDraw Amount Wrong");
return false;
}
else{
return true;
}
}
public boolean Deposit(double depositmoney){
if(depositmoney>20000||depositmoney<0){
System.out.println("Deposit Amount Wrong");
return false;
}
else{
return true;
}
}
public double Getinterestrate( ){
double rate;
rate=getBalance()*(this.annualInterestRate/1200);
return rate;
}
}
(2) Main类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
double getmoney ;
double depositmoney;
double rate;
int id=sc.nextInt();
double balance=sc.nextDouble();
Account account =new Account(id,balance);
account.setAnnualInterestRate(sc.nextDouble());
getmoney =sc.nextDouble();
depositmoney=sc.nextDouble();
if(account.WithDraw(getmoney)){
account.setBalabce(account.getBalance()-getmoney);
}
if(account.Deposit(depositmoney)){
account.setBalabce(account.getBalance()+depositmoney);
}
System.out.print(String.format("The Account'balance:%.2f\n",account.getBalance()));
System.out.print(String.format("The Monthly interest:%.2f\n",account.Getinterestrate()));
System.out.print("The Account'dateCreated:2020-07-31");
}
}
2.输出给出日期下一天
import java.util.Scanner;
public class Nextdate {
public static int days[] = new int[13];
public static void main(String[] args) {
days[1]=31;days[2]=28;days[3]=31;days[4]=30;
days[5]=31;days[6]=30;days[7]=31;days[8]=31;
days[9]=30;days[10]=31;days[11]=30;days[12]=31;
Scanner input = new Scanner(System.in);
while(input.hasNext()) {
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
if( year < 1600 || year > 2100 || month < 1 || month > 12 || day < 1 || day > 31 ) {
System.out.println("输入数据非法");
continue;
}
if(judgeLeap(year)) { //闰年
days[2] = 29;
} else {
days[2] = 28;
}
if(day > days[month]) {
System.out.println("输入数据非法");
continue;
}
day++; //计算下一天
if(day > days[month]) {
day = 1;
month++; //跳下一个月
if(month > 12) {
month = 1;
year++; //跳下一个年
}
}
System.out.println(year + " " + month + " " + day);
}
}
public static boolean judgeLeap(int year) {
if( (year%4 == 0 && year%100 != 0) || year%400 == 0 )
return true;
else
return false;
}
}
四.总结
通过这几次的题目集练习,我学会了类的创建,正则表达式的使用,对象的创建,还有构造方法的使用(包含无参构造和有参构造),方法的调用和设计一个输出给定日期的下一天的方法,能够独立解决一些不太复杂的实际问题,然后我的代码编写能力和代码编写速度需要进一步提高,正则表达式还不够熟练,需要进一步学习。
浙公网安备 33010602011771号