第一次博客作业
一、前言
对于前三次的题目来说,题量适中,难度也适中,知识点较多,对于中等学生来说较为适合,题目大多为十天期限,对提高写作业效率有较大帮助,在写程序时问题出现较多,常常需要花大量时间来调试,对写程序的效率有较大的影响,常常要两三个小时来完成作业,这也就导致了有时实验报告无法按时提交的情况,但是在写程序的过程中,能让我学到更多知识,无论是上网查询到的知识还是同学告诉的知识,都使我不断地获取新知识,使我受益匪浅,在与他人交流题目的过程,也是增长我与他人交流能力的过程,对于今后的发张也有着至关重要的作用,在想算法的时候,也锻炼了我的思维能力,对我今后对各个事情的思考也有很大帮助,在写程序的同时,因为想不出该怎么下手也常常感到厌烦,但这也磨练了我的耐心,使我在面对比较大的程序时采用分块的思想,将问题进行模块化,将这些小模块进行一个一个解决,锻炼自己的思维。
前几次的题目总体来说适中,但有一些题目对于没有预习的同学来说可能比较困难,特别是之前没有学到正则表达式的时候,题目中大量要采用正则表达式,倘若有同学没有对这个正则表达式进行预习,那写程序的时候就会感到很艰难,甚至无法下手,单从Java框架来说是没问题,但是对于主题算法就比较的困难,正则表达式针对字符串有一些特殊的操作,因此在之后的Java学习中要不断地进行提前学习,而不能跟在老师的后面,那样的话就无法真正学到知识,这也就要求我们软件工程师要不断地学习,建立终生学习的人生目标,对于题目集三来说,最难的就是第三题了,对于正则表达式无从下手,对于从未接触过正则表达式的我来说,写程序莫过于举步维艰,但前两题对于我来说就还行,没有那么难,通过对书上的知识和网站的知识都能很好地解决建立账户类、求下一天的要求,但是在写程序时无法建立正确的算法,常常无法通过测试点,我知道自己的问题后马上就会去eclipse上进行进行调试,看看是不是算法问题,将问题进行不断思考,若实在思考不出来就去问班上编程能力强的同学,再将其自我量化,将知识进行吸收。
对于前几次作业的知识点来说,知识点覆盖的较为全面,刚学java时将其与c语言进行对比起来学,刚开始进行对过程的程序过渡到面向对象的程序,给了我们熟悉的感觉,对我们的入门也比较友好,在写面向过程的程序来说,主要是为了解决某一个存在的问题,而面向对象的程序主要是为了解决某个对象要完成的功能,对我们的思考也有一定挑战,同时,在写程序的同时,也要注意数据的各种形式,方法的类型,类的调用等等,对我们的细心也有一定的考察。
二、设计与分析
1、对于第一题来说,题目要求输入三角形的三条边判断三角形的类型,同时也说明了输入的数据类型为实数型,这就要求输入的时候要定义各条边要定义为double类型,不能将数据类型进行更改,否则就会导致程序无法运行,程序里只有一个main方法,对输入的三条边输入进行判断,输出三角形的类型。
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
if(a<1||a>200||b<1||b>200||c<1||c>200)
System.out.println("Wrong Format");
else if((a+b<=c)||(b+c<=a)||(a+c<=b))
System.out.println("Not a triangle");
else if((a==b)||(b==c)||(a==c))
{
if((a==b)&&(b==c))
System.out.println("Equilateral triangle");//等边三角形
else if((Math.abs(c*c-a*a-b*b)<=0.00001)||(Math.abs(a*a-c*c-b*b)<=0.00001)||(Math.abs(b*b-a*a-c*c)<=0.00001))
System.out.println("Isosceles right-angled triangle");//等腰直角三角形
else
System.out.println("Isosceles triangle");//等腰三角形
}
else if( (Math.pow(a,2)+Math.pow(b,2)==Math.pow(c,2))||(Math.pow(a,2)+Math.pow(c,2)==Math.pow(b,2))||(Math.pow(b,2)+Math.pow(c,2)==Math.pow(a,2)))
System.out.println("Right-angled triangle");//直角三角形
else
System.out.println("General triangle");//普通三角形
}
}

