上机实践 7 字符串 、时间与数字 请注意:此次实验请注意使用 Java API 官方文档,链接: Java TM Platform Standard Edition 6 中文版: http://tool.oschina.net/apidocs/apidoc?api=jdk-zh Oracle 官方 Java TM Platform Standard Edition 9 英文版(最新): https://docs.oracle.com/javase/9/docs/api/ 实验 1 String 类的常用方法 模板代码 StringExample.java class StringExample { public static void main(String args[]) { String s1=new String("you are a student"), s2=new String("how are you"); if( 【代码 1 】 ) // 使用 equals 方法判断 s1 与 s2 是否相同 { System.out.println("s1 与 s2 相同 "); } else { System.out.println("s1 与 s2 不相同 "); } String s3=new String("22030219851022024"); if( 【代码 2 】 ) // 判断 s3 的前缀是否是“ 220302 ”。 { System.out.println(" 吉林省的身份证 "); } String s4=new String(" 你 "), s5=new String(" 我 "); if( 【代码 3 】 )// 按着字典序 s4 大于 s5 的表达式。 { System.out.println(" 按字典序 s4 大于 s5"); } else { System.out.println(" 按字典序 s4 小于 s5"); } int position=0; String path="c:\\java\\jsp\\A.java"; position= 【代码 5 】 // 获取 path 中最后出现目录分隔符号的位置 System.out.println("c:\\java\\jsp\\A.java 中最后出现 \\ 的位置 :"+position); String fileName= 【代码 6 】 // 获取 path 中“ A.java ”子字符串。 System.out.println("c:\\java\\jsp\\A.java 中含有的文件名 :"+fileName); String s6=new String("100"), s7=new String("123.678"); int n1= 【代码 7 】 // 将 s6 转化成 int 型数据。 double n2= 【代码 8 】 // 将 s7 转化成 double 型数据。 double m=n1+n2; System.out.println(m); String s8= 【代码 9 】 //String 调用 valuOf(int n) 方法将 m 转化为字符串对象 position=s8.indexOf("."); String temp=s8.substring(position+1); System.out.println(" 数字 "+m+" 有 "+temp.length()+" 位小数 ") ; String s9=new String("ABCDEF"); char a[]= 【代码 10 】 // 将 s8 存放到数组 a 中。 for(int i=a.length-1;i>=0;i--) { System.out.print(" "+a[i]); } } } 请将代码补充完整,并将运行结果截图写入实验报告。 实验 2 比较日期的大小 模板代码 DateExample import java.util.*; import javax.swing.JOptionPane; public class DateExample { public static void main(String args[ ]) { String str=JOptionPane.showInputDialog(" 输入第一个日期的年份 :"); int yearOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(" 输入该年的月份 :"); int monthOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(" 输入该月份的日期 :"); int dayOne=Integer.parseInt(str); str=JOptionPane.showInputDialog(" 输入第二个日期的年份 :"); int yearTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(" 输入该年的月份 :"); int monthTwo=Integer.parseInt(str); str=JOptionPane.showInputDialog(" 输入该月份的日期 :"); int dayTwo=Integer.parseInt(str); Calendar calendar= 【代码 1 】 // 初始化日历对象 【代码 2 】 // 将 calendar 的时间设置为 yearOne 年 monthOne 月 dayOne 日 long timeOne= 【代码 3 】 //calendar 表示的时间转换成毫秒 【代码 4 】 // 将 calendar 的时间设置为 yearTwo 年 monthTwo 月 dayTwo 日 long timeTwo= 【代码 5 】 //calendar 表示的时间转换成毫秒。 Date date1= 【代码 6 】 // 用 timeOne 做参数构造 date1 Date date2= 【代码 7 】 // 用 timeTwo 做参数构造 date2 if(date2.equals(date1)) { System.out.println(" 两个日期的年、月、日完全相同 "); } else if(date2.after(date1)) { System.out.println(" 您输入的第二个日期大于第一个日期 "); } else if(date2.before(date1)) { System.out.println(" 您输入的第二个日期小于第一个日期 "); } long days= 【代码 8 】 // 计算两个日期相隔天数 System.out.println(yearOne+" 年 "+monthOne+" 月 "+dayOne+" 日和 " +yearTwo+" 年 "+monthTwo+" 月 "+dayTwo+" 相隔 "+days+" 天 "); } } 请将代码补充完整,并将运行结果截图写入实验报告。 实验 3 处理大整数 模板代码 BigintegerExample import java.math.*; class BigIntegerExample { public static void main(String args[]) { BigInteger n1=new BigInteger("987654321987654321987654321"), n2=new BigInteger("123456789123456789123456789"), result=null; result= 【代码1】//n1和n2做加法运算 System.out.println(" 和:"+result.toString()); result= 【代码2】//n1和n2做减法运算 System.out.println(" 差:"+result.toString()); result= 【代码3】//n1和n2做乘法运算 System.out.println(" 积:"+result.toString()); result= 【代码4】//n1和n2做除法运算 System.out.println(" 商:"+result.toString()); BigInteger m=new BigInteger("1968957"), COUNT=new BigInteger("0"), ONE=new BigInteger("1"), TWO=new BigInteger("2"); System.out.println(m.toString()+" 的因子有:"); for(BigInteger i=TWO;i.compareTo(m)<0;i=i.add(ONE)) { if((n1.remainder(i).compareTo(BigInteger.ZERO))==0) { COUNT=COUNT.add(ONE); System.out.print(" "+i.toString()); } } System.out.println(""); System.out.println(m.toString()+" 一共有"+COUNT.toString()+"个因子"); } } 请将代码补充完整,并将运行结果截图写入实验报告。 练习 1 教材 220 页,第 4 题第 (3) 小题,请在实验报告中描述程序思路,并展示源代码和运行结果。 练习 2 教材 220 页,第 4 题第 (6) 小题,请在实验报告中描述程序思路,并展示源代码和运行结果。