第一次博客总结
1.前言:通过一个月的学习,对前三次作业做一次总结
第一次作业:
题目分析:第一次作业较简单,C语言代码大家都练习过,因此算法部分其实并非难点。对于大多数刚开始接触Java同学来说:主要是要熟悉java的语法,控制语句
,循环语句。还没提到面向对象的思想,其中最有代表性的是判断三角形类型的题,犯得是一些基础语法的错误,比如if的嵌套,循环的次数错误等。SourceMonitor的生成报表内容以及PowerDesigner的相应类图比较简单。
输入三角形三条边,判断该三角形为什么类型的三角形。
输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。
输出格式:
(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”。
输入样例1:
在这里给出一组输入。例如:
50 50 50.0
输出样例1:
在这里给出相应的输出。例如:
Equilateral triangle
输入样例2:
在这里给出一组输入。例如:
60.2 60.2 80.56
输出样例2:
在这里给出相应的输出。例如:
Isosceles triangle
输入样例3:
在这里给出一组输入。例如:
0.5 20.5 80
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
代码如下:
import java.util.*;
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){
if(a+b>c&&b+c>a&&a+c>b)
{
if(a==b||b==c||a==c||Math.pow(a,2)+Math.pow(b,2)==Math.pow(c,2)||Math.pow(b,2)+Math.pow(c,2)==Math.pow(a,2)||Math.pow(a,2)+Math.pow(c,2)==Math.pow(b,2))
{
if(a==b||b==c||a==c)
{
if(a==b&&b==c&&a==c){
System.out.println("Equilateral triangle");
}
else{
System.out.println("Isosceles triangle");
}
}
if(Math.pow(a,2)+Math.pow(b,2)==Math.pow(c,2)||Math.pow(b,2)+Math.pow(c,2)==Math.pow(a,2)||Math.pow(a,2)+Math.pow(c,2)==Math.pow(b,2)){
if(a==b||b==c||a==c){
System.out.println("Isosceles right-angled triangle");
}
else{
System.out.println("Right-angled triangle");
}
}
}
else
System.out.println("General triangle");
}
else{
System.out.println("Not a triangle");
}}
else
System.out.println("Wrong Format");
}}
类图和sourse分析如下:


