zoukankan      html  css  js  c++  java
  • 第4次作业类测试代码+005+陈晓华

    1、类图:

    2、界面和相应的功能

    1)不合法输入(为空,不在范围内):

    2)合法输入:

    3、代码

      1 package swingEdu;
      2 import swingEdu.NextDate;
      3 import java.awt.EventQueue;
      4 
      5 import javax.swing.JFrame;
      6 import javax.swing.JButton;
      7 import javax.swing.JOptionPane;
      8 
      9 import javax.swing.JTextField;
     10 import javax.swing.JLabel;
     11 
     12 import java.awt.event.ActionListener;
     13 import java.awt.event.ActionEvent;
     14 
     15 public class calculator {
     16 
     17     private JFrame frame;
     18     private JTextField input_year;
     19     private JTextField today_week;
     20     private JTextField nextday_week;
     21     private JTextField lastday_week;
     22     private JTextField input_month;
     23     private JTextField input_day;
     24     private int inputYear;
     25     private int inputMonth;
     26     private int inputDay;
     27     
     28 
     29     /**
     30      * Launch the application.
     31      */
     32     public static void main(String[] args) {
     33         EventQueue.invokeLater(new Runnable() {
     34             public void run() {
     35                 try {
     36                     calculator window = new calculator();
     37                     window.frame.setVisible(true);
     38                 } catch (Exception e) {
     39                     e.printStackTrace();
     40                 }
     41             }
     42         });
     43     }
     44 
     45     /**
     46      * Create the application.
     47      */
     48     public calculator() {
     49         initialize();
     50     }
     51 
     52     /**
     53      * Initialize the contents of the frame.
     54      */
     55     private void initialize() {
     56         frame = new JFrame();
     57         frame.setTitle("日期计算程序");
     58         frame.setBounds(100, 100, 450, 300);
     59         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     60         frame.getContentPane().setLayout(null);
     61 
     62         JLabel lblNewLabel = new JLabel("请输入需要计算的年月日:");
     63         lblNewLabel.setBounds(8, 6, 159, 28);
     64         frame.getContentPane().add(lblNewLabel);
     65 
     66         input_year = new JTextField();
     67         input_year.setBounds(54, 41, 81, 28);
     68         frame.getContentPane().add(input_year);
     69         input_year.setColumns(10);
     70 
     71         input_month = new JTextField();
     72         input_month.setBounds(187, 40, 83, 29);
     73         frame.getContentPane().add(input_month);
     74         input_month.setColumns(10);
     75 
     76         input_day = new JTextField();
     77         input_day.setBounds(320, 43, 92, 22);
     78         frame.getContentPane().add(input_day);
     79         input_day.setColumns(10);
     80 
     81         JLabel label = new JLabel("年:");
     82         label.setBounds(29, 46, 35, 19);
     83         frame.getContentPane().add(label);
     84 
     85         JLabel label_1 = new JLabel("月:");
     86         label_1.setBounds(158, 46, 65, 19);
     87         frame.getContentPane().add(label_1);
     88 
     89         JLabel label_2 = new JLabel("日:");
     90         label_2.setBounds(295, 47, 61, 16);
     91         frame.getContentPane().add(label_2);
     92 
     93         JButton calculate = new JButton("OK");
     94         calculate.addActionListener(new ActionListener() {
     95             public void actionPerformed(ActionEvent e) {
     96                 NextDate nextdate = new NextDate();
     97                 nextday_week.setText("");
     98                 today_week.setText("");
     99                 lastday_week.setText("");
    100                 if (input_year.getText().length() == 0) {
    101                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "年份错误提示",
    102                             JOptionPane.ERROR_MESSAGE);
    103                     return;
    104                 }else if (input_month.getText().length() == 0) {
    105                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "月份错误提示",
    106                             JOptionPane.ERROR_MESSAGE);
    107                     return;
    108                 }else if (input_day.getText().length() == 0) {
    109                     JOptionPane.showMessageDialog(null, "输入有误,请重新输入",
    110                             "日期错误提示", JOptionPane.ERROR_MESSAGE);
    111                     return;
    112                 }else {
    113                     inputYear=Integer.parseInt(input_year.getText());
    114                     inputMonth=Integer.parseInt(input_month.getText());
    115                     inputDay=Integer.parseInt(input_day.getText());
    116                     String nextDayResult=nextdate.nextdate(inputMonth, inputDay, inputYear);
    117                     if (nextDayResult.contains("超出范围")) {
    118                         JOptionPane.showMessageDialog(null, nextDayResult,
    119                                 "计算错误提示", JOptionPane.ERROR_MESSAGE);
    120                         input_year.setText("");
    121                         input_month.setText("");
    122                         input_day.setText("");
    123                         return;
    124                     } else {
    125                         nextday_week.setText(nextDayResult);
    126                     }
    127                     String lastDayResult=nextdate.lastDay(inputMonth, inputDay, inputYear);
    128                     if (lastDayResult.contains("超出范围")) {
    129                         JOptionPane.showMessageDialog(null, nextDayResult,
    130                                 "计算错误提示", JOptionPane.ERROR_MESSAGE);
    131                         input_year.setText("");
    132                         input_month.setText("");
    133                         input_day.setText("");
    134                         return;
    135                     } else {
    136                         lastday_week.setText(lastDayResult);
    137                     }
    138                     int weekDayResult=nextdate.weekDay(inputMonth, inputDay, inputYear);
    139                         today_week.setText(weekDayResult+"");
    140                 }
    141 
    142             }
    143         });
    144         calculate.setBounds(102, 81, 65, 29);
    145         frame.getContentPane().add(calculate);
    146 
    147         JButton reset = new JButton("Cancel");
    148         reset.addActionListener(new ActionListener() {
    149             public void actionPerformed(ActionEvent e) {
    150                 input_year.setText("");
    151                 input_month.setText("");
    152                 input_day.setText("");
    153 
    154             }
    155         });
    156         reset.setBounds(232, 81, 72, 29);
    157         frame.getContentPane().add(reset);
    158 
    159         JLabel lblNewLabel_1 = new JLabel("这天是星期:");
    160         lblNewLabel_1.setBounds(102, 128, 92, 16);
    161         frame.getContentPane().add(lblNewLabel_1);
    162 
    163         today_week = new JTextField();
    164         today_week.setBounds(179, 122, 134, 28);
    165         frame.getContentPane().add(today_week);
    166         today_week.setColumns(10);
    167 
    168         JLabel lblNewLabel_2 = new JLabel("下一天是:");
    169         lblNewLabel_2.setBounds(102, 174, 72, 16);
    170         frame.getContentPane().add(lblNewLabel_2);
    171 
    172         JLabel label_3 = new JLabel("上一天是:");
    173         label_3.setBounds(102, 220, 61, 16);
    174         frame.getContentPane().add(label_3);
    175 
    176         nextday_week = new JTextField();
    177         nextday_week.setBounds(179, 168, 134, 28);
    178         frame.getContentPane().add(nextday_week);
    179         nextday_week.setColumns(10);
    180 
    181         lastday_week = new JTextField();
    182         lastday_week.setBounds(179, 214, 134, 28);
    183         frame.getContentPane().add(lastday_week);
    184         lastday_week.setColumns(10);
    185 
    186     }
    187     
    188     
    189 }
    calculator
      1 package swingEdu;
      2 
      3 public class NextDate {
      4 
      5     //nextdate函数 计算下一天
      6             public String nextdate(int month,int day,int year){
      7                 int isleap[]={31,29,31,30,31,30,31,31,30,31,30,31};
      8                 int noleap[]={31,28,31,30,31,30,31,31,30,31,30,31};
      9                 int m,d,y;
     10                 
     11                 if (month<1 || month>12){
     12                     return "月份超出范围";
     13                 }
     14                 if (day<1 || day>31){
     15                     return "日期超出范围";
     16                 }
     17                 if (year<1912 || year>2050){
     18                     return "年份超出范围";
     19                 }
     20 
     21                 if ((year%4==0 && year%100!=0) || year%400==0){//闰年
     22                     if (day<isleap[month-1]){
     23                         d = day+1;
     24                         m = month;
     25                         y = year;
     26                     } 
     27                     else if (day==isleap[month-1]){//该月的最后一天
     28                         d=1;
     29                         if (month==12){//一年的最后一天
     30                             m = 1;
     31                             y = year+1;
     32                         }
     33                         else{
     34                             m = month+1;
     35                             y = year;
     36                         }
     37                     }
     38                     else{//针对31天以内,但是超出该月最大天数
     39                         return "日期超出范围";
     40                     }    
     41                 }
     42                 
     43                 else{//非闰年
     44                     if (day<noleap[month-1]){
     45                         d = day+1;
     46                         m = month;
     47                         y = year;
     48                     } 
     49                     else if (day==noleap[month-1]){//该月的最后一天
     50                         d = 1;
     51                         if (month==12){//一年的最后一天    
     52                             m = 1;
     53                             y = year+1;
     54                         }
     55                         else{
     56                             m = month+1;
     57                             y = year;
     58                         }
     59                     }
     60                     else{//针对31天以内,但是超出该月最大天数
     61                         return "日期超出范围";
     62                     }
     63                 }
     64                 return y + "年" + m + "月" + d + "日";
     65             }
     66             
     67             //lastDay函数 计算上一天
     68             public String lastDay(int month,int day,int year)
     69             {
     70                 int isleap[]={31,29,31,30,31,30,31,31,30,31,30,31};
     71                 int noleap[]={31,28,31,30,31,30,31,31,30,31,30,31};
     72                 int m,d,y;
     73                 
     74                 if (month<1 || month>12){
     75                     return "月份超出范围";
     76                 }
     77                 if (day<1 || day>31){
     78                     return "日期超出范围";
     79                 }
     80                 if (year<1912 || year>2050){
     81                     return "年份超出范围";
     82                 }
     83 
     84                 if(month==1 && day==1){
     85                     y = year -1;
     86                     m = 12;
     87                     d = 31;
     88                 }else if(day==1){
     89                     y = year;
     90                     m = month - 1;
     91                     if((year%4==0 && year%100!=0) || year%400==0){//闰年
     92                         d = isleap[m-1];
     93                     }
     94                     else{//非闰年
     95                         d = noleap[m-1];
     96                     }
     97                 }else{
     98                     y = year;
     99                     m = month;
    100                     d = day - 1;
    101                 }
    102                 
    103                 return y + "年" + m + "月" + d + "日";
    104             }
    105             
    106             //weekDay函数 计算星期几
    107             public int weekDay(int month,int day,int year){
    108                 int iweek = 0;
    109                 int y = 0, c = 0, m = 0, d = 0;
    110                 
    111                 if(month == 1 || month == 2)
    112                 {
    113                     c = (year - 1) / 100;
    114                     y = (year - 1) % 100;
    115                     m = month + 12;
    116                     d = day;
    117                 }
    118                 else
    119                 {
    120                     c = year / 100;
    121                     y = year % 100;
    122                     m = month;
    123                     d = day;
    124                 }
    125                 
    126                 iweek = y + y/4 + c/4 - 2*c + 26*(m+1)/10 + d - 1; //泰勒公式
    127                 iweek = iweek >= 0 ? (iweek % 7) : (iweek % 7 + 7);
    128                 if(iweek == 0)
    129                 {
    130                     iweek = 7;
    131                 }
    132                 
    133                 return iweek;
    134             }
    135 }
    NextDate
  • 相关阅读:
    diy_markdown 的 html 显示
    根据 vuex 的 this.$store.dispatch() 返回值 处理逻辑
    vue 项目配置: 局域网 ip 发布
    vue-markdown 之 markdown-it, 以及 table of content 的实现:markdown-it-toc-and-anchor
    程序员面试金典-面试题 08.05. 递归乘法
    程序员面试金典-面试题 08.04. 幂集
    程序员面试金典-面试题 08.03. 魔术索引
    程序员面试金典-面试题 08.02. 迷路的机器人
    程序员面试金典-面试题 08.01. 三步问题
    程序员面试金典-面试题 05.08. 绘制直线
  • 原文地址:https://www.cnblogs.com/ChenXh123/p/6800184.html
Copyright © 2011-2022 走看看