2、第二题为输入一个日期求下一天,这个题目非常有挑战性,涉及到很多种情况,分别有二月的天数的考虑,输入的合法性,是否每个月都有31号,若月份为12月份31号则年份要加一,月份和日都要变成一等等情况,对于思维有一定的挑战性,对于那些2对生活常理不是很明白的同学来说有一定的挑战性,要求我们软件工程师在生活中要对各方面的东西有洞察力,利用程序来思考问题。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year=input.nextInt();
int month=input.nextInt();
int day=input.nextInt();
Main m=new Main(year,month,day);//构造一个对象,类型为main;
m.getNextDate(year, month, day);//
}
int year;
int month;
int day;
public Main(int year,int month,int day){//静态方法不能引用实例变量或实例方法
this.year=year;//左边的year为成员year,
this.month=month;
this.day=day;
}
public int getYear(){
return year;
}
public void setYear(int year) {
this.year=year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month=month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day=day;
}
public static boolean isLeapYear(int year){
if((year%4==0&&year%100!=0)||year%400==0)
return true;
else
return false;
}
public static boolean checkInputValidity(int year,int month,int day){
if( (year<1900||year>2000||month<1||month>12||day<1||day>31) || ((isLeapYear(year)==true&&month==2&&day>29) || (isLeapYear(year)==false&&month==2&&day>28) ) )
{
return false;
}
else {
return true;
}
}
public static void getNextDate(int year,int month,int day) {
int []m=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(checkInputValidity(year,month,day)==true)
{
if(isLeapYear(year)==true)
{
m[2]=29;
}
else
{
m[2]=28;
}
if(month==2&&isLeapYear(year)==true&&day==29)
{
month=month+1;
day=1;
}
else if(month==2&&isLeapYear(year)!=true&&day==28)
{
month=month+1;
day=1;
}
else if(month==12&&day==m[12]) {
year=year+1;
month=1;
day=1;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
{
month=month+1;
day=1;
}
else if((month==4||month==6||month==9||month==11)&&day==30)
{
month=month+1;
day=1;
}
else{
day=day+1;
}
System.out.println("Next day is:"+year+"-"+month+"-"+day);
}
else
{
System.out.println("Format is Wrong");
}
}
}


