第一次Blog作业总结01
第一次Blog作业总结01
一 、前言
第一次题目集主要知识点涉及:Java基本语法,if、switch等语法运用,int、char、double等类型的掌握。。
第二次题目集主要知识点涉及:考察类与对象的运用。
第三次题目集主要知识点涉及:考察类的封装型与正则表达式的使用。
题目集的题量数逐渐减少,但难度由易入难,逐渐递增。
二 、设计与分析
7-8题:判断三角形类型,输入三角形三边,判断该三角形是什么类型的三角形。
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double a=in.nextDouble();
double b=in.nextDouble();
double c=in.nextDouble();
if(a<1 || a>200 || b<1 || b>200 || c<1 || c>200)
System.out.println("Wrong Format");
else if((a+b<=c)||(a+c<=b)||(b+c<=a))
System.out.println("Not a triangle");
else if(a==b && b==c)
System.out.println("Equilateral triangle");
else if((a == b || b ==c || a == c) && (a*a+b*b-c*c <0.000001 || a*a+c*c-b*b <0.000001 || c*c+b*b-a*a<0.000001))
System.out.println("Isosceles right-angled triangle");
else if(a==b||b==c||a==c)
System.out.println("Isosceles triangle");
else if(a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
System.out.println("Right-angled triangle");
else
System.out.println("General triangle");
}
}
分析:这题我通过使用if、else语句分别判断各种情况并输出相应结果。当两边之和大于第三边即数据合法(1)如果输入数据非法,则输出“Wrong Format”; (2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”; (3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”; (3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”; (5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”; (6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”; (7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。
(2)题目集2:
7-2题:合并两个升序排序的整数型数组为一个新的升序整数型数组并输出。
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] a;
int[] b;
int[] c;
int n = in.nextInt();
a = new int[n];
for(int i = 0;i < a.length; i++) {
a[i] = in.nextInt();
}
int m = in.nextInt();
b = new int[m];
for (int j = 0; j < b.length; j++) {
b[j] = in.nextInt();
}
c = new int[n+m];
for(int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for(int j = 0; j < b.length;j++) {
c[a.length+j] = b[j];
}
for(int j = 0; j < c.length-1; j++) {
for(int i = j+1; i < c.length; i++) {
if(c[i]<c[j]){
int temp = a[j];
c[j] = c[i];
c[i] = temp;
}
}
}
for(int i =0; i < c.length; i++){
System.out.print(c[i]+" ");
}
}
}
分析:这题我通过创建第三个c数组,将输入的a、b两个数组的值分别赋给c,数组的长度为a、b数组长度之和,并通过选择排序数组c后输出。
7-3:判断闰年星期几。
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
int days = numOfDays(year,month,day);
if( year <= 2020 && year >= 1820 && month >= 1 && month <= 12 && (day >= 1 && day <=31) ){
if(leapYear(year) ){
System.out.println(year+" is a leap year.");
System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(days)+".");
}
else{
if(month == 2 && day >28){
System.out.println("Wrong Format");
}
else{
System.out.println(year+" is not a leap year.");
System.out.println(year+"-"+month+"-"+day+" is "+getWhatDay(days)+".");
}
}
}
else
System.out.println("Wrong Format");
}
//判断year是否为闰年,返回boolean类型;
public static boolean leapYear(int year){
boolean isleapYear;
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
isleapYear = true;
}
else
isleapYear = false;
return isleapYear;
}
//求出year-month-day到0001-1-1的距离天数,返回整型数;
public static int numOfDays(int year,int month,int day){
int days = 0;
int i ;
int []a = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
for(i = 1;i < year;i++){
if(leapYear(i)) {
days+=366;
}
else
days+=365;
}
if(leapYear(year))
a[2]=29;
for(i = 1;i < month;i++){
days+=a[i];
}
days+=day;
return days;
}
//根据天数返回星期几,其中参数days为天数,整型数,返回星期几的英文单词
public static String getWhatDay(int days){
String week = new String();
switch (days % 7){
case 0:
week = "Sunday";
break;
case 1:
week = "Monday";
break;
case 2:
week = "Tuesday";
break;
case 3:
week = "Wednesday";
break;
case 4:
week = "Thursday";
break;
case 5:
week = "Friday";
break;
case 6:
week = "Saturday";
break;
}
return week;
}
}
分析:首先,我创建了三个方法:leapYear方法判断year是否为闰年,返回boolean类型;numofDays方法求出year-month-day到0001-1-1的距离天数,返回整型数,即计算出当前输入日期离0001年1月1日所差的天数 ;getWhatDay根据天数返回星期几,其中参数days为天数,整型数,返回星期几的英文单词。然后调用这三个方法并输出结果。
7-4 求下一天。输出年月日的值(均为整型数),输出该日期的下一天。
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
if(checkInputValidity(year,month,day)){
System.out.print("Next date is:");
nextDate( year,month, day);
}
else
System.out.println("Wrong Format");
}
//判断输入日期是否合法,返回布尔值
public static boolean checkInputValidity(int year,int month,int day) {
if( year <= 2020 && year >= 1820 && month >= 1 && month <= 12 && (day >= 1 && day <=31) ){
if(isleapYear(year) ){
return true;
}
else{
if(month == 2 && day >28){
return false;
}
else{
return true ;
}
}
}
else
return false;
}
//判断year是否为闰年,返回boolean类型;
public static boolean isleapYear(int year){
boolean isleapYear;
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
isleapYear = true;
}
else
isleapYear = false;
return isleapYear;
}
//求输入日期的下一天
public static void nextDate(int year,int month,int day){
int Month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isleapYear(year))
Month[2] = 29;
if(day < Month[month]){
System.out.println(year+"-"+month+"-"+(day+1));
}
else{
if(month < 12){
System.out.println(year+"-"+(month+1)+"-1");
}
else
System.out.println((year+1)+"-1-1");
}
}
}
分析:首先,有4个方法public static void main(String[] args);//主方法 public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值 public static void nextDate(int year,int month,int day) ; //求输入日期的下一天 。调用以上方法并输出结果。这题有几点注意:注意输入数据的边界、注意2月份情况、注意12月份情况、注意每月有30天和31天的情况。
7-5:求前N天。
题目集3:输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
int n = in.nextInt();
if(checkInputValidity(year,month,day,n)){
System.out.print(n+" days ago is:");
nextDate(year,month,day,n);
}
else
System.out.println("Wrong Format");
}
//判断输入日期是否合法,返回布尔值
public static boolean checkInputValidity(int year,int month,int day,int n) {
if( year <= 2020 && year >= 1820 && month >= 1 && month <= 12 && (day >= 1 && day <=31) ){
if(isleapYear(year) ){
return true;
}
else{
if(month == 2 && day >28){
return false;
}
else{
return true ;
}
}
}
else
return false;
}
//判断year是否为闰年,返回boolean类型;
public static boolean isleapYear(int year){
boolean isleapYear;
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
isleapYear = true;
}
else
isleapYear = false;
return isleapYear;
}
//输出n天后的日期
public static void nextDate(int year,int month,int day,int n){
int Month[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isleapYear(year))
Month[2] = 29;
if(day-n >= 1 && day - n <= Month[month]) {
System.out.println(year+"-"+month+"-"+(day-n));
}
else{
if(month < 12 && month > 1 ){
if(n <= 0)
System.out.println(year+"-"+(month+1)+"-"+(-n-(Month[month]-day)));
else
System.out.println(year+"-"+(month-1)+"-"+(Month[month-1]-(n-day)));
}
else {
if( n <= 0)//当n<=0时已经知道 month = 12
System.out.println((year+1)+"-1-"+(-n-(Month[month]-day))); // month = 12;
else
System.out.println((year-1)+"-12-"+(Month[12]-(n-day))); // month = 1;
}
}
}
}
分析:首先在7-4方法public static void main(String[] args);//主方法 public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值 的基础上创建方法public static void nextDay//输出n天后的日期,并调用方法输出结果。
注意n为负值的情况。
(3)题目集三:
7-2:定义日期类。
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
Date date = new Date(year,month,day);
if(date.checkInputValidity()) {
System.out.print("Next day is:");
date.NextDate();
}
else {
System.out.println("Date Format is Wrong");
}
}
}
class Date{
private int year =0;
private int month =0;
private int day = 0;
public Date(int year,int month,int day){
this.year = year;
this.month =month;
this.day = day;
}
public int getYear(int year) {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth(int month) {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay(int day) {
return day;
}
public void setDay(int day) {
this.day = day;
}
public boolean isLeapYear() {
boolean isleapYear;
if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
isleapYear = true;
}
else
isleapYear = false;
return isleapYear;
}
//判断输入日期是否合法,返回布尔值
public boolean checkInputValidity(){
if( year <= 2000 && year >= 1900 && month >= 1 && month <= 12 && (day >= 1 && day <=31) ){
if(isLeapYear() ){
return true;
}
else{
if(month == 2 && day >28){
return false;
}
else{
return true ;
}
}
}
else
return false;
}
//求输入日期的下一天
public void NextDate(){
int Month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear())
Month[2] = 29;
if(day < Month[month]){
System.out.println(year+"-"+month+"-"+(day+1));
}
else{
if(month < 12){
System.out.println(year+"-"+(month+1)+"-1");
}
else
System.out.println((year+1)+"-1-1");
}
}
}
分析:Date类结构如下图所示:

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。类中所用方法参考题目集2 中7-4题。
7-3:一元多项式求导。编写程序性,实现对简单多项式的导函数进行求解。
- 分析:对表达式合法性校验建议采用正则表达式(自学)
- 注意表达式中存在空格的情况(需要先滤掉空格)
- 建议尝试使用数组或者List(ArrayList或者LinkedList)存储识别的每个表达式中的项。
三 、踩坑心得
1、题目集1.7-8 有一个样例点一直过不了。经请教同学发现一处代码错误,将(a*a+b*b-c*c <1 || a*a+c*c-b*b <1 || c*c+b*b-a*a<1))
改为(a*a+b*b-c*c <0.000001 || a*a+c*c-b*b <0.000001 || c*c+b*b-a*a<0.000001)) 。原来是要注意int、double的范围。
2、在题目集2.7-3判断闰年及星期几这题中利用定义一个数组int []a = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};代替一直用if、else语句判断某月对应的天数。
3、在题目集3.刚开始写7-2是为熟练掌握类的封装性,经过改进实现类的封装性。
4、在题目集3. 7-3中未认真学习掌握正则表达式导致这题未写出来.
四 、改进建议
1、题目集2.7-2合并两个有序数组为新的有序数组 中对数组c排序时我使用了选择排序法:for(int j = 0; j < c.length-1; j++) {
for(int i = j+1; i < c.length; i++) {
if(c[i]<c[j]){
int temp = a[j];
c[j] = c[i];
c[i] = temp;
}
}
}
后通过学习我发现Java中可以import一个类Arrays,其中有一个方法sort可以直接调用进行排序.
import Java.util.Arrays;
Array.sort(c);
改进代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] a;
int[] b;
int[] c;
int n = in.nextInt();
a = new int[n];
for(int i = 0;i < a.length; i++) {
a[i] = in.nextInt();
}
int m = in.nextInt();
b = new int[m];
for (int j = 0; j < b.length; j++) {
b[j] = in.nextInt();
}
c = new int[n+m];
for(int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for(int j = 0; j < b.length;j++) {
c[a.length+j] = b[j];
}
Arrays.sort(c);
for(int i =0; i < c.length; i++){
System.out.print(c[i]+" ");
}
}
}
2、待经过更多学习继续发现代码不足再来补充.
五 、总结。
1、通过本阶段三次题目集的综合性总结,我学习了Java的基本语法,学习了Java中对类的运用,了解了Java类的封装性带来的益处.
2、需要进一步学习类之间的关系,并熟练掌握.正则表达式还需要认真学习并学会使用.
3、多了解类的建模的设计,注意代码的复杂度的,提升代码的质量.
4、在老师的通过优秀的线上线下教学下,需要更加认真的学习,提升自身能力.
浙公网安备 33010602011771号