实验9 常用类_第1页
实验9 常用类_第2页
实验9 常用类_第3页
实验9 常用类_第4页
实验9 常用类_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验 9 常用类 一、实验目的 1掌握 String类的基本构造方法和常用方法的使用; 2熟悉 StringBuffer类的追加、插入、查找、替换、删除等操作; 3熟悉 Math类常量及常用方法的调用; 4熟悉包装类的功能、重要属性、主要构造器和常用方法; 5熟悉 Date类的主要构造方法和常用方法,明白其不足之处; 6熟悉 GregorianCalendar类的主要构造方法、常用方法,能够正确地创建日历对象、输出日期对象 对应的分量及计算两个日期的间隔; 7熟悉 SimpleDateFormat类的主要构造方法、常用方法,掌握格式化输出日期的方法与步骤; 8熟悉 API文档的使用,能够查阅包中类、接口、枚举、异常的相关信息; 9了解 StringTokenizer类的一般使用。 二、 实验内容与步骤 1运行下列程序,并回答问题: /字符串的比较 public class StringCompare public static void main(String args) String s1 = “abc“; String s2 = “abc“; String s3 = new String(“abc“); String s4 = new String(“abc“); System.out.println(“s1=s2?: “ + (s1 = s2); System.out.println(“s1=s3?: “ + (s1 = s3); System.out.println(“s3=s4?: “ + (s3 = s4); System.out.println(“s1.equals(s2)?:“ + s1.equals(s2); System.out.println(“s1.equals(s3)?:“ + s1.equals(s3); System.out.println(“s3.equals(s4)?:“ + s3.equals(s4); System.out.println(“s1.equals(s3)?:“ + pareTo(s3); 问题: (1) 对于 String 对象来说,“=”运算符与 equals( )方法的功能有什么不同? (2) s1 和 s2 是否指向同一对象?为什么? (3) s3 和 s4 是否指向同一对象?为什么? (4) s1=s3 是否成立?为什么? (5) s1、s2、s3 、s4 的内容是否相同? (5) compareTo( )方法的功能是什么?当比较结果分别为 负数、正数、0 时,代表什么含义? 2根据程序注释,将所缺代码补充完整,然后运行程序: /String类的使用 public class StringTest public static void main(String args) / 创建一个以““为参数的String对象 String str = _; System.out.println(“字符串的长度: “ + _); / 输出字符串的长度 System.out.println(“字符串的首字符: “ + _);/ 输出字符串的首字符 / 输出字符串的最后一个字符 System.out.println(“字符串的最后一个字符: “ + str.charAt(str.length() - 1); / 输出字符 的索引号(即下标) System.out.println(“字符的索引号(即下标): “ + _); / 输出最后一个点号(.)的索引号(即下标) System.out.println(“最后一个点号(.) 的索引号(即下标): “ + str.lastIndexOf(.); / 输出该邮箱的用户名(即第一个单词) System.out.println(“该邮箱的用户名(即第一个单词): “ + str.substring(0, str.indexOf(); / 输出该邮箱的顶级域名(即最后一个单词) System.out.println(“该邮箱的顶级域名(即最后一个单词): “ + _); / 字符串全部以大写方式输出 System.out.println(“字符串全部以大写方式输出为: “ + _); / 字符串全部以小写方式输出 System.out.println(“字符串全部以小写方式输出为: “ + str.toLowerCase(); (小技巧:如果某一行的内容不会填写,可用/ 将该行内容注释掉,从面不影响整个程序的运行) 3根据程序注释,将所缺代码补充完整,再运行程序: /StringBuffer的增加、删除和修改 public class StringBufferTest public static void main(String args) char ch=2,0,0,8,年; / 创建一个以 “北京奥运会开幕时间:“为参数的StringBuffer对象 StringBuffer sb=_; _; / 在sb尾部追加“8月8日晚8 点“ 字符串 sb.insert(_,_); / 在sb头部插入字符数组ch的内容 System.out.println(“字符串内容为: “+sb.toString(); int n=sb.indexOf(“北京“); sb.replace(n,_,“第29届“); / 将字符串中“北京“替换为“第29届“ System.out.println(“替换后,得到的字符串内容为: “+sb.toString(); System.out.println(“字符串的长度为: “+_);/ 输出字符串的长度 sb.delete(_,18); / 删除字符串中从第6个字符开始到第18个字符为止的内容 System.out.println(“删除字符串后,得到的字符串内容为: “+sb.toString(); 4. 运行下列程序,从中体会Math类中静态变量、静态方法的用法: /Math类的使用 public class MathTest public static void main(String args) System.out.println(Math.E); System.out.println(Math.PI); System.out.println(Math.exp(2); System.out.println(Math.random(); System.out.println(Math.sqrt(10.0); System.out.println(Math.pow(2, 3); System.out.println(Math.round(99.4); System.out.println(Math.abs(-8.88); 5(提高题)编写一个程序,从编号为1-50的学生中随机产生一等奖1名、二等奖2 名、三等奖3名,任何 一个学生不能重复中奖,抽奖顺序是:先抽三等奖,再抽二等奖,最后抽一等奖。 6编写 Calculator.java 程序,实现功能:从命令行输入两个操作数和运算符,参数格式为:操作数 1 运算符 操作数 2,其中:两个操作数为 double类型,运算符为:+、-、*、/中的一个,程序输出运算 结果。 7分析、填充、运行下列程序,并回答相关问题: /Date类的使用 import java.util.Date; public class DateTest public static void main(String args) _; / 创建一个日期对象now,以记录系统当前时间 System.out.println(“当前日期: “+_);/ 输出now 对象的内容 Date newDate=new Date(5000000); / 距离GMT 1970.1.1 0:0:0的间隔为5000秒 System.out.println(“新的日期: “+newDate); System.out.println(“当前日期早于新日期: “+now.before(newDate); System.out.println(“当前日期晚于新日期: “+now.after(newDate); System.out.println(“当前时间距离GMT 1970.1.1 00:00:00的毫秒数: “+_); 问题: (1)Date类中的时间间隔是以什么为单位来计算的? (2)Date类的 getTime()方法的功能是什么? (3)解释程序中 新日期 newDate 的输出结果。 8利用 GregorianCalendar 的 get()方法输出系统的当前时间,输出格式: 北京时间:xxxx 年 xx 月 xx 日 xx 时 xx 分 xx 秒。 9分析、运行下列程序,从中体会格式化输出日期的用法,并回答相关问题: /SimpleDateFormatTest类的使用 import java.util.*; import java.text.*; public class SimpleDateFormatTest public static void main(String args) Calendar now = new GregorianCalendar(); SimpleDateFormat formatter = new SimpleDateFormat(); formatter.applyPattern(“现在时间: yyyy年MM 月dd日HH时mm 分ss秒 E“); String str = formatter.format(now.getTime(); System.out.println(str); /伦敦奥运会的开幕时间是2012年7月27 日20时12分,即是北京时间:2012 年7月28日3时12 分 Calendar london2012 = new GregorianCalendar(2012, 6, 28, 3, 12, 0); / 得到两个时间相差的毫秒数 long distance = london2012.getTimeInMillis() - now.getTimeInMillis(); int days = (int) (distance / (24 * 60 * 60 * 1000); / 转换为天数 / 剩余的转换为“总秒数“,并考虑“四舍五入“ long totalSeconds = Math .round(distance % (24 * 60 * 60 * 1000) / 1000.0 + 0.5); int hh = (int) (totalSeconds / (60 * 60); / 转换成小时数 int mm = (int) (totalSeconds % (60 * 60) / 60); / 转换成分钟数 int ss = (int) (totalSeconds % (60 * 60) % 60); / 转换成秒钟数 System.out.println(“距离2012 年伦敦奥运会开幕式还有:“ + days + “天“ + hh + “时“ + mm + “分“ + ss + “秒“); 问题: (1)SimpleDateFormat类的功能是什么? (2)Sim

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论