前言
采用java对三天打鱼两天晒网题目进行编写,基于面向对象程序设计的原理进行构设,用例为300个连续日期,并将输出结构保存至文本文件,再利用python进行了数据的可视化分析,检验了算法的正确性
提示:以下是本篇文章正文内容
1.创建时间类管理时间对象
package beordie;
/**
* @className: Time
* @description: 定义时间对象
* @author: 30500
* @date: 2021/3/22
**/
public class Time {
public int year;
public int month;
public int day;
public static int hour = 0;
public static int minute = 0;
public static int second = 0;
public Time() {
this.year = 2010;
this.month = 01;
this.day = 01;
}
public Time(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public void setTime(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
2.创建日期字典
package beordie;
import java.util.HashMap;
import java.util.Map;
/**
* @className: Assist
* @description: 定义月份天数字典
* @author: 30500
* @date: 2021/3/22
**/
public class Assist {
public Map<String, String> leapYear = new HashMap<>();
public Map<String, String> averageYear = new HashMap<>();
public Assist() {
leapYear.put("1", "0");
leapYear.put("2", "31");
leapYear.put("3", "60");
leapYear.put("4", "91");
leapYear.put("5", "121");
leapYear.put("6", "152");
leapYear.put("7", "182");
leapYear.put("8", "213");
leapYear.put("9", "244");
leapYear.put("10", "274");
leapYear.put("11", "305");
leapYear.put("12", "335");
averageYear.put("1", "0");
averageYear.put("2", "31");
averageYear.put("3", "59");
averageYear.put("4", "90");
averageYear.put("5", "120");
averageYear.put("6", "151");
averageYear.put("7", "181");
averageYear.put("8", "212");
averageYear.put("9", "243");
averageYear.put("10", "273");
averageYear.put("11", "304");
averageYear.put("12", "334");
}
}
3.创建Date类计算时间间隔
按照从年到日的顺序依次进行天数的统计,最后将结果返回到上一层进行处理。
package beordie;
/**
* @className: Date
* @description: 两个时间之间的天数计算
* @author: 30500
* @date: 2021/3/22
**/
public class Date {
public int calculateDate(Time date1, Time date2){
int countYear = calculateYears(date1, date2);
int countMonth = calculateMonths(date1, date2);
int countDay = calculateDays(date1, date2);
return countDay + countMonth + countYear;
}
public int calculateYears(Time date1, Time date2){
int countDay = 0;
for(int tempYear = date1.year ; tempYear < date2.year ; tempYear++){
if((tempYear % 4 ==0 && tempYear % 100 != 0) || (tempYear % 400 == 0)){
countDay += 366;
}
else
countDay += 365;
}
return countDay;
}
public int calculateMonths(Time date1, Time date2){
int countDay1 = 0;
int countDay2 = 0;
Assist assist = new Assist();
if(isLeapYear(date1)){
String month = String.valueOf(date1.month);
String tempDay = assist.leapYear.get(month);
countDay1 = Integer.parseInt(tempDay);
}
else{
String month = String.valueOf(date1.month);
String tempDay = assist.averageYear.get(month);
countDay1 = Integer.parseInt(tempDay);
}
if(isLeapYear(date2)){
String month = String.valueOf(date2.month);
String tempDay = assist.leapYear.get(month);
countDay2 = Integer.parseInt(tempDay);
}
else{
String month = String.valueOf(date2.month);
String tempDay = assist.averageYear.get(month);
countDay2 = Integer.parseInt(tempDay);
}
return countDay2 - countDay1;
}
public int calculateDays(Time date1, Time date2){
return date2.day - date1.day;
}
public boolean isLeapYear(Time date){
if((date.year % 4 ==0 && date.year % 100 != 0) || (date.year % 400 == 0)){
return true;
}
else
return false;
}
}
4.创建入口类实现数据的读入处理
import beordie.Date;
import beordie.Assist;
import beordie.Time;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @className: Calculate
* @description: 用于接收数据的输入和进行数据预处理
* @author: 30500
* @date: 2021/3/22
**/
class Calculate{
// 两个时间之间的相差天数
public int days;
Date demo = new Date();
public void calculateDay(){
try {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份");
int year = scanner.nextInt();
System.out.println("请输入月份");
int month = scanner.nextInt();
System.out.println("请输入日");
int day = scanner.nextInt();
Time date2 = new Time(year, month, day);
if(!injudge(date2)){
days = -2;
return;
}
Time date1 = new Time();
days = demo.calculateDate(date1, date2);
}catch(InputMismatchException e){
System.out.println("请输入整型的数字");
days = -2;
return;
}
}
public void calculateDay(int year, int month, int day){
Time date2 = new Time(year, month, day);
Time date1 = new Time();
days = demo.calculateDate(date1, date2);
}
public void display(){
switch ((days + 1) % 5){
case 1:
System.out.println("这天打鱼");
break;
case 2:
System.out.println("这天打鱼");
break;
case 3:
System.out.println("这天打鱼");
break;
case 4:
System.out.println("这天晒网");
break;
case 0:
System.out.println("这天晒网");
break;
default:
break;
}
}
public String display(int i){
switch ((days + 1) % 5){
case 1:
System.out.println("这天打鱼");
return "这天打鱼";
case 2:
System.out.println("这天打鱼");
return "这天打鱼";
case 3:
System.out.println("这天打鱼");
return "这天打鱼";
case 4:
System.out.println("这天晒网");
return "这天晒网";
case 0:
System.out.println("这天晒网");
return "这天晒网";
default:
break;
}
return "----";
}
public boolean injudge(Time time){
if(time.year<2010){
System.out.println("年份输入错误");
return false;
}
else if(time.month>12){
System.out.println("月份输入错误");
return false;
}
else if(demo.isLeapYear(time)){
if(time.day>29 && time.month==2){
System.out.println("日输入错误");
return false;
}
}
else if(!demo.isLeapYear(time)){
if(time.day>28 && time.month==2){
System.out.println("日输入错误");
return false;
}
}
else{
if(time.day>31 && (time.month==1 || time.month==3 ||time.month==5 ||time.month==7 ||time.month==8 ||time.month==10 ||time.month==12)){
System.out.println("日输入错误");
return false;
}
else if(time.day>30 && (time.month==4 || time.month==6 ||time.month==9 ||time.month==11)){
System.out.println("日输入错误");
return false;
}
}
return true;
}
}
public class FishingNet {
/*
* 主函数入口
*/
public static void main(String[] args) {
Calculate model = new Calculate();
model.calculateDay();
model.display();
}
}
5.创建测试类测试模块功能
mport java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @className: Test
* @description: 方法测试
* @author: 30500
* @date: 2021/3/22
**/
public class Test {
public static void main(String[] args) {
List<String> year = new ArrayList<String>();
List<String> month = new ArrayList<String>();
List<String> day = new ArrayList<String>();
List<String> resultList = new ArrayList<>();
List<String> result = new ArrayList<>();
try{
BufferedReader br = new BufferedReader(new FileReader("src/dataset"));
String s = null;
while((s = br.readLine())!=null){
resultList.add(s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
for(int i =0;i<resultList.size();i++){
year.add(resultList.get(i).split("-")[0]);
month.add(resultList.get(i).split("-")[1]);
day.add(resultList.get(i).split("-")[2]);
}
Calculate model = new Calculate();
for(int i =0;i<resultList.size();i++){
model.calculateDay(Integer.parseInt(year.get(i)),Integer.parseInt(month.get(i)),Integer.parseInt(day.get(i)));
String str = model.display(1);
result.add(str);
}
try{
BufferedWriter br = new BufferedWriter(new FileWriter("src/result.txt"));
for(int i = 0;i< result.size();i++){
br.write(resultList.get(i) + " " + result.get(i)+"\n");
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
总结
以上就是关于三天打鱼,两天晒网题目的java编写,并用测试类对各个模块的功能进行了测试,结果与预期一致。