3、第三题要求输入一个日期,输出该日期的前n天,和第二题其实大同小异,只需改一些重要的算法。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year=input.nextInt();
int month=input.nextInt();
int day=input.nextInt();
Main m=new Main(year,month,day);//构造一个对象,类型为main;
m.getNextDate(year, month, day);//
}
int year;
int month;
int day;
public Main(int year,int month,int day){//静态方法不能引用实例变量或实例方法
this.year=year;//左边的year为成员year,
this.month=month;
this.day=day;
}
public int getYear(){
return year;
}
public void setYear(int year) {
this.year=year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month=month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day=day;
}
public static boolean isLeapYear(int year){
if((year%4==0&&year%100!=0)||year%400==0)
return true;
else
return false;
}
public static boolean checkInputValidity(int year,int month,int day){
if( (year<1900||year>2000||month<1||month>12||day<1||day>31) || ((isLeapYear(year)==true&&month==2&&day>29) || (isLeapYear(year)==false&&month==2&&day>28) ) )
{
return false;
}
else {
return true;
}
}
public static void getNextDate(int year,int month,int day) {
int []m=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(checkInputValidity(year,month,day)==true)
{
if(isLeapYear(year)==true)
{
m[2]=29;
}
else
{
m[2]=28;
}
if(month==2&&isLeapYear(year)==true&&day==29)
{
month=month+1;
day=1;
}
else if(month==2&&isLeapYear(year)!=true&&day==28)
{
month=month+1;
day=1;
}
else if(month==12&&day==m[12]) {
year=year+1;
month=1;
day=1;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
{
month=month+1;
day=1;
}
else if((month==4||month==6||month==9||month==11)&&day==30)
{
month=month+1;
day=1;
}
else{
day=day+1;
}
System.out.println("Next day is:"+year+"-"+month+"-"+day);
}
else
{
System.out.println("Format is Wrong");
}
}
}
4、第四题要求创建账户类Account,在一行内分别输入账户id、初始余额、当前利率、提取金额、存储金额,数据间采用一个或多个空格分隔,分三行输出,分别为约、计算的月利息以及开户日期,对数据的输入与输出形式有一定要求,要求我们对账户类及逆行定义并计算该账户的余额,该题难度不大。
import java.util.Scanner;//shuhru
import java.time.*;//shijainbao
public class Main{
public static void main(String[] args){
Scanner input =new Scanner(System.in);
int id=input.nextInt();//输入id
double balance=input.nextDouble();//输入余额
double annualInterestRate=input.nextDouble();//输入年利率
double quqian=input.nextDouble();//shurucunqianshu
double cunqian=input.nextDouble(); //shuruquqianshu
Main account=new Main(id,balance,annualInterestRate);
if(quqian<0||quqian>balance) {
System.out.println("WithDraw Amount Wrong");}
else {
account.withDraw(quqian);
}
if(cunqian>20000||cunqian<0) {
System.out.println("Deposit Amount Wrong");
}
else
{
account.deposit(cunqian);
}
System.out.printf("The Account'balance:%.2f\n",account.getBalance());
System.out.printf("The Monthly interest:%.2f\n",account.getMonthlyInterstRate());
System.out.printf("The Account'dateCreated:"+account.getdateCreated());
}
private int id=0;//初始化id
private double balance=0;//初始化余额
private double annualInterestRate=0;//初始化年利率
private LocalDate riqi=LocalDate.of(2020,Month.JULY,31);//初始化创建日期
public Main(int id,double balance,double annualInterestRate) {
this.id=id;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
// getdateCreated=new LocalDate();
//dateCreated=new LocalDate();
// this.dateCreated=new LocalDate;
// LocalDate=new java.util.Date();
}
public int getId() {
return id;
}
public void setId(int id){
this.id=id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance=balance;
}
public double getAnnualInterstRate() {
return annualInterestRate;
}
public void setAnnualInterstRate(double annualInterstRate) {
this.annualInterestRate=annualInterstRate;
}
public LocalDate getdateCreated(){
return riqi;
}
public double getMonthlyInterstRate() {
return balance*(annualInterestRate/1200);//月利率算法
}
public void withDraw(double quqian) {
balance=balance-quqian;//取钱
}
public void deposit(double cunqian) {
balance=balance+cunqian;//存钱
}
}
5、第五题要球创建日期类,实质上与第三题没差别,代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year=input.nextInt();
int month=input.nextInt();
int day=input.nextInt();
Main m=new Main(year,month,day);//构造一个对象,类型为main;
m.getNextDate(year, month, day);//
}
int year;
int month;
int day;
public Main(int year,int month,int day){//静态方法不能引用实例变量或实例方法
this.year=year;//左边的year为成员year,
this.month=month;
this.day=day;
}
public int getYear(){
return year;
}
public void setYear(int year) {
this.year=year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month=month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day=day;
}
public static boolean isLeapYear(int year){
if((year%4==0&&year%100!=0)||year%400==0)
return true;
else
return false;
}
public static boolean checkInputValidity(int year,int month,int day){
if( (year<1900||year>2000||month<1||month>12||day<1||day>31) || ((isLeapYear(year)==true&&month==2&&day>29) || (isLeapYear(year)==false&&month==2&&day>28) ) )
{
return false;
}
else {
return true;
}
}
public static void getNextDate(int year,int month,int day) {
int []m=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(checkInputValidity(year,month,day)==true)
{
if(isLeapYear(year)==true)
{
m[2]=29;
}
else
{
m[2]=28;
}
if(month==2&&isLeapYear(year)==true&&day==29)
{
month=month+1;
day=1;
}
else if(month==2&&isLeapYear(year)!=true&&day==28)
{
month=month+1;
day=1;
}
else if(month==12&&day==m[12]) {
year=year+1;
month=1;
day=1;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31)
{
month=month+1;
day=1;
}
else if((month==4||month==6||month==9||month==11)&&day==30)
{
month=month+1;
day=1;
}
else{
day=day+1;
}
System.out.println("Next day is:"+year+"-"+month+"-"+day);
}
else
{
System.out.println("Format is Wrong");
}
}
}
三、踩坑心得
1、对于第一题,刚开始并没有对三角形的种类进行分类,而只是简单将各种情况进行罗列,导致最后测试点无法全部通过,从而无法拿到满分,特别是对于等腰直角三角形这种特殊的三角形,要将其放在有两边相等的情况里,若没有进行分类,则说明三角形的类型没有搞清楚,要重新对三角形有一个认识,对三角形要有明确的分类,而不仅仅是对三角形的各种情况进行罗列,而且数据类型定义的时候一定要明确该变量到底是什么类型,否则会出语法上的错误,还有就是对math库里面的函数调用一定要注意格式,调用时要书写成"Math.",否则就会调用出现错误,但不用在main方法前写import java.util.math。
2、在写定义账户类的时候,无法对类进行正确的定义,常常报错,在查询相关资料后了解类的相关知识后能进行正确的定义。
3、在数据类型的定义的时候,常常搞错数据的类型,无法定义数据的类型,需要我们在读题目的时候认真读题,将数据正确认知并定义。
四、改进建议
1、对于第一题,可以多定义几个方法,体现Java程序的封装性,而不应该把所有的算法放在一个main函数里头,而且也无法显示出java的面向对象的优势。
2、在定义方法的时候,常常不知道如何定义,常常定义在main函数里,在eclipse里头常常报错,但是如果指定义一个main函数,把其他内容书写进去的话就显得头重脚轻,所以一定要能正确定义方法,还有方法的类型。
五、总结
1、写完了这些题目的时候,我学到了正则表达式里面的各种表达方式,以及正则表达式里的各种函数,了解了它们各自的作用,java中大部分情况是对字符串的操作,正则表达式有着非常重要的作用,我充分理解了正则表达式的重要作用,并且明白了这对我们今后的写程序来说是很有用的,我认为在今后的学习中要更加变得耐心,对于从未见过的知识要敢于接受,内化于心,而不是害怕接受新知识,在正则表达式的知识方面还有许多知识要学习,正则表达式里面还有很多函数需要去了解,需要我不断去探索。
2、在上java课的时候发现常常许多人没有听课,都在自顾自的学,这对我们的学习很不利,我建议老师多与同学互动,而不是干讲知识点,而且建议老师发布题目时要根据学生的具体完成情况来调整时间,而不仅仅是只设置一个截止时间,在许多同学都没完成作业的情况下,要了解情况后进行调整时间。
浙公网安备 33010602011771号