Blog总结报告
一前言
pta上已经发布3次实验作业,难度递增特别是第三次,题目量虽少,但难度极高。三次实验考查到方面很广,包括但不限于java基本语法,数据的输入于输出,字符串的各种用法(获取字符串长度,从字符串中获得字符,链接字符串,转换字符串,从控制台读取字符串/字符,字符串的比较,查找字符串中的指定字符或字符串,字符串与数值之间的转换),多维数组,对象和类(对对象定义,创建对象,引用变量访问对象,使用java、库中的类)等等...
其中实验一中,主要考察java的基本语法和各种关于字符串的方法,总共12题,其中第10题难度非常大,涉及各种复杂的操作,到现在我也没弄明白。此外其余题目主要考察的是逻辑算法,语法主要是考察字符串的操作,难度适中。实验二中已经涉及到类的简单操作,有第一次实验的基础,反而更有经验了,题目难度不高,没有特别难攻克的点,完成情况也是这3次实验中最好的。比较有新意的是第八题判断三角形的类别,语法上没有难点,主要是逻辑上需要考虑各种情况,非常考察学生的全面逻辑。实验三,共4题,前三题难度适中,第四题难度很高,都是在考察类的创建和使用。前三题类的功能比较单一也易于设计,而第四题要求类中还需好几个功能,且设计到java中的日期方法,较为的复杂。
二设计与分析
例子 题集3,7-3
源码:

import java.util.*;
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();
Date d=new Date( year, month, day);
if(d.checkInputValidity(year, month, day)){
d.nextDate( year, month, day);
}
else{
System.out.println("Date Format is Wrong");
}
}
}
class Date{
private int year;
private int month;
private int day;
static int[] mon_maxnu1 =new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] mon_maxnu2 =new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
public Date(){
}
public Date(int year,int month,int day){
this.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 boolean checkInputValidity(int year,int month,int day){
if(isLeapYear( year)){
if(year>=1900&&year<=2000&&month>=1&&month<=12&&day>=1&&day<=mon_maxnu2[this.month]){
return true;
}
else return false;
}
else{
if(year>=1900&&year<=2000&&month>=1&&month<=12&&day>=1&&day<=mon_maxnu1[this.month]){
return true;
}
else return false;
}
}
public boolean isLeapYear(int year){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}
else return false;
}
public void nextDate(int year,int month,int day){
if(isLeapYear(year)){
if(day== mon_maxnu2[this.month]&&month!=12){
month=month+1;
day=1;}
else if(month==12){
if(day==31){
year=year+1;
month=1;
day=1;
}
}
else{
day=day+1;
}
}
else{
if(day== mon_maxnu1[this.month]&&month!=12){
month=month+1;
day=1;}
else if(month==12){
if(day==31){
year=year+1;
month=1;
day=1;
}
}
else{
day=day+1;
}
}
System.out.println("Next day is:"+year+"-"+month+"-"+day);
}
}
例子 题集3,7-4
源码:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;
int choice = input.nextInt();
if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity(year, month ,day)) {
System.out.println("Wrong Format");
System.exit(0);
}
m = input.nextInt();
if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(date.getyear() + "-" + date.getmonth() + "-" + date.getday() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity(year,month,day)) {
System.out.println("Wrong Format");
System.exit(0);
}
n = input.nextInt();
if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
System.out.print(
date.getyear() + "-" + date.getmonth() + "-" + date.getday() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());
DateUtil fromDate = new DateUtil(year, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
if (fromDate.checkInputValidity(year,month,day) && toDate.checkInputValidity(year,month,day)) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class DateUtil{
private int year;
private int month;
private int day;
static int[] mon_maxnu1 =new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] mon_maxnu2 =new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
public DateUtil(){
}
public DateUtil(int year,int month,int day){
this.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 boolean checkInputValidity(int year,int month,int day){
if(isLeapYear( year)){
if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day<=mon_maxnu2[this.month]){
return true;
}
else return false;
}
else{
if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day<=mon_maxnu1[this.month]){
return true;
}
else return false;
}
}
public boolean isLeapYear(int year){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}
else return false;
}
DateUtil getNextNDays(int m){
int y2, m2, d2;
int D;
y2=year;
m2=month;
d2=day;
if(!isLeapYear(year)){
D=365;
}
else{
D=366;
}
while(true){
if(m/D<=0)
break;
m=m-D;
y2++;
}
if((m+d2) <= mon_maxnu1[m2]){
d2 = m+d2;
}
else
{
d2 = m + d2 - mon_maxnu1[m2];
m2++;
if(m2>=12)
{
y2++;
m2 = m2%12;
}
}
year=y2;
month=m2;
day=d2;
return this;
}
public DateUtil getPreviousNDays(int n) {
return null ;
}
public boolean compareDates(DateUtil date) {
return false;
}
public boolean equalTwoDates(DateUtil date) {
return false;
}
public int getDaysofDates(DateUtil date) {
return day ;
}
public String showDate() {
return (year+"-"+month+"-"+day) ;
}
}
三 踩坑心得

