该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解
1、 使用类String类的分割split 将字符串 “Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel” 单词提取输出。单词以空格或,分割。
package java常用工具类编程; public class Splittest { public static void main(String[] args) { String s1=new String("Solutions to selected exercises can be found in the electronic document The " + "Thinking in Java Annotated Solution Guide, available for a small fee from BruceEckel"); String[] s=s1.split(" "); for(String str:s) { System.out.print(str+","); } } }
示例截图
2、 调试p14 例2.8,将程序加上注释。
package java常用工具类编程; public class StringAndStringBuffer { /* * 对string对象使用替换函数 生成新string其地址被改变 */ public static void stringReplace(String text) { text=text.replace('j', 'i'); } /* * 对StringBuffer对象使用append函数 指向地址不变 内容改变 */ public static void bufferReplace(StringBuffer text) { text=text.append(" EE"); } public static void main(String[] args) { //声明String和StringBuffer String ts=new String("java"); StringBuffer tb=new StringBuffer("java"); //调用函数 stringReplace(ts); bufferReplace(tb); System.out.println(ts+","+tb); } }
示例截图
3、 调试p15 例2.10,将程序加上注释。
package java常用工具类编程; import java.text.SimpleDateFormat; import java.util.Date; public class 日期格式化示例 { public static void main(String[] args) { //设置时间格式化 SimpleDateFormat format1=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒"); SimpleDateFormat format2=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat format3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format4=new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒E"); //获取当前时间 Date date=new Date(); //输出当前时间 对应的4个格式化 System.out.println(format1.format(date)); System.out.println(format2.format(date)); System.out.println(format3.format(date)); System.out.println(format4.format(date)); //输出当前时间 按初始格式化 System.out.println(date.toString()); } }
示例截图
4、设计一个程序计算2010-05-01日与系统当前日期相差的天数。
package java常用工具类编程; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class 计算日期差 { public static void main(String[] args) throws ParseException { Date date1=new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); String s=new String("2010-5-1"); Date date2 = format.parse(s); int days=(int)((date1.getTime()-date2.getTime())/(1000*60*60*24)); System.out.println("今天日期:"+format.format(date1)+"距离"+s+" "+days+"天"); } }
示例截图
5、 完成一个日期工具类MyCalendar,实现功能上与Calendar相似(即实现其get以及set函数),利用Date以及DateFormat类去完成.
package java常用工具类编程; import java.text.SimpleDateFormat; import java.util.Date; public class MyCalendar { Date date; MyCalendar(){ date=new Date(); } public String get() { SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); return format.format(date); } public String set(String s1,int num) { String currenttime = get(); String changetime=""; String[] arr = currenttime.split("-"); if(s1.equalsIgnoreCase("year")) { arr[0]=Integer.toString(num); }else if(s1.equalsIgnoreCase("month")) { arr[1]=Integer.toString(num); }else if(s1.equalsIgnoreCase("day")) { arr[2]=Integer.toString(num); }else return "数据有误"; for(int i=0;i<2;i++) { changetime+=arr[i]; changetime+="-"; } changetime+=arr[2]; return changetime; } public static void main(String[] args) { MyCalendar c = new MyCalendar(); System.out.println("得到当前日期"+c.get()); System.out.println("年份修改后日期"+c.set("year", 2011)); } }
示例截图
6、设计一个类Student,类的属性有:姓名,学号,出生日期,性别,所在系等。并生成学生类对象数组。按照学生的姓名将学生排序输出。使用String类的compareTo方法。
1)、定义学生类
package java常用工具类编程; public class Student { private String sno; private String sname; private String sbirth; private String ssex; private String sdept; public Student(String sno, String sname, String sbirth, String ssex, String sdept) { super(); this.sno = sno; this.sname = sname; this.sbirth = sbirth; this.ssex = ssex; this.sdept = sdept; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSbirth() { return sbirth; } public void setSbirth(String sbirth) { this.sbirth = sbirth; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public String getSdept() { return sdept; } public void setSdept(String sdept) { this.sdept = sdept; } @Override public String toString() { return "sno=" + sno + ", sname=" + sname + ", sbirth=" + sbirth + ", ssex=" + ssex + ", sdept=" + sdept; } }
2)、定义测试类
package java常用工具类编程; public class TestStudent { public Student[] initStudent(){ //初始化学生信息 Student s[]=new Student[5]; String[] names={"zhou","zhang","liu","li","xu"}; String[] nos= {"1","2","3","4","5"}; String[] births= {"1999/4/5","1998/12/7","1996/11/6","1999/1/25","1999/3/2"}; String[] sess= {"M","F","F","M","F"}; String[] depts= {"计算机","经管","自动化","电气","国交"}; for(int i=0;i<s.length;i++) s[i]=new Student(nos[i],names[i],births[i],sess[i],depts[i]); return s; } public void sortStudent(Student[] s){//排序按照姓名,选择法 for(int i=0;i<s.length-1;i++){ int min=i; for(int j=i+1;j<s.length;j++) if((s[min].getSname().compareTo(s[j].getSname())>0)) min=j; if(min!=i){ Student t=s[i];s[i]=s[min];s[min]=t; } } } public void dispStudent(Student[] s){//输出学生信息 for(Student ss:s) { System.out.println(ss); } } public static void main(String[] args){ TestStudent obj=new TestStudent(); Student[] s=obj.initStudent(); obj.sortStudent(s); obj.dispStudent(s); } }
示例截图
7、使用日历类等相关方法 按截图做出一个日历 参照书本示例,研究其中代码回顾与复习利用Java Swing编程。
package java常用工具类编程; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; public class CalendarSwing extends JFrame{ private static final long serialVersionUID = 1L; JTable table; JPanel jp1; CalendarSwing(){ JLabel year=new JLabel("年"); JLabel month=new JLabel("月"); JButton confirm=new JButton("确认"); JComboBox<String> yearchoose=new JComboBox<>(); for(int i=1980;i<2050;i++) yearchoose.addItem(i+""); JComboBox<String> monthchoose=new JComboBox<>(); for(int i=1;i<13;i++) monthchoose.addItem(i+""); jp1=new JPanel(); jp1.add(yearchoose); jp1.add(year); jp1.add(monthchoose); jp1.add(month); jp1.add(confirm); add(jp1,BorderLayout.NORTH); setTitle("cc的日历"); setBounds(400,300,400,400); validate(); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); confirm.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { showCalendar(Integer.parseInt(yearchoose.getSelectedItem().toString()),Integer.parseInt(monthchoose.getSelectedItem().toString())); } }); } public void showCalendar(int year,int month){ Calendar cal=Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); //计算当前月一共有多少天 int days=cal.getActualMaximum(Calendar.DAY_OF_MONTH); //计算当前月的1号为星期几 cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号 int firstweek=cal.get(Calendar.DAY_OF_WEEK)-1; Object[] title = {"日", "一", "二", "三", "四", "五", "六"}; Object[][] a=new Object[6][7]; int day=1; boolean flag=false; boolean ispass=false; for(int i=0;i<6;i++) { for(int j=0;j<7;j++) { if((firstweek%7)==j&&!ispass) { flag=true; ispass=true; } if(!flag) a[i][j]=""; else { a[i][j]=Integer.toString(day); if(day<days) day++; else flag=false; } } } getContentPane().removeAll(); table=new JTable(a,title); add(jp1,BorderLayout.NORTH); add(new JScrollPane(table),BorderLayout.CENTER); validate(); } public static void main(String[] args) { new CalendarSwing(); } }
示例截图