第4次作业类测试代码+020+梁睿阳
一、类图

二、代码与界面
因为有很大一部分代码是旧的并没有做更改,本次试验只是新增了几个类。
为了实现weekDay功能,利用了Kim Larsson calculation formula,可以很方便得通过公式计算出星期
1 public static int weekDay(int y,int m,int d) 2 { 3 if(m==1||m==2) 4 { 5 m+=12; 6 y--; 7 } 8 int iWeek = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; 9 switch(iWeek){ 10 case 0: return 1; 11 case 1: return 2; 12 case 2: return 3; 13 case 3: return 4; 14 case 4: return 5; 15 case 5: return 6; 16 case 6: return 7; 17 } 18 return 0; 19 }
此外,还实现了lastday
1 public static String lastdate(int y,int m,int d) 2 { 3 month[2]=28; 4 if(checkLeapYear(y)==1) 5 { 6 month[2]=29; 7 } 8 switch (checkVal(y,m,d)) 9 { 10 case 1: 11 return "月份超出范围"; 12 case 2: 13 return "日期超出范围"; 14 case 3: 15 return "年份超出范围"; 16 default: 17 if(d>1) 18 { 19 return getString(y, m, d-1); 20 } 21 if(d==1) 22 { 23 if(checkFirstday(y, m, d)==1) 24 { 25 return getString(y-1, 12, 31); 26 } 27 else 28 { 29 return getString(y, m-1, month[m-1]); 30 } 31 } 32 return "程序错误"; 33 } 34 }
模型部分完整代码如下:
1 package main; 2 import java.util.Scanner; 3 4 public class run { 5 public static int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 6 public static int weekDay(int y,int m,int d) 7 { 8 if(m==1||m==2) 9 { 10 m+=12; 11 y--; 12 } 13 int iWeek = (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; 14 switch(iWeek){ 15 case 0: return 1; 16 case 1: return 2; 17 case 2: return 3; 18 case 3: return 4; 19 case 4: return 5; 20 case 5: return 6; 21 case 6: return 7; 22 } 23 return 0; 24 } 25 public static int checkVal(int y,int m,int d)//check whether the inputs are legal or not 26 { 27 if(m<1||m>12) 28 return 1; 29 if(d<1||d>month[m]) 30 return 2; 31 if(y<1912||y>2050) 32 return 3; 33 return 0; 34 } 35 36 public static int checkLeapYear(int y) ////check if y year is leap year return 1 otherwise return 0 37 { 38 if(y%4==0&&y%100!=0) 39 return 1; 40 if(y%100==0&&y%400==0) 41 return 1; 42 return 0; 43 } 44 public static int checkLastday(int y,int m,int d)//check whether the date is the last day of a year,if it is return 1 otherwise return 0 45 { 46 if(m==12&&d==31) 47 return 1; 48 else 49 return 0; 50 } 51 52 public static String getString(int y,int m,int d)//generate date in string form 53 { 54 return y+"年"+m+"月"+d+"日"; 55 } 56 public static int checkFirstday(int y,int m,int d)//if it is the first day of a year return 1 otherwise 0 57 { 58 if(m==1&&d==1) 59 return 1; 60 else 61 return 0; 62 } 63 64 public static String nextdate(int y,int m,int d)//calculate next date, result is presented in "xxxx年x月x日" form 65 { 66 month[2]=28; 67 if(checkLeapYear(y)==1) 68 { 69 month[2]=29; 70 } 71 switch (checkVal(y,m,d)) 72 { 73 case 1: 74 return "月份超出范围"; 75 case 2: 76 return "日期超出范围"; 77 case 3: 78 return "年份超出范围"; 79 default: 80 if(d<month[m]&&checkLastday(y,m,d)==0) 81 { 82 return getString(y, m, d+1); 83 } 84 if(d==month[m]) 85 { 86 if(checkLastday(y, m, d)==1) 87 { 88 return getString(y+1, 1, 1); 89 } 90 else 91 { 92 return getString(y, m+1, 1); 93 } 94 } 95 return "程序错误"; 96 } 97 } 98 public static String lastdate(int y,int m,int d) 99 { 100 month[2]=28; 101 if(checkLeapYear(y)==1) 102 { 103 month[2]=29; 104 } 105 switch (checkVal(y,m,d)) 106 { 107 case 1: 108 return "月份超出范围"; 109 case 2: 110 return "日期超出范围"; 111 case 3: 112 return "年份超出范围"; 113 default: 114 if(d>1) 115 { 116 return getString(y, m, d-1); 117 } 118 if(d==1) 119 { 120 if(checkFirstday(y, m, d)==1) 121 { 122 return getString(y-1, 12, 31); 123 } 124 else 125 { 126 return getString(y, m-1, month[m-1]); 127 } 128 } 129 return "程序错误"; 130 } 131 } 132 public static void main (String[] args) 133 { 134 while(true) 135 { 136 System.out.println("请输入日期"); 137 int y,m,d; 138 Scanner input=new Scanner(System.in); 139 y=input.nextInt(); 140 if(y==-1) 141 break; 142 else 143 { 144 m=input.nextInt(); 145 d=input.nextInt(); 146 System.out.println(nextdate(y,m,d)); 147 } 148 } 149 } 150 }
界面部分主要任务就是使用上面写好的类,以及控制合法输入的问题:
限制只允许输入数字的核心代码:
1 inputDay.addKeyListener(new KeyAdapter(){ 2 public void keyTyped(KeyEvent e) { 3 int keyChar = e.getKeyChar(); 4 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 5 6 }else{ 7 e.consume(); 8 } 9 } 10 });
OK按钮的动作:
1 JButton okButton = new JButton("OK"); 2 okButton.addActionListener(new ActionListener() { 3 @Override 4 public void actionPerformed(ActionEvent e) 5 { 6 //get input info 7 String s_y = inputYear.getText(); 8 String s_m = inputMonth.getText(); 9 String s_d = inputDay.getText(); 10 //parse String to Int 11 int y = Integer.parseInt(s_y); 12 int m = Integer.parseInt(s_m); 13 int d = Integer.parseInt(s_d); 14 //get next day 15 String outputNextday = run.nextdate(y, m, d); 16 System.out.println(outputNextday); 17 String ans_first=(String) outputNextday.subSequence(0, 1); 18 System.out.println(ans_first); 19 if(isNumeric(ans_first)==true) 20 { 21 nextdayOutput.setText(outputNextday); 22 //get previous day 23 String outputLastday = run.lastdate(y, m, d); 24 lastdayOutput.setText(outputLastday); 25 //get week day 26 int weekday = run.weekDay(y, m, d); 27 weekdayOutput.setText(String.valueOf(weekday)); 28 } 29 else 30 { 31 //pop up a waring window 32 JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数"); 33 //clear all input blocks 34 inputYear.setText(""); 35 inputMonth.setText(""); 36 inputDay.setText(""); 37 lastdayOutput.setText(""); 38 nextdayOutput.setText(""); 39 weekdayOutput.setText(""); 40 } 41 } 42 });
Cancel按钮的动作:
1 JButton cancelButton = new JButton("Cancel"); 2 cancelButton.addActionListener(new ActionListener() { 3 @Override 4 public void actionPerformed(ActionEvent e) { 5 //shutdown application 6 inputYear.setText(""); 7 inputMonth.setText(""); 8 inputDay.setText(""); 9 lastdayOutput.setText(""); 10 nextdayOutput.setText(""); 11 weekdayOutput.setText(""); 12 } 13 });
界面部分的完整代码如下:
1 package main; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 import javax.swing.JOptionPane; 7 import javax.swing.JButton; 8 import java.awt.BorderLayout; 9 import javax.swing.JEditorPane; 10 import javax.swing.JTextField; 11 import javax.swing.JTextPane; 12 import java.awt.Color; 13 import java.awt.SystemColor; 14 import java.awt.event.ActionListener; 15 import java.awt.event.KeyAdapter; 16 import java.awt.event.KeyEvent; 17 import java.awt.event.ActionEvent; 18 19 public class WindowForRun { 20 21 private JFrame frame; 22 private JTextField inputYear; 23 private JTextField inputMonth; 24 private JTextField inputDay; 25 private JTextField weekdayOutput; 26 private JTextField nextdayOutput; 27 private JTextField lastdayOutput; 28 29 public static boolean isNumeric(String str) 30 { 31 for(int i=0;i<str.length();i++) 32 { 33 if(!Character.isDigit(str.charAt(i))) 34 { 35 return false; 36 } 37 } 38 return true; 39 } 40 /** 41 * Launch the application. 42 */ 43 public static void main(String[] args) { 44 EventQueue.invokeLater(new Runnable() { 45 public void run() { 46 try { 47 WindowForRun window = new WindowForRun(); 48 window.frame.setVisible(true); 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } 52 } 53 }); 54 } 55 56 /** 57 * Create the application. 58 */ 59 public WindowForRun() { 60 initialize(); 61 } 62 /** 63 * Initialize the contents of the frame. 64 */ 65 private void initialize() { 66 frame = new JFrame(); 67 frame.setBounds(100, 100, 296, 300); 68 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 69 frame.getContentPane().setLayout(null); 70 71 JButton okButton = new JButton("OK"); 72 okButton.addActionListener(new ActionListener() { 73 @Override 74 public void actionPerformed(ActionEvent e) 75 { 76 //get input info 77 String s_y = inputYear.getText(); 78 String s_m = inputMonth.getText(); 79 String s_d = inputDay.getText(); 80 //parse String to Int 81 int y = Integer.parseInt(s_y); 82 int m = Integer.parseInt(s_m); 83 int d = Integer.parseInt(s_d); 84 //get next day 85 String outputNextday = run.nextdate(y, m, d); 86 System.out.println(outputNextday); 87 String ans_first=(String) outputNextday.subSequence(0, 1); 88 System.out.println(ans_first); 89 if(isNumeric(ans_first)==true) 90 { 91 nextdayOutput.setText(outputNextday); 92 //get previous day 93 String outputLastday = run.lastdate(y, m, d); 94 lastdayOutput.setText(outputLastday); 95 //get week day 96 int weekday = run.weekDay(y, m, d); 97 weekdayOutput.setText(String.valueOf(weekday)); 98 } 99 else 100 { 101 //pop up a waring window 102 JOptionPane.showMessageDialog(null, "输入有误,请重请输入正确整数"); 103 //clear all input blocks 104 inputYear.setText(""); 105 inputMonth.setText(""); 106 inputDay.setText(""); 107 lastdayOutput.setText(""); 108 nextdayOutput.setText(""); 109 weekdayOutput.setText(""); 110 } 111 } 112 }); 113 okButton.setBounds(29, 132, 92, 29); 114 frame.getContentPane().add(okButton); 115 116 JButton cancelButton = new JButton("Cancel"); 117 cancelButton.addActionListener(new ActionListener() { 118 @Override 119 public void actionPerformed(ActionEvent e) { 120 //clear all input blocks 121 inputYear.setText(""); 122 inputMonth.setText(""); 123 inputDay.setText(""); 124 lastdayOutput.setText(""); 125 nextdayOutput.setText(""); 126 weekdayOutput.setText(""); 127 } 128 }); 129 cancelButton.setBounds(173, 132, 92, 29); 130 frame.getContentPane().add(cancelButton); 131 132 inputYear = new JTextField(); 133 inputYear.setBounds(43, 27, 92, 29); 134 frame.getContentPane().add(inputYear); 135 inputYear.setColumns(10); 136 //limit input to integer 137 inputYear.addKeyListener(new KeyAdapter(){ 138 public void keyTyped(KeyEvent e) { 139 int keyChar = e.getKeyChar(); 140 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 141 142 }else{ 143 e.consume(); 144 } 145 } 146 }); 147 148 inputMonth = new JTextField(); 149 inputMonth.setColumns(10); 150 inputMonth.setBounds(173, 27, 36, 29); 151 frame.getContentPane().add(inputMonth); 152 //limit input to integer 153 inputMonth.addKeyListener(new KeyAdapter(){ 154 public void keyTyped(KeyEvent e) { 155 int keyChar = e.getKeyChar(); 156 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 157 158 }else{ 159 e.consume(); 160 } 161 } 162 }); 163 164 inputDay = new JTextField(); 165 inputDay.setColumns(10); 166 inputDay.setBounds(243, 27, 36, 29); 167 frame.getContentPane().add(inputDay); 168 //limit input to integer 169 inputDay.addKeyListener(new KeyAdapter(){ 170 public void keyTyped(KeyEvent e) { 171 int keyChar = e.getKeyChar(); 172 if(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9){ 173 174 }else{ 175 e.consume(); 176 } 177 } 178 }); 179 180 JTextPane textPane = new JTextPane(); 181 textPane.setBackground(SystemColor.window); 182 textPane.setText("年:"); 183 textPane.setBounds(17, 33, 26, 18); 184 frame.getContentPane().add(textPane); 185 186 JTextPane textPane_1 = new JTextPane(); 187 textPane_1.setText("月:"); 188 textPane_1.setBackground(SystemColor.window); 189 textPane_1.setBounds(148, 33, 26, 18); 190 frame.getContentPane().add(textPane_1); 191 192 JTextPane textPane_2 = new JTextPane(); 193 textPane_2.setText("日:"); 194 textPane_2.setBackground(SystemColor.window); 195 textPane_2.setBounds(221, 33, 26, 18); 196 frame.getContentPane().add(textPane_2); 197 198 JTextPane textPane_3 = new JTextPane(); 199 textPane_3.setText("这天是星期:"); 200 textPane_3.setBackground(SystemColor.window); 201 textPane_3.setBounds(29, 184, 78, 18); 202 frame.getContentPane().add(textPane_3); 203 204 weekdayOutput = new JTextField(); 205 weekdayOutput.setColumns(10); 206 weekdayOutput.setBounds(105, 173, 26, 29); 207 frame.getContentPane().add(weekdayOutput); 208 209 JTextPane textPane_4 = new JTextPane(); 210 textPane_4.setText("下一天是:"); 211 textPane_4.setBackground(SystemColor.window); 212 textPane_4.setBounds(29, 227, 78, 18); 213 frame.getContentPane().add(textPane_4); 214 215 JTextPane textPane_5 = new JTextPane(); 216 textPane_5.setText("上一天是:"); 217 textPane_5.setBackground(SystemColor.window); 218 textPane_5.setBounds(29, 254, 78, 18); 219 frame.getContentPane().add(textPane_5); 220 221 nextdayOutput = new JTextField(); 222 nextdayOutput.setColumns(10); 223 nextdayOutput.setBounds(105, 214, 151, 29); 224 frame.getContentPane().add(nextdayOutput); 225 226 lastdayOutput = new JTextField(); 227 lastdayOutput.setColumns(10); 228 lastdayOutput.setBounds(105, 243, 151, 29); 229 frame.getContentPane().add(lastdayOutput); 230 } 231 }
最终效果如下:

posted on 2017-04-30 00:26 StackOverflow! 阅读(329) 评论(0) 收藏 举报
浙公网安备 33010602011771号