答案错误级存在逻辑问题没用考虑到闰年2月的天数
算法错误即存在算法问题,算法过于复杂
还有部分功能代码因实力有限未能完成,所以存在多处非零返回
7-3

3个答案错误其中两个位考虑到闰年二月天数,一个为没考虑跨年天数变化。
改正后:

已完成全部要求.
四 总结
收获与心得:
通过这三次实验的整理与学习,我在感觉java编程方面有了很大的进步,同时也更好的理解的面向对象的编程思路,懂得程序设计的可用性以及代码编写的规范性!并且拥有了更好的代码执行经验。
有些知识点以前没有学过,但我也没有去研究,实验时突然间觉得自己真的有点无知,虽然现在去看依然可以解决问题,但要浪费许多时间,这一点是我必须在以后的学习中加以改进的地方,同时也要督促自己在学习的过程中不断的完善自我。另外一点,也是在实验中必不可少的部分,就是同学之间的互相帮助。所谓*者迷,旁观者清,有些东西感觉自己做的是时候明明没什么错误,偏偏程序运行时就是有错误,让其他同学帮忙看了一下,发现其实是个很小的错误。所以说,相互帮助是很重要的一点,这在以后的工作或生活中也是很关键的。俗话说:要想为事业多添一把火,自己就得多添一捆材。此次实验,我深深体会到了积累知识的重要性。在实验当中我们遇到了不少难题,但是经过我们大家的讨论和老师细心的一一指导,问题得到了解决。两个月的实验结束了,收获颇丰,同时也更深刻的认识到要做一个合格的程序员并非我以前想像的那么容易,最重要的还是细致严谨。社会是不会要一个一无是处的人的,所以我们要更多更快地从一个学生向工作者转变,总的来说我对这次实习还是比较满意的,它使我学到了很多东西,为我以后的学习做了引导,点明了方向。
在标识符、变量、以及数据类型方面,通过学习,我了解到标识符明白必须规范,不能随便定义,当以下划线(_)、美元符号($)等此类符号定义时,方可通过java编译,否则则会运行报错,另外在定义变量时,int,double也要注意区分,虽然都能进行计算,但是其精度是不一样的!在java中一共有8中数据类型,分别是int,long,short,float,double,char,boolean,byte,除此之外,在创建多个变量时不能重名,并且一定在变量赋值之后才能用,同一条语句中可以定义多条变量。
其次在对数组的使用时,有俩种方式定义数组,一种是直接new Object[int i]另外一种是直接在定义的数组后边写{}在里面赋值即可。在使用数组时,我常常遇到报错问题,例如访问数组的元素超过了索引的范围,程序则会抛出java.lang.ArrayIndexOutOfBoundsException。告诉我数组越界,这时我将重新判断数组,以保证程序正常运行。学习数组之后,我了解到集合,并且知道ArrayList用于存储相同的数据或对象的底层也是数组!而集合在java中相当重要。
通过学习,我也了解到了各种类的使用,使我对java的了解又增添了一大步!
在对类的使用,以及面向对象的实现——类的实例化,方法,构造参数的使用,我也收获颇丰,在使用类时,我常报找不到main方法,原因有 2其一为我在文件编译时加入了Package,其二为没有定义主方法,只是写了类的私有方法导致报错!在实例化对象时,我有时也会犯错,在继承的时候,我将父类强转为子类,导致程序报错!并且未在父类中定义无参构造方法时,在其子类中写了有参构造方法,导致子类中的有参构造方法一直报错!我积极排查错误!其次,在调用类中的属性时,因为类中的私有属性不能被继承,只能靠重写来实现。在代码运行时,常发生找不到方法,原因为我未在类中定义其方法!
在完成实验中,我遇到了许多问题,每一次我都积极去排查错误,然后将错误铭记,以保证在以后的程序设计中避开它们,完成好每一次的程序设计,做好每一个程序,也可以更好的学习java。

浙公网安备 33010602011771号