第二次作业:
题目分析:第二次作业要比第一次更难一点,如果第一次作业是为了让我们知道有Java的存在,第二次就是让我们熟练掌握java的基础语法。算法比一次要更难,不管是(7-4)求某年的下一天还是(7-5)求某年的前几天。可以用date类的方法,但老师不让。还没有提到类的概念,要熟练掌握构造方法。
7-4示例:
输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。
要求:Main类中必须含有如下方法,签名如下:
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) ; //求输入日期的下一天
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
- 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
输入样例1:
在这里给出一组输入。例如:
2020 3 10
输出样例1:
在这里给出相应的输出。例如:
Next date is:2020-3-11
输入样例2:
在这里给出一组输入。例如:
2025 2 10
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
代码示例:
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();
if(month == 2){
if(day>28){
System.out.println("Wrong Format");
System.exit(0);
}
}
if(year>=1820&&year<=2020&&month>0&&month<13&&day>0&&day<32){
nextDate(year,month,day);
}
else
System.out.println("Wrong Format");
}
public static boolean isLeapYear(int year){//判断year是否为闰年,返回boolean类型;
boolean years = false;
if(year % 4==0&&year%100 != 0||year%400 ==0) {
years = true;
}
return years;
}
public static void nextDate(int year,int month ,int day){
int nextday = 0;
switch (month){
case 1: if(month == 1){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 3: if(month == 3){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 4: if(month == 4){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 5: if(month == 5){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 6: if(month == 6){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 7: if(month == 7){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 8: if(month == 8){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 11: if(month == 11){
if(day == 30) {
nextday = 1;}
else
nextday = day + 1;
}
case 12: if(month == 12){
if(day == 31) {
nextday = 1;
year++;
}
else
nextday = day + 1;
}
case 9: if(month == 9){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 10: if(month == 10){
if(day == 31) {
nextday = 1;
}
else {
nextday = day + 1;
}
}
}
if(month == 2){
if(year % 4==0&&year%100 != 0||year%400 ==0){
if(day == 29) {
nextday = 1;
}
else {
nextday = day + 1;
}
}
else{
if(day == 28)
nextday = 1;
else
nextday = day + 1;
}
}
if(nextday == 1){
month++;
}
if(month == 13){
month = 1;
}
System.out.println("Next date is:"+year+"-"+month+"-"+nextday);
}
}
7-5:求前几天:
输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。
输入格式:
在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。
输出格式:
- 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
- 当输入数据合法时,输出“n days ago is:年-月-日”
输入样例1:
在这里给出一组输入。例如:
2018 6 19 8
输出样例1:
在这里给出相应的输出。例如:
8 days ago is:2018-6-11
输入样例2:
在这里给出一组输入。例如:
2018 6 19 -8
输出样例2:
在这里给出相应的输出。例如:
-8 days ago is:2018-6-27
代码示例:
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();
int n = input.nextInt();
if(month == 2){
if(day>28){
System.out.println("Wrong Format");
System.exit(0);
}
}
if(year>=1820&&year<=2020&&month>0&&month<13&&n<=10&&n>=-10&&day>0&&day<32){
nextDate(year,month,day,n);
}
else
System.out.println("Wrong Format");
}
public static void nextDate(int year,int month ,int day,int n){
int nextday = 0;
nextday = day - n;
switch (month){
case 1: if(month == 1){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday<1){
nextday = nextday+31;
month = 12;
year--;
}
}
case 3: if(month == 3){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday < 1){
nextday = nextday + isLeapYear(year);
month--;
}
}
case 4: if(month == 4){
if(nextday > 30){
nextday = nextday-30;
month++;
}
if(nextday < 1){
nextday = nextday + 31;
month--;
}
}
case 5: if(month == 5){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday < 1){
nextday = nextday + 30;
month--;
}
}
case 6: if(month == 6){
if(nextday > 30){
nextday = nextday-30;
month++;
}
if(nextday < 1){
nextday = nextday + 31;
month--;
}
}
case 7: if(month == 3){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday < 1){
nextday = nextday + 30;
month--;
}
}
case 8: if(month == 8){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday < 1){
nextday = nextday + 31;
month--;
}
}
case 9: if(month == 9){
if(nextday > 30){
nextday = nextday-30;
month++;
}
if(nextday < 1){
nextday = nextday + 31;
month--;
}
}
case 10: if(month == 10){
if(nextday > 31){
nextday = nextday-31;
month++;
}
if(nextday < 1){
nextday = nextday + 30;
month--;
}
}
case 11: if(month == 11){
if(nextday > 30){
nextday = nextday-30;
month++;
}
if(nextday < 1){
nextday = nextday + 31;
month--;
}
}
case 12: if(month == 12){
if(nextday > 31){
nextday = nextday-31;
month=1;
year++;
}
if(nextday < 1){
nextday = nextday + 30;
month--;
}
}
}
if(month == 2){
if(year % 4==0&&year%100 != 0||year%400 ==0){
if(day>29){
System.out.println("Wrong Format");
System.exit(0);
}
if(nextday > 29) {
nextday = nextday-29;
month++;
}
if(nextday<1){
nextday = nextday+31;
month--;
}
}
else{
if(nextday > 28) {
nextday = nextday-28;
month++;
}
if(nextday<1){
nextday = nextday+31;
month--;
}
}
}
System.out.println(n+" "+"days ago is:"+year+"-"+month+"-"+nextday);
}
public static int isLeapYear(int year){
int month2 = 28;
if(year % 4==0&&year%100 != 0||year%400 ==0){
month2 = 29;
}
return month2;
}
}
类图和分析如下:



第三次作业:
题目分析:这一次题目家相较上次又有难度,有所增加,而且这次题目以及引入了类的概念,累的几个基本属性关联,聚集,继承。还有类的属性是公有属性还是私有属性
如何访问类的变量,类里面的各种方法,真正的开始了面向对象的这个思想。还有如何使用正则表达式,如何构造方法,用getter方法和setter方法,是一次质的飞跃。
例如幂函数的求导,让人很头痛。
7-2求后一天
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:

