柯侧耳倾听者 - 云代码空间
—— 翱翔在Java世界的海洋之上
Java基础类库2(2学时)
一、实验目的与要求
1. 熟练掌握String,StringBuffer和StringBuilder
2. Math类
3. Java7中的ThreadLocalRandom与Random
4. BigDecimal类
5. Java8的日期,时间类
6. 正则表达式
二、实验原理
Oracle为Java提供了丰富的基础类库,Java 8提供了4000多个基础类(包括下一章将要介绍的集合框架),通过这些基础类库可以提高开发效率,降低开发难度。对于合格的Java程序员而言,至少要熟悉Java SE 70%以上的类(当然本书并不是让读者去背诵Java API文档),但在反复查阅API文档的过程中,会记住大部分类的功能、方法,因此程序员一定要多练,多敲代码。
三、预习与准备
1. 熟练掌握String,StringBuffer和StringBuilder
2. Math类
3. Java7中的ThreadLocalRandom与Random
4. BigDecimal类
5. Java8的日期,时间类
6. 正则表达式
7. 国际化与格式化
四、实验内容
1. 使用String,StringBuffer,StringBuilder完成字符串的一些功能;
2. 使用ThreadLocaiRadom和Random来产生随机数
3. 使用BigDecimal类运行外double数据的运算
4. 使用Date和Calendar类获取时间和日期
5. 学会使用正则表达式
6. 国际化与格式化
五、实验过程
1. 熟练掌握String,StringBuffer和StringBuilder
package ke02;
public class StringTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.complareTo(s2):"+s1.compareTo(s2));
System.out.println("s1.complareTo(s3):"+s1.compareTo(s3));
String s4="fkit.org";
String s5=".org";
System.out.println("s4.endWith(s5)"+s4.endsWith(s5));
char[] s6={'I',' ','l','o','v','e',' ','j','a','v','a'};
String s7 =new String("ejb");
s2.getChars(0, 3, s6, 7);
System.out.println(s6);
String s="www.fkit.org";
String ss="it";
System.out.println("s.indexOf('r'):"+s.indexOf('r'));
System.out.println("s.indexOf('r',2):"+s.indexOf('r',2));
System.out.println("s.indexOf(ss):"+s.indexOf(ss));
}
}
2. 使用ThreadLocaiRadom和Random来产生随机数
package ke02;
import java.util.Arrays;
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();
System.out.println("rand.nextBoolean():"+rand.nextBoolean());
byte[] buffer = new byte[16];
rand.nextBytes(buffer);
System.out.println(Arrays.toString(buffer));
System.out.println("rand.nextDouble()"+rand.nextDouble());
System.out.println("rand.nextFloat()"+rand.nextFloat());
System.out.println("rand.nextGaussian()"+rand.nextGaussian());
System.out.println("rand.nextInt()"+rand.nextInt());
System.out.println("rand.nextInt(26)"+rand.nextInt(26));
System.out.println("rand.nextLong()"+rand.nextLong());
}
}
package ke02;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class SeedTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r1= new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r1.nextBoolean():\t"+r1.nextBoolean());
System.out.println("r1.nextInt():\t"+r1.nextInt());
System.out.println("r1.nextDouble():\t"+r1.nextDouble());
System.out.println("r1.nextGaussian():\t"+r1.nextGaussian());
Random r2= new Random(50);
System.out.println("第一个种子为50的Random对象");
System.out.println("r2.nextBoolean():\t"+r2.nextBoolean());
System.out.println("r2.nextInt():\t"+r2.nextInt());
System.out.println("r2.nextDouble():\t"+r2.nextDouble());
System.out.println("r2.nextGaussian():\t"+r2.nextGaussian());
Random r3= new Random(100);
System.out.println("第一个种子为50的Random对象");
System.out.println("r3.nextBoolean():\t"+r3.nextBoolean());
System.out.println("r3.nextInt():\t"+r3.nextInt());
System.out.println("r3.nextDouble():\t"+r3.nextDouble());
System.out.println("r3.nextGaussian():\t"+r3.nextGaussian());
ThreadLocalRandom rand = ThreadLocalRandom.current();
int val1 = rand.nextInt(4,20);
double val2 =rand.nextDouble(2.0,10.0);
System.out.println(val1);
System.out.println(val2);
}
}
3. BigDecimal类
package ke02;
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigDecimal f1=new BigDecimal("0.05");
BigDecimal f2=BigDecimal.valueOf(0.01);
BigDecimal f3=new BigDecimal(0.05);
System.out.println("使用String作为BigDeciaml构造器");
System.out.println("0.05+0.01 = "+f1.add(f2));
System.out.println("0.05-0.01 = "+f1.subtract(f2));
System.out.println("0.05*0.01 = "+f1.multiply(f2));
System.out.println("0.05/0.01 = "+f1.divide(f2));
System.out.println("使用String作为BigDeciaml构造器");
System.out.println("0.05+0.01 = "+f3.add(f2));
System.out.println("0.05-0.01 = "+f3.subtract(f2));
System.out.println("0.05*0.01 = "+f3.multiply(f2));
System.out.println("0.05/0.01 = "+f3.divide(f2));
}
}
4. Math类
package ke02;
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
BigDecimal f1=new BigDecimal("0.05");
BigDecimal f2=BigDecimal.valueOf(0.01);
BigDecimal f3=new BigDecimal(0.05);
System.out.println("使用String作为BigDeciaml构造器");
System.out.println("0.05+0.01 = "+f1.add(f2));
System.out.println("0.05-0.01 = "+f1.subtract(f2));
System.out.println("0.05*0.01 = "+f1.multiply(f2));
System.out.println("0.05/0.01 = "+f1.divide(f2));
System.out.println("使用String作为BigDeciaml构造器");
System.out.println("0.05+0.01 = "+f3.add(f2));
System.out.println("0.05-0.01 = "+f3.subtract(f2));
System.out.println("0.05*0.01 = "+f3.multiply(f2));
System.out.println("0.05/0.01 = "+f3.divide(f2));
}
}
5.日期,时间类
package ke02;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date d1 = new Date();
Date d2 = new Date(System.currentTimeMillis()+100);
System.out.println(d2);
System.out.println(d1.compareTo(d2));
System.out.println(d1.before(d2));
}
}
package ke02;
import java.util.Calendar;
public class CalendarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar c= Calendar.getInstance();
System.out.println(c.get(Calendar.YEAR));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.DATE));
c.set(2003,10,23,12,32,23);
c.add(Calendar.YEAR, -1);
c.roll(Calendar.MONTH, -8);
System.out.println(c.getTime());
}
}
package ke02;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatTest {
public static void main(String[] args) throws ParseException
{
Date d = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("Gyyyy年中的第D天");
String dateStr = sdf1.format(d);
System.out.println(dateStr);
String str = "14###三月##21";
SimpleDateFormat sdf2 = new SimpleDateFormat("y###MMM##d");
System.out.println(sdf2.parse(str));
}
}
6.正则表达式
package ke02;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FindGroup {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "我想求购一本《疯狂Java讲义》,请尽快联系我135000066666"
+"交朋友,电话号码1361112563"
+"出售二手电脑,联系方式15899903312";
Matcher m = Pattern.compile("((13\\d)|(15\\d))\\d{8}").matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
}
六、实验效果
1. 使用String,StringBuffer,StringBuilder完成字符串的一些功能;
2. 使用ThreadLocaiRadom和Random来产生随机数
3. 使用BigDecimal类运行外double数据的运算
4. 使用Date和Calendar类获取时间和日期
5. 学会使用正则表达式
六、实验总结与体会
通过这次课的学习,初步掌握了java基础类的基本用法和一些常用的方法,也发现了自身的一些问题,比如不注重细节,导致程序无法运行,同时也发现要与同学进行交流与合作,及时发现问题并解决问题,也使自己了解到自身的不足,所以在今后的学习中,要更加认真学习,并多与同学和老师交流,这样能够更好的学好Java。