oop第二次作业
一.前言
第四次作业
涉及知识点
考察
1.降低时间复杂度,使代码效率更高
例如quickSort方法的熟练运用,以及HashSet的使用
点击查看代码
//quickSort的一个示例
public static void quickSort(int[] a,int begin,int end)
{
int i = begin;
int j = end;
int t, temp;
if(i >= j)
return;
temp = a[i];
while(i != j){
while(a[j] >= temp && i < j)
j--;
while(a[i] <= temp && i < j)
i++;
if(i < j){
t = a[j];
a[j] =a [i];
a[i] = t;
}
}
a[begin] = a[i];
a[i] = temp;
quickSort(a, begin, i-1);
quickSort(a, i+1, end);
}
}
2.LocalDate类的使用
(1)日期转换
点击查看代码
LocalDate localDate = LocalDate.parse("2018-02-18");
String formattedDate = localDate.format(DateTimeFormatter.ofPattern("MMM dd, yyyy"));
(2)比较两个日期大小(返回值为true或false)
isAfter(ChronoLocalDate other): 检查此日期是否在给定日期之后。
isBefore(ChronoLocalDate other): 检查此日期是否在给定日期之前。
isEqual(ChronoLocalDate other): 检查此日期是否等于给定日期。
(3)计算两个日期间的时间量
点击查看代码
LocalDate localDate = LocalDate.parse("2018-02-18");
Period period = localDate.until(LocalDate.parse("2018-03-28"));
System.out.println(period.getDays());
long val = localDate.until(LocalDate.parse("2018-03-28"), ChronoUnit.DAYS);
3.使得代码具有封装性
4.其他:
忽略大小写排序
Arrays.sort(str, String.CASE_INSENSITIVE_ORDER)
题量和难度
题量中等,除第一题外,其他题目难度中等。
第五次作业
涉及知识点
考察
正则表达式的基本使用
(1)用正则表达式表示一定范围内的数字,如取值范围为[1,1000)的数字。
([1-9][0-9][0-9][0-9])
含义:首位数字为1-9,第二位为0-9...
(2)([0-9]{3}[a-zA-Z])
含义:前3个数为0-9的数字,第四个为不区分大小写的字母
(3)^2020(1[1-7]|7[1-3]|8[1-2]|61)(0[1-9]|[1-4][0-9])
含义:以2020开头,5、6两个数为1117或7173或81、82或61...
题量和难度
题量中等,题目比较简单
第六次作业
涉及知识点
考察
设计,对类的设计,有哪些类;逻辑,if-else语句较多,需要准确判定条件的符合程度。
二.设计与分析
题目集4
7-1
题目分析
题目给出的信息包括三部分:菜谱,订单(桌号、点菜记录、代点菜记录、删除点菜记录),结束。
根据三部分的信息,截取每一条信息的长度,通过长度判断该信息属于哪一类。
输入:
菜谱包括:菜名、单价
订单:桌号包括:桌号、时间
点菜记录包括:序号、菜名、份额、份数
代点菜记录包括:被点菜的桌号、序号、菜名、份额、份数
删除点菜记录包括:delete和点菜序号
过程:
信息长度为2:(1)菜谱(读入菜谱)
(2)删除点菜记录(删除记录有效,打标记,算总价时不计入;删除记录无效,输出delete error)
信息长度为4:(1)桌号及时间(记录桌号和时间),输出桌号
(2)点菜记录(菜名存在,输出序号+菜名+单价;菜名不存在,输出菜名+does not exist)
信息长度为5:代点菜记录(代点桌号存在,输出相关信息)
输出:
(1)输出桌号
(2)输出菜单(判断菜名是否存在)
(3)代点菜记录
(4)删除点菜记录
(5)重复以上记录,直到最后一个桌号结束
(6)输出桌号对应的信息,如果在营业时间内,输出桌号+总价;如果不在营业时间,输出桌号+out of opening hours
源码分析
类图
源码
代码是别人的,我自己没写出来
点击查看代码
import java.time.LocalDateTime;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Menu menu = new Menu();
Table[] tables = new Table[10];
int dishAmount = 0;//菜单菜的数目
int orderAmount = 0;//点菜记录的数目(包括带点菜的记录)
Dish dish;
int tableAmount = 0;//桌子的数目
int count;//数组长度
String[] temp;//输入字符串去除空格后的数组
int a1,a2,a3,a4,a5;
while (true) {
String string = input.nextLine();
temp = string.split(" ");
if(string.equals("end"))
break;
count = temp.length;
if (count == 2) {
if (temp[1].equals("delete")) {
a1 = Integer.parseInt(temp[0]);
if (tables[tableAmount].order.delARecordByOrderNum(a1) == 0) {
System.out.println("delete error;");
} else {
int c = tables[tableAmount].order.delARecordByOrderNum(a1);//要删除序号的点菜记录的价格
tables[tableAmount].order.sum -= c;//删除记录后那桌的未打折的总价格
}
} else{
a2 = Integer.parseInt(temp[1]);
menu.dishs[dishAmount] = menu.addDish(temp[0], a2);//添加新的菜到菜单里面
dishAmount++;//菜的数目加1
}
}
else if (count == 4) {
if (temp[0].equals("table")){
tableAmount++;//桌子数加一
orderAmount = 0;//每个桌子的点菜记录数目不同,创建一个新的类时需要重置数目
tables[tableAmount] = new Table(temp[1], temp[2], temp[3]);//创建一个新的类
tables[tableAmount].processTime();//处理桌子的日期和时间
tables[tableAmount].setDiscount();//设置每个桌子时间对应的菜的折扣
System.out.println("table " + temp[1] + ": ");
} else {
a3 = Integer.parseInt(temp[0]);//序号
a4 = Integer.parseInt(temp[2]);//份额
a5 = Integer.parseInt(temp[3]);//份数
tables[tableAmount].order.addARecord(a3, temp[1],a4 , a5);//使用函数给点菜记录赋予相关的序号、菜名、份额、份数,将该条记录添加到总记录中
dish = menu.searthDish(temp[1]);//找点菜记录的菜名,存在菜名,返回dish,否则返回null
if(dish==null){
System.out.println(temp[1]+" does not exist");
}
if (dish != null) {
tables[tableAmount].order.records[orderAmount].d = dish;//让记录的菜等于dish,以方便获得菜的价钱
int a = tables[tableAmount].order.records[orderAmount].getPrice();//计算记录的价格
System.out.println(tables[tableAmount].order.records[orderAmount].orderNum + " " + dish.name + " " + a);
tables[tableAmount].order.sum += a;//把该记录的价格加到该桌子的总价格中
}
orderAmount++;//点菜记录加一
}
}
else if (count == 5){
a1 = Integer.parseInt(temp[1]);//序号
a2 = Integer.parseInt(temp[3]);//份额
a3 = Integer.parseInt(temp[4]);//份数
tables[tableAmount].order.addARecord(a1, temp[2], a2, a3);//使用函数给代点菜记录赋予相关的序号、菜名、份额、份数,将该条记录添加到总记录中
dish = menu.searthDish(temp[2]);//找点菜记录的菜名,存在菜名,返回dish,否则返回null
if(dish==null){
System.out.println(temp[2]+" does not exist");
}
if (dish != null){
tables[tableAmount].order.records[orderAmount].d = dish;//让记录的菜等于dish,以方便获得菜的价钱
int b = tables[tableAmount].order.records[orderAmount].getPrice();////计算记录的价格
System.out.println(temp[1] + " table " + tables[tableAmount].tableNum + " pay for table " + temp[0] + " " + b);
tables[tableAmount].order.sum += b;//把该记录的价格加到该桌子的总价格中
}
orderAmount++;//点菜记录加一
}
}
for(int i = 1; i < tableAmount + 1; i++){
if(tables[i].getDiscount() > 0){
System.out.println("table " + tables[i].tableNum + ": "+Math.round(tables[i].order.getTotalPrice()*tables[i].getDiscount()));
}
else System.out.println("table " + tables[i].tableNum + " out of opening hours");
}
}
}
class Dish {
String name;
int unit_price;
int getPrice(int portion){//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
if (portion == 0)
return 0;
else if (portion == 1)
return this.unit_price;
else if (portion == 2)
return (int)(this.unit_price * 1.5 + 0.5);
return this.unit_price * 2;
}
}
class Menu {
Dish[] dishs = new Dish[10];
int dishCount = 0;
Dish searthDish(String dishName){
for(int i = dishCount-1; i >= 0; i--){
if(dishName.equals(dishs[i].name)){
return dishs[i];
}
}
return null;
}
Dish addDish(String dishName, int unit_price){
Dish dish = new Dish();
dish.name = dishName;
dish.unit_price = unit_price;
dishCount ++;
return dish;
}
}
class Record {
int orderNum;
Dish d = new Dish();
int num = 0;
int portion;
int getPrice(){
return d.getPrice(portion) * num;
}
}
class Order {
Record[] records = new Record[10];
int count = 0;
int sum;
void addARecord(int orderNum,String dishName,int portion,int num){
records[count] = new Record();
records[count].d.name = dishName;
records[count].orderNum = orderNum;
records[count].portion = portion;
records[count].num = num;
count++;
}
int getTotalPrice(){
return sum;
}
int delARecordByOrderNum(int orderNum){
if(orderNum>count||orderNum<=0){
return 0;
}else {
return records[orderNum - 1].getPrice();
}
}
Record findRecordByNum(int orderNum) {
for (int i = count - 1; i >= 0; i--) {
if (orderNum == records[i].orderNum) {
return records[i];
}
}
return null;
}
}
class Table {
int tableNum;
String Date;
String tableTime;
int year;
int month;
int day;
int week;
int hh;
int mm;
int ss;
public Table() {
}
public Table(String tableNum, String date, String tableTime) {
this.tableNum = Integer.parseInt(tableNum);
Date = date;
this.tableTime = tableTime;
}
Order order = new Order();
float discount = -1;
void processTime(){
String[] temp1 = Date.split("/");
String[] temp2 = tableTime.split("/");
year = Integer.parseInt(temp1[0]);
month = Integer.parseInt(temp1[1]);
day = Integer.parseInt(temp1[2]);
/*
Calendar date = Calendar.getInstance();
date.set(year, (month - 1), day);
week = date.get(Calendar.DAY_OF_WEEK);
if(week == 1)
week = 7;
else
week--;*/
//判断当前日期是第几周
LocalDateTime Date = LocalDateTime.of(year, month, day, hh, mm, ss);
week = Date.getDayOfWeek().getValue();//获得当前日期是第几周
hh = Integer.parseInt(temp2[0]);
mm = Integer.parseInt(temp2[1]);
ss = Integer.parseInt(temp2[2]);
}
void setDiscount(){
if(week >= 1 && week <= 5)
{
if(hh >= 17 && hh < 20)
discount =0.8F;
else if(hh == 20 && mm < 30)
discount =0.8F;
else if(hh == 20 && mm == 30 && ss == 0)
discount =0.8F;
else if(hh >= 11 && hh <=13 || hh == 10 && mm >= 30)
discount =0.6F;
else if(hh == 14 && mm < 30)
discount =0.6F;
else if(hh == 14 && mm == 30 && ss == 0)
discount =0.6F;
}
else
{
if(hh >= 10 && hh <= 20)
discount = 1.0F;
else if(hh == 9&& mm >= 30)
discount = 1.0F;
else if(hh == 21 && mm < 30 || hh == 21 && mm == 30 && ss == 0)
discount = 1.0F;
}
}
float getDiscount(){
return discount;
}
}
小结
需要先理清题目给的信息以及对信息的处理,要有整体思路,而不是上来就写,后面改起来会很要命。
题目集5
7-5
题目分析
这道题的基本功能实现和之前的大同小异,主要是把原来DateUtil里的属性Day,Month,Year单独拿出来做成了类,通过类之间的聚合关系实现功能,主要在于理清楚这些类之间的关系。
源码分析
类图
源码
点击查看代码
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(day, month, year);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
m = input.nextInt();
if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
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(day, month, year);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
n = input.nextInt();
if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
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(day, month, year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);
if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println(fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class Day {
private int value;
private Month month = new Month();//若写成private Month month;会导致NullPointerException错误
private int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Day() {
}
Day(int yearValue, int monthValue, int dayValue) {
month.getYear().setValue(yearValue);
month.setValue(monthValue);
this.value = dayValue;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month value) {
month.setValue(value.getValue());
month.getYear().setValue(value.getYear().getValue());
}
public void resetMin() {
this.value = 1;
}
public void resetMax() {
int temp = 0;
if (this.getMonth().getYear().isLeapYear() && this.getMonth().getValue() == 2)
temp = mon_maxnum[month.getValue() - 1] + 1;
if (this.getMonth().getValue() == 2) {
if (this.getMonth().getYear().isLeapYear())
this.value = temp;
else
this.value = mon_maxnum[1];
}
else
this.value = mon_maxnum[month.getValue() - 1];
}
public boolean validate() {
if (month.getYear().isLeapYear())
mon_maxnum[1]++;
if (this.value <= mon_maxnum[month.getValue() - 1] && this.value >= 1){
mon_maxnum[1] = 28;
return true;
}
mon_maxnum[1] = 28;
return false;
}
public void dayIncrement() {
if (month.getYear().isLeapYear())
mon_maxnum[1]++;
if (this.value == mon_maxnum[this.getMonth().getValue() - 1]) {
this.month.monthIncrement();
resetMin();
} else
this.value++;
mon_maxnum[1] = 28;
}
public void dayReduction() {
if (this.value == 1) {
this.month.monthReduction();
resetMax();
} else
this.value--;
}
}
class Month{
private int value = 0;
private Year year = new Year();
Month () {
}
Month (int yearValue, int monthValue) {
year.setValue(yearValue);
this.value = monthValue;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public void resetMin() {
this.value = 1;
}
public void resetMax() {
this.value = 12;
}
public boolean validate(){
if (this.value >= 1 && this.value <= 12)
return true;
return false;
}
public void monthIncrement() {
if (this.value == 12) {
this.year.yearIncrement();
resetMin();
}
else
this.value ++;
}
public void monthReduction() {
if (this.value == 1) {
this.year.yearReduction();
resetMax();
}
else
this.value --;
}
}
class Year{
private int value = 0;
Year() {
}
Year (int value) {
this.value = value;
}
public int getValue () {
return this.value;
}
public void setValue (int value) {
this.value = value;
}
public boolean isLeapYear (){
if ((this.value % 4 == 0 && this.value % 100 != 0) || this.value % 400 == 0)
return true;
return false;
}
public boolean validate () {
if (this.value >= 1900 && this.value <= 2050)
return true;
return false;
}
public void yearIncrement () {
this.value ++;
}
public void yearReduction () {
this.value --;
}
}
class DateUtil {
private Day day = new Day();
DateUtil() {
}
DateUtil(int d, int m, int y) {
this.day.setValue(d);
this.day.getMonth().setValue(m);
this.day.getMonth().getYear().setValue(y);
}
public Day getDay() {
return this.day;
}
public void setDay(Day d) {
this.day.setValue(d.getValue());
this.day.getMonth().setValue(d.getMonth().getValue());
this.day.getMonth().getYear().setValue(d.getMonth().getYear().getValue());
}
public boolean checkInputValidity() {
if (this.day.getMonth().getYear().validate() && this.day.getMonth().validate() && this.day.validate())
return true;
return false;
}
/*public boolean compareDates(DateUtil date) {
if(this.day.getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue())
return true;
if(this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.day.getMonth().getValue() > date.getDay().getMonth().getValue())
return true;
if(this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue() && this.day.getMonth().getValue() > date.getDay().getMonth().getValue() && this.day.getValue() > date.getDay().getValue())
return true;
return false;
}*/
public boolean compareDates(DateUtil date) {
if (this.day.getMonth().getYear().getValue() > date.getDay().getMonth().getYear().getValue())
return true;
if (this.day.getMonth().getValue() > date.getDay().getMonth().getValue())
return true;
if (this.day.getValue() > date.getDay().getValue())
return true;
return false;
}
public boolean equalTwoDates(DateUtil date) {
if (this.day.getValue() == date.getDay().getValue() && this.day.getMonth().getValue() == date.getDay().getMonth().getValue() && this.day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue())
return true;
return false;
}
public String showDate() {
return (this.day.getMonth().getYear().getValue() + "-" + this.day.getMonth().getValue() + "-" + this.day.getValue());
}
public DateUtil getNextNDays(int n) {
int i;
for (i = 0; i < n; i++)
this.day.dayIncrement();
return this;
}
public DateUtil getPreviousNDays(int n) {
int i;
for (i = 0; i < n; i++)
this.day.dayReduction();
return this;
}
public int getDaysofDates(DateUtil date) {
int i = 0, sum = 0;
if (this.equalTwoDates(date))
return 0;
if (this.compareDates(date)) {
while (!this.equalTwoDates(date)) {
sum++;
this.day.dayReduction();
}
}
else {
while (!this.equalTwoDates(date)){
sum ++;
this.day.dayIncrement();
}
}
return sum;
}
}
小结
理解类之间的相互调用关系,可以比较迅速地完成各个类中方法的设计。
7-6
题目分析
这题的思路和上一题差不多,差别在于上一题是线性的聚合关系,一个类把另一个类设为自己的属性。这题是星状的聚合关系,把DateUtil看做中心,把其他类作为自己的属性,在DateUtil类里实现对其他类的功能使用。
源码分析
类图
源码
点击查看代码
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()) {
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().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " 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()) {
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().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " 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() && toDate.checkInputValidity()) {
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 Day {
private int value;
Day() {
}
Day(int Value) {
this.value = Value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void dayIncrement() {
this.value++;
}
public void dayReduction() {
this.value--;
}
}
class Month{
private int value = 0;
Month () {
}
Month (int Value) {
this.value = Value;
}
public int getValue() {
return this.value;
}
public void setValue(int value) {
this.value = value;
}
public void resetMin() {
this.value = 1;
}
public void resetMax() {
this.value = 12;
}
public boolean validate(){
if (this.value >= 1 && this.value <= 12)
return true;
return false;
}
public void monthIncrement() {
if (this.value == 12) {
resetMin();
}
else
this.value ++;
}
public void monthReduction() {
if (this.value == 1) {
resetMax();
}
else
this.value --;
}
}
class Year{
private int value = 0;
Year() {
}
Year (int value) {
this.value = value;
}
public int getValue () {
return this.value;
}
public void setValue (int value) {
this.value = value;
}
public boolean isLeapYear (){
if ((this.value % 4 == 0 && this.value % 100 != 0) || this.value % 400 == 0)
return true;
return false;
}
public boolean validate () {
if (this.value >= 1820 && this.value <= 2020)
return true;
return false;
}
public void yearIncrement () {
this.value ++;
}
public void yearReduction () {
this.value --;
}
}
class DateUtil {
private Year year = new Year();
private Month month = new Month();
private Day day = new Day();
private int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
DateUtil() {
}
DateUtil(int y, int m, int d) {
year.setValue(y);
month.setValue(m);
day.setValue(d);
}
public Year getYear(){
return this.year;
}
public void setYear(Year year) {
this.year = year;
}
public Month getMonth() {
return this.month;
}
public void setMonth(Month month) {
this.month = month;
}
public Day getDay () {
return this.day;
}
public void setDay (Day day) {
this.day = day;
}
public void setDayMin (){
day.setValue(1);
}
public void setDayMax (){
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
this.day.setValue(mon_maxnum[month.getValue() - 1]);
}
public boolean checkInputValidity() {
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
if (this.getYear().validate() && this.getMonth().validate() && this.getDay().getValue() <= mon_maxnum[this.month.getValue() - 1] && this.getDay().getValue() >= 1) {
return true;
}
return false;
}
public boolean compareDates(DateUtil date) {
if(this.getYear().getValue() > date.getYear().getValue())
return true;
if(this.getYear().getValue() == date.getYear().getValue() && this.getMonth().getValue() > date.getMonth().getValue())
return true;
if(this.getMonth().getValue() == date.getMonth().getValue() && this.getDay().getValue() > date.getDay().getValue() && this.getDay().getValue() > date.getDay().getValue())
return true;
return false;
}
public boolean equalTwoDates(DateUtil date) {
if (this.year.getValue() == date.year.getValue() && this.month.getValue() == date.month.getValue() && this.day.getValue() == date.day.getValue())
return true;
return false;
}
public DateUtil getNextNDays(int n) {
int i;
for (i = 0; i < n; i++) {
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
if (this.day.getValue() == mon_maxnum[this.month.getValue() - 1]){
if (this.month.getValue() == 12) {
this.month.resetMin();
this.year.yearIncrement();
}
else
this.month.monthIncrement();
setDayMin();
}
else
this.day.dayIncrement();
}
return this;
}
public DateUtil getPreviousNDays(int n) {
int i;
for (i = 0; i < n; i++){
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
if (this.day.getValue() == 1){
if (this.month.getValue() == 1){
this.month.resetMax();
this.year.yearReduction();
}
else
this.month.monthReduction();
setDayMax();
}
else
this.day.dayReduction();
}
return this;
}
public int getDaysofDates(DateUtil date) {
int i = 0, sum = 0;
if (this.equalTwoDates(date))
return 0;
if (this.compareDates(date)) {
while (!this.equalTwoDates(date)) {
sum++;
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
if (this.day.getValue() == 1){
if (this.month.getValue() == 1){
this.month.resetMax();
this.year.yearReduction();
}
else
this.month.monthReduction();
setDayMax();
}
else
this.day.dayReduction();
}
}
else {
while (!this.equalTwoDates(date)) {
sum++;
mon_maxnum[1] = 28;
if (this.year.isLeapYear())
mon_maxnum[1] ++;
if (this.day.getValue() == mon_maxnum[this.month.getValue() - 1]){
if (this.month.getValue() == 12){
this.month.resetMin();
this.year.yearIncrement();
}
else
this.month.monthIncrement();
setDayMin();
}
else
this.day.dayIncrement();
}
}
return sum;
}
public String showDate() {
return (this.year.getValue() + "-" + this.month.getValue() + "-" + this.day.getValue());
}
}
小结
把Month,Day,Year等作为属性后,合理调用类的方法完成方法getNextNDays(),getPreviousNDays(),getDaysofDates()的功能设计。
题目集6
7-1
题目分析
此题在第四次作业的基础上增加了异常处理的功能。
输入和上次作业大同小异,其他的差别如下
过程:
信息长度为2:(1)菜谱(读入菜谱;重复菜名,记录最后一条信息)
(2)删除点菜记录(删除记录有效,标记1,算总价时不计入,标记2,重复删除输出deduplication+序号;删除记录无效,输出delete error)
信息长度为4:(1)桌号及时间(记录桌号和时间),输出桌号
(2)点菜记录(菜名存在,输出序号+菜名+单价;菜名不存在,输出菜名+does not exist)
信息长度为5:代点菜记录(代点桌号存在,输出相关信息)
信息长度为3:
类图
源码
又是别人的代码
点击查看代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Menu menu = new Menu();
Table[] tablemes = new Table[10];
int dishAmount = 0;
int orderAmount = 0;
Dish dish;
int tableAmount = 0;
int count;
String[] temp;
int a1,a2,a3,a4,a5;
int flag1=0;
int mount=0;
while (true) {
String string = input.nextLine();
temp = string.split(" ");
if(string.equals("end"))
break;
count = temp.length;
if (count == 2) {
if (temp[1].equals("delete")) {
if(tablemes[tableAmount].getFlag()==1){
}
else{
a1 = Integer.parseInt(temp[0]);
if (tablemes[tableAmount].order.delARecordByOrderNum(a1) == 0) {
System.out.println("delete error;");
} else {
if (!tablemes[tableAmount].order.records[a1 - 1].IsDelete()) {
int c = tablemes[tableAmount].order.delARecordByOrderNum(a1);
tablemes[tableAmount].order.records[a1 - 1].setDelete(true);
if(!tablemes[tableAmount].order.records[a1 - 1].d.IsSpecialDish()) tablemes[tableAmount].order.commonSum -= c;
else tablemes[tableAmount].order.specialSum -= c;
} else System.out.println("deduplication " + a1);
}
}
} else {
if(flag1==1){
if(tablemes[tableAmount].getFlag()==1){
}
else System.out.println("invalid dish");
}
else{
if(temp[1].matches("[1-9][\\d]*")){
if(Integer.parseInt(temp[1])<300){
a2 = Integer.parseInt(temp[1]);
menu.dishs[dishAmount] = menu.addDish(temp[0], a2);
dishAmount++;
}
else System.out.println(temp[0]+" price out of range "+temp[1]);
}
else if(temp[1].matches("0")) System.out.println(temp[0]+" price out of range "+temp[1]);
else System.out.println("wrong format");
}
}
}
else if(count==3){
if(temp[2].equals("T")){
if(temp[1].matches("[1-9][\\d]*")){
if(Integer.parseInt(temp[1])<300){
a2 = Integer.parseInt(temp[1]);
menu.dishs[dishAmount] = menu.addDish(temp[0], a2);
menu.dishs[dishAmount].setSpecialDish(true);
dishAmount++;
}
else System.out.println(temp[0]+" price out of range "+temp[1]);
}
else if(temp[1].matches("0")) System.out.println(temp[0]+" price out of range "+temp[1]);
else System.out.println("wrong format");
}
else System.out.println("wrong format");
}
else if (count == 4) {
if (temp[0].equals("table")) {
flag1=1;
if(temp[1].matches("[1-9][\\d]*")){
int num = Integer.parseInt(temp[1]);
if (num > 55) {
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println(num +" "+"table num out of range");
} else {
if(temp[2].matches("[1-9][\\d]{3}/[1-9][\\d]?/[1-9][\\d]?")){
String[] str = temp[2].split("/");
int year1=Integer.parseInt(str[0]);
int month1=Integer.parseInt(str[1]);
int day1=Integer.parseInt(str[2]);
int[] mon_maxNum={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month1>=1&&month1<=12){
if(day1>=1&&day1<=mon_maxNum[month1]){
if(year1>=2022&&year1<=2023){
if(temp[3].matches("[1-9][\\d]?/[\\d]{2}/[\\d]{2}")){
tableAmount++;
orderAmount = 0;
mount=0;
tablemes[tableAmount] = new Table(temp[1], temp[2], temp[3]);
tablemes[tableAmount].setFlag(0);
tablemes[tableAmount].processTime();
tablemes[tableAmount].setCommonDiscount();
tablemes[tableAmount].setSpecialDiscount();
System.out.println("table " + temp[1] + ": ");
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println("wrong format");
}
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println("not a valid time period");
}
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println(num +" date error");
}
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println(num +" date error");
}
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println("wrong format");
}
}
}
else{
if(tableAmount==0) tablemes[tableAmount] = new Table();
tablemes[tableAmount].setFlag(1);
System.out.println("wrong format");
}
} else {
if(tablemes[tableAmount].getFlag()==1){
}
else{
a3 = Integer.parseInt(temp[0]);
a4 = Integer.parseInt(temp[2]);
a5 = Integer.parseInt(temp[3]);
tablemes[tableAmount].order.addARecord(a3, temp[1],a4 , a5);
dish = menu.searthDish(temp[1]);
if(dish==null){
System.out.println(temp[1]+" does not exist");
}
if (dish != null) {
if(temp[2].matches("[1-3]")){
if(temp[3].matches("[1-9]")||temp[3].matches("1[0-5]")){
if(mount==0){
tablemes[tableAmount].order.records[orderAmount].setFlag2(1);
mount++;
tablemes[tableAmount].order.records[orderAmount].d = dish;
int a = tablemes[tableAmount].order.records[orderAmount].getPrice();
System.out.println(tablemes[tableAmount].order.records[orderAmount].orderNum + " " + dish.name + " " + a);
if(!dish.IsSpecialDish()) tablemes[tableAmount].order.commonSum += a;
else tablemes[tableAmount].order.specialSum += a;
}
else{
for(int i=orderAmount-1;i>=0;i--){
if(tablemes[tableAmount].order.records[i].flag2 ==1){
if(a3<=tablemes[tableAmount].order.records[i].orderNum){
System.out.println("record serial number sequence error");
}
else{
tablemes[tableAmount].order.records[orderAmount].setFlag2(1);
mount++;
tablemes[tableAmount].order.records[orderAmount].d = dish;
int a = tablemes[tableAmount].order.records[orderAmount].getPrice();
System.out.println(tablemes[tableAmount].order.records[orderAmount].orderNum + " " + dish.name + " " + a);
if(!dish.IsSpecialDish()) tablemes[tableAmount].order.commonSum += a;
else tablemes[tableAmount].order.specialSum += a;
}
break;
}
}
}
}
else if(temp[2].matches("[1-9][\\d]*")) System.out.println(a3+" num out of range "+a5);
else System.out.println("wrong format");
}
else if(temp[2].matches("[4-9]")) System.out.println(a3+" portion out of range "+a4);
else if(temp[2].matches("[1-9][\\d]*")) System.out.println("not a valid portion range");
else System.out.println("wrong format");
}
orderAmount++;
}
}
}
else if (count == 5) {
if(temp[0].matches("[1-9][\\d]*")){
int num = Integer.parseInt(temp[0]);
if (num > 55) System.out.println("Table number :"+temp[0]+" does not exist");
else {
if(tablemes[tableAmount].getFlag()==1){
}
else{
a1 = Integer.parseInt(temp[1]);
a2 = Integer.parseInt(temp[3]);
a3 = Integer.parseInt(temp[4]);
tablemes[tableAmount].order.addARecord(a1, temp[2], a2, a3);
dish = menu.searthDish(temp[2]);
if (dish != null) {
tablemes[tableAmount].order.records[orderAmount].d.unit_price = dish.unit_price;
int b = tablemes[tableAmount].order.records[orderAmount].getPrice();
System.out.println(temp[1] + " table " + tablemes[tableAmount].tableNum + " pay for table " + temp[0] + " " + b);
if(!dish.IsSpecialDish()) tablemes[tableAmount].order.commonSum += b;
else tablemes[tableAmount].order.specialSum += b;
}
orderAmount++;
}
}
}
else System.out.println("wrong format");
}
else System.out.println("wrong format");
}
for (int i = 1; i < tableAmount + 1; i++) {
if(tablemes[i].getCommonDiscount()>0&&tablemes[i].getSpecialDiscount()>0){
System.out.println("table " + tablemes[i].tableNum + ": "+tablemes[i].order.getTotalPrice()+" "+Math.round(tablemes[i].order.commonSum*tablemes[i].getCommonDiscount()+tablemes[i].order.specialSum*tablemes[i].getSpecialDiscount()));
}
else System.out.println("table " + tablemes[i].tableNum + " out of opening hours");
}
}
}
class Dish {
String name;
int unit_price;
boolean isSpecialDish=false;
public boolean IsSpecialDish() {
return isSpecialDish;
}
public void setSpecialDish(boolean specialDish) {
isSpecialDish = specialDish;
}
int getPrice(int portion) {
if (portion == 1) return unit_price;
else if (portion == 2) return Math.round((float) (unit_price * 1.5));
else return 2*unit_price;
}
}
class Menu {
Dish[] dishs = new Dish[10];
int dishCount = 0;
Dish searthDish(String dishName){
for(int i=dishCount-1;i>=0;i--){
if(dishName.equals(dishs[i].name)){
return dishs[i];
}
}
return null;
}
Dish addDish(String dishName,int unit_price){
Dish dish = new Dish();
dish.name = dishName;
dish.unit_price = unit_price;
dishCount++;
return dish;
}
}
class Record {
int orderNum;
Dish d = new Dish();
int num = 0;
int portion;
int flag2=0;
public void setFlag2(int flag2) {
this.flag2 = flag2;
}
boolean isDelete=false;
int getPrice(){
return d.getPrice(portion)*num;
}
public void setDelete(boolean delete) {
isDelete = delete;
}
public boolean IsDelete() {
return isDelete;
}
}
class Order {
Record[] records = new Record[10];
int count = 0;
int commonSum;
int specialSum;
void addARecord(int orderNum,String dishName,int portion,int num){
records[count] = new Record();
records[count].d.name = dishName;
records[count].orderNum = orderNum;
records[count].portion = portion;
records[count].num = num;
count++;
}
int getTotalPrice(){
return commonSum+specialSum;
}
int delARecordByOrderNum(int orderNum){
if(orderNum>count||orderNum<=0){
return 0;
}else {
return records[orderNum - 1].getPrice();
}
}
Record findRecordByNum(int orderNum) {
for (int i = count - 1; i >= 0; i--) {
if (orderNum == records[i].orderNum) {
return records[i];
}
}
return null;
}
}
class Table {
int tableNum;
String Date;
String tableTime;
int flag=0;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public Table() {
}
public Table(String tableNum, String date, String tableTime) {
this.tableNum = Integer.parseInt(tableNum);
Date = date;
this.tableTime = tableTime;
}
int year,month,day,week,hh,mm,ss;
int sum=0;
Order order = new Order();
float commonDiscount = -1;
float specialDiscount=-1;
void processTime(){
String[] temp1 = Date.split("/");
String[] temp2 = tableTime.split("/");
year = Integer.parseInt(temp1[0]);
month = Integer.parseInt(temp1[1]);
day = Integer.parseInt(temp1[2]);
Calendar date = Calendar.getInstance();
date.set(year, (month-1), day);
week = date.get(Calendar.DAY_OF_WEEK);
if(week==1)
week = 7;
else
week--;
hh = Integer.parseInt(temp2[0]);
mm = Integer.parseInt(temp2[1]);
ss = Integer.parseInt(temp2[2]);
}
void setCommonDiscount(){
if(week>=1&&week<=5)
{
if(hh>=17&&hh<20)
commonDiscount =0.8F;
else if(hh==20&&mm<30)
commonDiscount =0.8F;
else if(hh==20&&mm==30&&ss==0)
commonDiscount =0.8F;
else if(hh>=11&&hh<=13||hh==10&&mm>=30)
commonDiscount =0.6F;
else if(hh==14&&mm<30)
commonDiscount =0.6F;
else if(hh==14&&mm==30&&ss==0)
commonDiscount =0.6F;
}
else
{
if(hh>=10&&hh<=20)
commonDiscount = 1.0F;
else if(hh==9&&mm>=30)
commonDiscount = 1.0F;
else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
commonDiscount = 1.0F;
}
}
void setSpecialDiscount(){
if(week>=1&&week<=5)
{
if(hh>=17&&hh<20)
specialDiscount =0.7F;
else if(hh==20&&mm<30)
specialDiscount =0.7F;
else if(hh==20&&mm==30&&ss==0)
specialDiscount =0.7F;
else if(hh>=11&&hh<=13||hh==10&&mm>=30)
specialDiscount =0.7F;
else if(hh==14&&mm<30)
specialDiscount =0.7F;
else if(hh==14&&mm==30&&ss==0)
specialDiscount =0.7F;
}
else
{
if(hh>=10&&hh<=20)
specialDiscount = 1.0F;
else if(hh==9&&mm>=30)
specialDiscount = 1.0F;
else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
specialDiscount = 1.0F;
}
}
float getCommonDiscount() {
return commonDiscount;
}
float getSpecialDiscount() {
return specialDiscount;
}
}
小结
我一开始只写了一桌的情况,没有加循环进去,导致后面修改时一直有错误出现,而且我判断循环跳出的条件存在问题,主要还是没理清楚这些异常处理的先后关系和输出的先后关系,后面就没办法进行下去了。
三.踩坑心得
第四次作业
7-3(去掉重复的数据)
错误点
题目要求:读入数据,检查是否有重复的数据。如果有,去掉所有重复的数字。最后按照输入顺序输出没有重复数字的数据。
Hashset和Arrays.sort()都会对数据进行排序,所以输出时不会按照原数据的顺序输出
ArrayList遍历时间复杂度是O(n),list.contains()很慢
错误代码
点击查看代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int i;
int[] a = new int[n];
for (i = 0; i < n; i++)
a[i] = input.nextInt();
Arrays.sort(a);
for (i = 0; i < n - 1; i++)
if(a[i] != a[i + 1])
System.out.print(a[i] + " ");
System.out.print(a[n - 1]);
}
}
正确代码
点击查看代码
import java.util.*;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int i;
int[] a = new int[n];
HashSet<Integer> sites1 = new HashSet<>();
HashSet<Integer> sites2 = new HashSet<>();
for (i = 0; i < n; i++){
a[i] = input.nextInt();
sites2.add(a[i]);
}
int size = sites2.size();
for (i = 0; i < n - 1; i++) {
if(!sites1.contains(a[i]) ) {
System.out.print(a[i]);
sites1.add(a[i]);
if((size == 1 && i == 0) || size == sites1.size())
continue;
System.out.print(" ");
}
}
if(!sites1.contains(a[n - 1]))
System.out.print(a[n - 1]);
}
}
7-4(单词统计与排序)
错误点
重复输出,且忽略长文本中的文本换行情况
错误代码
点击查看代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str1 = new String();
str1 = input.nextLine().replaceAll(",", "");
str1 = str1.replaceAll("\\."," ");
String[] str = str1.split(" ");
Arrays.sort(str,String.CASE_INSENSITIVE_ORDER);
int i, j;
int[] a = new int[str.length];
int max = 0;
for(i = 0; i< str.length; i++){
a[i] = str[i].length();
if(max < a[i])
max = a[i];
}
for(i = max; i >= 0; i--){
for (j = 0; j < str.length; j ++)
if(i == a[j])
System.out.println(str[j]);
}
}
}
正确代码
点击查看代码
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str1 = new String();
str1 = input.nextLine().replaceAll(",", "");
str1 = str1.replaceAll("\\.", " ");
str1 = str1.replaceAll("\n","");
String[] str = str1.split(" ");
HashSet<String> sites = new HashSet<String>();
Arrays.sort(str, String.CASE_INSENSITIVE_ORDER);
int i, j;
int[] a = new int[str.length];
int max = 0;
//String str_ = "\n";
//System.out.println(str_);
for (i = 0; i < str.length; i++) {
a[i] = str[i].length();
if (max < a[i])
max = a[i];
}
for (i = max; i > 0; i--) {//把i >= 0改为>
for (j = 0; j < a.length; j++) {
if (i == a[j] && !sites.contains(str[j])) {
System.out.println(str[j]);
sites.add(str[j]);
}
}
}
}
}
7-6(GPS测绘中度分秒转换)
错误点
输出时用了System.out.printf("%d°%d′%.2f″ = %.6f",this.degrees, this.minutes, this.seconds,this.convension());
限制了this.seconds保留两位小数
错误代码
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LatitudeConversion l = new LatitudeConversion(input.nextInt(), input.nextInt(), input.nextFloat());
l.convension();
l.print();
}
}
class LatitudeConversion{
private int degrees;
private int minutes;
private float seconds;
LatitudeConversion(){
}
LatitudeConversion(int degrees, int minutes, float seconds){
this.degrees = degrees;
this.minutes = minutes;
this.seconds = seconds;
}
public void setDegrees(int degrees){
this.degrees = degrees;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public void setSeconds(float seconds) {
this.seconds = seconds;
}
public int getDegrees(){
return this.degrees;
}
public int getMinutes() {
return this.minutes;
}
public float getSeconds() {
return this.seconds;
}
public double convension(){
return this.degrees + this.minutes / 60.0 + this.seconds / 3600.0;//this.minutes / 60.0
}
public void print(){
System.out.printf("%d°%d′%.2f″ = %.6f",this.degrees, this.minutes, this.seconds,this.convension());
}
}
正确代码
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LatitudeConversion l = new LatitudeConversion(input.nextInt(), input.nextInt(), input.nextFloat());
l.convension();
l.print();
}
}
class LatitudeConversion{
private int degrees;
private int minutes;
private float seconds;
LatitudeConversion(){
}
LatitudeConversion(int degrees, int minutes, float seconds){
this.degrees = degrees;
this.minutes = minutes;
this.seconds = seconds;
}
public void setDegrees(int degrees){
this.degrees = degrees;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public void setSeconds(float seconds) {
this.seconds = seconds;
}
public int getDegrees(){
return this.degrees;
}
public int getMinutes() {
return this.minutes;
}
public float getSeconds() {
return this.seconds;
}
public double convension(){
return this.degrees + this.minutes / 60.0 + this.seconds / 3600.0;//this.minutes / 60.0
}
public void print(){
System.out.print(this.degrees + "°" + this.minutes + "′" + this.seconds + "″ = ");
System.out.printf("%.6f",this.convension());
}
}
7-7(判断两个日期的先后,计算间隔天数、周数)
错误点
date1.until(date2)只能同一个月相差的天数
错误代码
点击查看代码
import java.time.LocalDate;
import java.util.*;
import java.time.Period;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] str11 = input.nextLine().split("-");
String[] str22 = input.nextLine().split("-");
LocalDate date1 = LocalDate.of(Integer.parseInt(str11[0], 10), Integer.parseInt(str11[1], 10), Integer.parseInt(str11[2], 10));
LocalDate date2 = LocalDate.of(Integer.parseInt(str22[0], 10), Integer.parseInt(str22[1], 10), Integer.parseInt(str22[2], 10));
String str1 = "早";
String str2 = "晚";
String str = new String();
if(date1.isBefore(date2))
str = str1;
else {
str = str2;
LocalDate t = date1;
date1 = date2;
date2 = t;
}
Period period = date1.until(date2);
int week = period.getDays() / 7;
System.out.println("第一个日期比第二个日期更" + str);
System.out.println("两个日期间隔" + period.getDays() +"天");
System.out.println("两个日期间隔" + week +"周");
}
}
正确代码
点击查看代码
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.time.Period;
import static java.time.temporal.ChronoUnit.DAYS;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] str11 = input.nextLine().split("-");
String[] str22 = input.nextLine().split("-");
LocalDate date1 = LocalDate.of(Integer.parseInt(str11[0], 10), Integer.parseInt(str11[1], 10), Integer.parseInt(str11[2], 10));
LocalDate date2 = LocalDate.of(Integer.parseInt(str22[0], 10), Integer.parseInt(str22[1], 10), Integer.parseInt(str22[2], 10));
String str1 = "早";
String str2 = "晚";
String str = new String();
if(date1.isBefore(date2))
str = str1;
else {
str = str2;
LocalDate t = date1;
date1 = date2;
date2 = t;
}
//Period period = date1.until(date2);
System.out.println("第一个日期比第二个日期更" + str);
System.out.println("两个日期间隔" + (date1.until(date2, ChronoUnit.DAYS) +"天"));
System.out.println("两个日期间隔" + (date1.until(date2, ChronoUnit.DAYS) / 7 +"周"));
}
}
其他
在第五次作业7-5中,把Day当做DateUtil的一个属性时,没有new,导致出现空指针错误
四.改进建议
优化代码,降低时间复杂度和空间复杂度
第五次作业
7-5(日期问题面向对象设计(聚合一))
在上次的blog中,我提到在实现功能时,有些方法没有用到,在这次作业中,因为对类之间的关系有了更好的理解,实现单一职责原则,不会出现可能在一个方法中的不正确设计导致其他方法用不上的情况出现。
总结
学到的知识
正确使用正则表达式
如何设计类以及处理类之间的关系
需要改进的地方
坚持吧,在第一次做菜单的那道题时,因为时间比较紧且看到大多数人都没解出来,所以就放弃了没做,导致第二次写的时候更是理不清关系,人总是会为了一时的轻松使得后面自己出现在更遭的状况中。
还有就是菜单的那道题让我深刻认识到先把题目读懂,设计好程序结构的重要性。
建议
老师好像总是忘记发布作业。