输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
- 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
- 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
输入样例1:
在这里给出一组输入。例如:
1912 12 25
输出样例1:
在这里给出相应的输出。例如:
Next day is:1912-12-26
输入样例2:
在这里给出一组输入。例如:
2001 2 30
输出样例2:
在这里给出相应的输出。例如:
Date Format is Wrong
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();
if(month == 2){
if(isLeapYear(year)){
if(day>29){
System.out.println("Date Format is Wrong");
System.exit(0);
}
}
else{
if(day>28){
System.out.println("Date Format is Wrong");
System.exit(0);
}
}
}
Date nextday = new Date( year,month,day);
if(year>=1900&&year<=2000&&month>0&&month<13&&day>0&&day<32){
nextday.getDate( year, month,day);
}
else
System.out.println("Date Format is Wrong");
}
public static boolean isLeapYear(int year){//判断year是否为闰年,返回boolean类型;
boolean years = false;
if(year % 4==0&&year%100 != 0||year%400 ==0) {
years = true;
}
return years;
}
}
class Date{
private int year;
private int month;
private int day;
public Date(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void getDate(int year,int month ,int day){
int nextday = 0;
switch (month){
case 1: if(month == 1){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 3: if(month == 3){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 4: if(month == 4){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 5: if(month == 5){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 6: if(month == 6){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 7: if(month == 7){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 8: if(month == 8){
if(day == 31) {
nextday = 1;
}
else
nextday = day + 1;
}
case 11: if(month == 11){
if(day == 30) {
nextday = 1;}
else
nextday = day + 1;
}
case 12: if(month == 12){
if(day == 31) {
nextday = 1;
year++;
}
else
nextday = day + 1;
}
case 9: if(month == 9){
if(day == 30) {
nextday = 1;
}
else
nextday = day + 1;
}
case 10: if(month == 10){
if(day == 31) {
nextday = 1;
}
else {
nextday = day + 1;
}
}
}
if(month == 2){
if(year % 4==0&&year%100 != 0||year%400 ==0){
if(day == 29) {
nextday = 1;
}
else {
nextday = day + 1;
}
}
else{
if(day == 28)
nextday = 1;
else
nextday = day + 1;
}
}
if(nextday == 1){
month++;
}
if(month == 13){
month = 1;
}
System.out.println("Next day is:"+year+"-"+month+"-"+nextday);
}
}
类图和分析如下:
![]()

7-3幂函数的求导:
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf
输入格式:
在一行内输入一个待计算导函数的表达式,以回车符结束。
输出格式:
- 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
- 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
- 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
- 当输出结果第一项系数符号为“+”时,不输出“+”;
- 当指数符号为“+”时,不输出“+”;
- 当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
输出格式见输入输出示例。
输入样例1:
在这里给出一组输入。例如:
-2* x^-2+ 5*x^12-4*x+ 12
输出样例1:
在这里给出相应的输出。例如:
4*x^-3+60*x^11-4
输入样例2:
在这里给出一组输入。例如:
2*x^6-0*x^7+5
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
代码如下:
●●●
Import Java.Math.bgIntegers
import java. util .ArrayList;
import java . util . Scanner ;
import java . util . regex . Matcher ;
import java . util. regex. Pattern;
public class Main {
public static void main(String[] args)
Scanner input = new Scanner (System. in) ;
String initExpress = input.nextL ine();
String express = initExpress . replaceAll("\\s", "");
1:
Express textExpress = new Express(express);
if (textExpress . check())
textExpress . show();
else
System . out . println( "Wrong Format" );
class Express
private string express;
pr1vate string term;
private string ratstr;// 系数部分
private string indexStr;// 指数部分
private BigInteger rat;
private BigInteger index ;
private ArrayList<string> containTerm = new ArrayList<>();
public Express(String express) {
this.express = express;
// System . out . println( express);
String termExpress
"([+- ]?[1-9][0-9]*)?" + "(\*?[+-]?(\^([+- ]?[1-9][0-9]*))?)?";
String wholeExpress = "(([+-]?[1-9][0-9]*)?" + "(\\*?[+-]?x(\\^([+-]?[1-9][0-9]*))?)?)+";
String constNum = "[+-]?[0-9]+";
/样例-2* x^-2+ 5*x^12-4*x+ 12
public boolean check() {
// Pattern p = Pattern. compile(termExpress);
// atcher m = p.matcher(express);
if (Pattern . matches (wholeExpress, express))
return true;
else
return false;
public void show() {
(Pattern. matches ( constNum, express))
system out nrintln("g").
CAPTESS) )
System. exi+(q).
System
Pattern
Pattern p = Pattern. compile(termExpress);
Matcher m = p.matcher(express);
1nt flag
while (m.
ratstr = m.group(1);
indexStr = m. group(4);
if (ratStr
= null)
rat = new BigInteger(ratStr);
else if (m. group(2). startsWith("- ")
at = BigInteger . value0f(-1);
else
rat = BigInteger .value0f(1);
// System. out. println( ratStr)
if (indexStr != null)
index = new BigInteger(indexStr);
rat = rat . multiply( index);
index = index. subtract(new BigInteger("1"));
f (rat. compareTo(new BigInteger("0")) > 0 && flag == 1)
System. out.print("+" + (rat. equals(new BigInteger("-1")) ? "-x" : rat + "*x")
( index. equals(new BigInteger("1")) ?””: ("A" + index)));
else
flag = 1;
System. out . print( (rat . equals( new BigInteger("-1")) ? "-x" : rat + "*x")
(index . equals(new BigInteger("1")) ? "”: ("^" + index)));
} else if (m.group(2) != null
System . out . print (m. group(1));
}
踩坑心得:符号有时会写错,还有数组越界,还有就是字符串会出错,还有强制类型转换等等。
改进建议:代码的圈复杂度太高了,还有就是累的命名不够准确,不能随意用字母命名,而是专门的英语单词,要加强英语的学习。
还有函数的功能,要做到单一性函数的功能太复杂了,尽量少用if语句。
多去学习库中有的原方法,正则表达式的配对要加强
总结:总算是进入了学习java的大门,说实话,吃了一点C语言的老板,但到了类的概念和面向对象的思想,我觉得这是很重要的,希望老师强调一下预习的重要性,没预习的话,根本做不出来

浙公网安备 33010602011771号