1、实验一 Java常用工具类编程实验一 Java常用工具类编程1.1实验指导1、String类使用String 类表示字符串。 在 Java 程序中所有的字符串常量,如 abc,都被实现为这个类的实例。 1)、构造函数 String s1=java; String s2=new String(java); 2)、比较函数 =比较引用是否相同 if(s1=s2) /返回falseequals():比较串内容是否相同 if(s1.equals(s2)/返回true compareTo():比较内容,返回数字 pareTo(s2) 如果s1s2 则返回0 如果s1=s2 则返回0 如果s1s2 则返回
2、0 3)、取子串substring(),取字符charAt(index) String s1=hello java; s1.substring(start,end); s1.substring(0,3);/start到end-1 String s2=s1.substring(6,10); 4)、分割split(); String s1=c+,java,; String str=s1.split(,); String s2=c+.java.vb; String s2=c+.java.vb dotnet; String str=s2.split(.| ); for(int i=0;istr.len
3、gth;i+) System.out.println(stri);2、StringBuffer类StringBuffer public StringBuffer()构造一个不包含字符的字符串缓冲区,其初始的容量设为 16 个字符。 StringBuffer public StringBuffer(int length):构造一个不包含字符的字符串缓冲区,其初始的容量由参数 length 设置。 StringBuffer public StringBuffer(String str):构造一个字符串缓冲区,来表示和字符串参数相同的字符序列。 length public int length():
4、返回字符串缓冲区的长度 (字符数)。 capacity public int capacity():返回字符串缓冲区的当前容量。 该容量表示可用于插入新的字符的存储空间;超出该容量时会发生新的容量分配。 ensureCapacity public synchronized void ensureCapacity(int minimumCapacity):保证缓冲区的容量至少等于指定的最小数。 如果字符串缓冲区的当前容量少于该参数,则分配一个新的更大的内部缓冲区。 新容量将取如下参数中较大的一个: setLength public synchronized void setLength(int
5、newLength):设置该字符串缓冲区的长度。 如果参数 newLength 小于该字符串缓冲区的当前长度。 该字符串缓冲区将被截断来包含恰好等于由参数 newLength 给出的字符数。 append public synchronized StringBuffer append(Object obj):把 Object 型参数的字符串表示添加到该字符串缓冲区。 StringBuffer x = new StringBuffer().append(a).append(4).append(c) .toString();insert public synchronized StringBuff
6、er insert(int offset, Object obj):把 Object 型参数的字符串表示插入到字符串缓冲区。 reverse public synchronized StringBuffer reverse():该字符串缓冲区的字符序列被其反向字符序列所替换。 toString public String toString():转换为一个表示该字符串缓冲区数据的字符串。 分配一个新的 String 对象,并且用字符串缓冲区所表示的字符序列进行初始化。 于是此 String 被返回。 随后缓冲区发生的变化不再影响该 String 的内容。 3、日期类示例1)获取服务器端当前日期:
7、 import java.util.Date; Date myDate = new Date();2) 获取当前年、月、日:Date myDate = new Date();int thisYear = myDate.getYear() + 1900;/thisYear = 2009int thisMonth = myDate.getMonth() + 1;/thisMonth = 10int thisDate = myDate.getDate();/thisDate = 303)按本地时区输出当前日期Date myDate = new Date();out.println(myDate.to
8、LocaleString();输出结果为:2003-5-30 4)按照指定格式打印日期import java.util.Date;import java.text.DateFormat;Date dNow = new Date();SimpleDateFormat formatter =new SimpleDateFormat(E yyyy.MM.dd at hh:mm:ss a zzz);System.out.println(It is + formatter.format(dNow);输出的结果为:It is 星期五 2003.05.30 at 11:30:46 上午 CST 5)将字符串
9、转换为日期import java.util.Date;String input = 1222-11-11;SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd);Date t = null;try t = formatter.parse(input);System.out.println(t); catch(ParseException e) System.out.println(unparseable using + formatter); 输出结果为:Fri Nov 11 00:00:00 CST 1222 6) 计算日期
10、之间的间隔 getTime()函数返回日期与1900-01-01 00:00:00相差的毫秒数Import java.util.Date;import java.text.DateFormat;String input = 2003-05-01;SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd);Date d1 = null;Date d2 = new Date();long diff = d2.getTime() - d1.getTime();out.println(Difference is + (diff/(1000
11、*60*60*24) + days.);输出结果为:Difference is 29 days. 7) 日期的加减运算方法:用Calendar类的add()方法Calendar now = Calendar.getInstance();SimpleDateFormat formatter = new SimpleDateFormat(E yyyy.MM.dd at hh:mm:ss a zzz);out.println(It is now + formatter.format(now.getTime();now.add(Calendar.DAY_OF_YEAR,-(365*2);out.pri
12、ntln();out.println(Two years ago was + formatter.format(now.getTime();输出结果为:It is now 星期五 2003.05.30 at 01:45:32 下午 CST Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST 8) 比较日期方法:用equals()、before()、after()方法DateFormat df = new SimpleDateFormat(yyy-MM-dd);Date d1 = df.parse(2000-01-01);Date d2 = d
13、f.parse(1999-12-31);String relation = null;if(d1.equals(d2) relation = the same date as;else if(d1.before(d2) relation = before;else relation = after;System.out.println(d1 + is + relation + + d2);输出结果为:Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 19991.2实验题目1、 使用类String类的分割split 将字符
14、串 “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” 单词提取输出。单词以空格或,分割。参照课本P10 例2.5运行代码:package EX1_1;public class EX1_1 public static void main(String args) String str1=new String(Solution
15、s 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 str2=str1.split( |,); for(int i=0;istr2.length;i+) System.out.println(str2i); 运行截图:2、 调试p14 例2.8,将程序加上注释。运行代码:package EX1_2;public class EX
16、1_2 public static void stringRepalce(String text) text=text.replace(j,i);/替换函数 把j替换成i public static void bufferRepalce(StringBuffer text) text=text.append(EE);/把 Object 型参数的字符串表示添加到该字符串缓冲区 public static void main(String args) String ts=new String(java);/字符型 StringBuffer tb=new StringBuffer(java); /S
17、tringBuffer型 stringRepalce(ts);/调用函数 bufferRepalce(tb); System.out.println(ts+ +tb);/打印输出 运行截图:stringRepalce函数中传进来的只是形参,是对原值的拷贝,原值不改变。bufferRepalce函数是对字符串缓冲区进行修改,是对原值的修改,故发生改变。3、 调试p15 例2.10,将程序加上注释。运行代码:package EX1_3;import java.text.SimpleDateFormat;import java.util.Date;public class EX1_3 public
18、static void main(String args) SimpleDateFormat format1=new SimpleDateFormat(yyyy年MM月dd日HH时mm分ss秒);/SimpleDateFormat指定格式输出 SimpleDateFormat format2=new SimpleDateFormat(yy/MM/dd/ HH:mm); SimpleDateFormat format3=new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); SimpleDateFormat format4=new SimpleDateFormat(
19、yyyy年MM月dd日HH时mm分ss秒 E); Date date=new Date(); 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日与系统当前日期相差的天数。
20、参照题3与书本上P17完成。运行代码:package EX1_4;import java.util.Date;import java.text.ParseException;import java.text.SimpleDateFormat;public class EX1_4 public static void main(String args) String input = 2010-05-01; SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd); Date d1 = null; Date d2 = new Dat
21、e(); try d1= formatter.parse(input); catch (ParseException e) e.printStackTrace(); long diff = d2.getTime() - d1.getTime(); System.out.println(Difference is + (diff/(1000*60*60*24) + days.); 运行截图:5、 设计一个类Student,类的属性有:姓名,学号,出生日期,性别,所在系等。并生成学生类对象数组。按照学生的姓名将学生排序输出。使用String类的compareTo方法。1)、定义学生类 class
22、Student private String sno,sname,sbirth,ssex,sdept;/构造函数,set-get函数 2)、创建一个测试类 public class Ex1_2 public Student initStudent() /初始化学生信息 Student s=new Student5; String names=zhou,zhang,liu,li,xu; ./定义几个数组放置属性信息 for(int i=0;is.length;i+) si=new Student(nosi,namesi,birthsi,sessi,deptsi); return s; publi
23、c void sortStudent(Student s)/排序按照姓名,选择法 for(int i=0;is.length-1;i+) int min=i; for(int j=i+1;j0) min=j; if(min!=i) Student t=si;si=smin;smin=t; public void dispStudent(Student s)/输出学生信息 . public static void main(String args) Ex1_2 obj=new Ex1_2(); Student s=obj.initStudent(); obj.sortStudent(s); ob
24、j.dispStudent(s); 运行代码:package EX1_5;public class Student private String sno,sname,sbirth,ssex,sdept; Student(String sno,String sname,String sbirth,String ssex,String sdept) this.sno=sno; this.sname=sname; this.sbirth=sbirth; this.ssex=ssex; this.sdept=sdept; public String getSno() return sno; publi
25、c 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
26、ssex) this.ssex = ssex; public String getSdept() return sdept; public void setSdept(String sdept) this.sdept = sdept; package EX1_5;public class Test public Student initStudent() /初始化学生信息 Student s=new Student5; String snos=20101706,20101707,20101708,20101709,20101710; String names=gaojian,cuichengd
27、i,huangyongsheng,huangwei,pengchonglin; String births=1990-01-01,1990-01-02,1990-01-03,1990-01-04,1990-01-05; String sess=男,男,男,女,男; String depts=计算机,数学,英语,测控,经济; for(int i=0;is.length;i+) si=new Student(snosi,namesi,birthsi,sessi,deptsi); return s; public void sortStudent(Student s)/排序按照姓名,选择法 for(
28、int i=0;is.length-1;i+) int min=i; for(int j=i+1;j0) min=j; if(min!=i) Student t=si; si=smin; smin=t; public void dispStudent(Student s) for(int i=0;is.length;i+) System.out.println(si.getSno()+ +si.getSname()+ +si.getSbirth()+ +si.getSsex()+ +si.getSdept(); public static void main(String args) Test obj=new Test(); Student s=obj.initStudent(); obj.sortStudent(s); obj.dispStudent(s); 运行截图:6、使用日历类等相关方法 按截图做出一个日历 参照书本示例,研究其中代码回顾与复习利用Java Swing编程。参考:以下函数根据输入的年和月计算相应的数字public voi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1