实验四 Java实用类及接口实验

实验四 Java实用类及接口实验
一、实验学时
4学时
二、实验目的
(一)熟练掌握字符串的合并、拆开等处理操作;
(二)掌握在不同的日期格式之间熟练地实现转换;
(三)熟练掌握集合中元素的增删改查操作;
(四)熟练其它实用类例如Math类和Random类的使用。

三、预习要求
了解字符串类及其使用方法、了解有哪些时间及日期处理类及其所属的方法、了解集合的概念。
四、实验内容
(一)运行下列程序打印其输出结果,掌握字符串如何截取和定位。
class SubStringExample{
public static void main(String[] args) {
String s=“hello Java语言”;
int n1=s.indexOf(‘a’);
int n2=s.indexOf(“a语”);
System.out.println(“n1=”+n1+" n2=“+n2);
char c=s.charAt(2);
String s1=s.substring(6,10);
String s2=s.substring(4,7);
System.out.println(“c=”+c+” s1=“+s1+” s2="+s2);
}
}

(二)使用String类的public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。

(三)编写一个程序,打印系统当前日期并以不同格式化输出。(掌握Date类和SimpleDateFormat类的使用)

(四)补充下列程序,使其完整并运行。
package sample;
import java.util.*;
public class ArrayListTest {

public static void main(String[] args) {

List l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(4));
l.add(new Integer(3));
l.add(new Integer(2));

Iterator it = l.iterator();
//完善你的代码                
  //完善你的代码                
  System.out.println("Element in list is: " + i);
}

}
}
使用泛型改进上面代码并打印结果,理解使用泛型带来的好处

(五)修改下列程序:(1) 使用泛型 (2)使数据倒序排列

import java.util.;
public class TreeSetTest {
public static void main(String[] args) {
Set c = new TreeSet(new MyComparator());
c.add(new Integer(2)); c.add(new Integer(4)); c.add(new Integer(1));
c.add(new Integer(5)); c.add(new Integer(3)); c.add(new Integer(4));
Iterator it = c.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
int j1 = ((Integer)o1).intValue();
int j2 = ((Integer)o2).intValue();
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
(六)补充下列程序,使其完整并运行。
import java.util.
;
public class HashMapExample{
public static void main(String[] args){
//创建了HashMap对象
HashMap hm=new HashMap();
//向HashMap对象中添加内容不同的键值对
hm.put(1,“A”);
hm.put(3,“B”);
hm.put(4,“C”);
hm.put(2,“D”);
hm.put(5,“E”);
System.out.println("添加元素后的结果为: ");
System.out.println(hm);
//移除了HashMap对象中键为3的值

	//替换键值4对应的值为F
	                            
	//打印输出HashMap中的内容
	System.out.print("删除和替换元素后结果为:"
	System.out.println(hm);
	//取出键2对应的值
	                      
	String s=(String)o;
	System.out.println("键2对应的值为:"+s);
	}

}
Map类中数据的存放和List类中数据的存放有什么区别?

(七)补充下列程序,使其完整并运行。
public class GenericMe {
//添加代码段处
}

public static void main(String args[]) {
	// 创建不同类型的数组,Integer和String类型
	Integer[] intArray = { 1, 2, 3, 4, 5 };
	String[] stringArray = { "one", "two", "three", "four", "five" };

	System.out.println("整型数组元素为:");
	genericMethods(intArray); // 输出整型数组

	System.out.println("\n字符串型数组元素为:");
	genericMethods(stringArray); // 输出字符串型数组
}

}
程序输出结果:
整型数组元素为:
1,2,3,4,5,
字符串型数组元素为:
one,two,three,four,five,

(八)随机产生一个包含10个整数的数组,并求其中的最大值,最小值,以及平均值。(使用Random和Math类中的方法编写程序)

五、实验注意事项
(一)集合转换成任何其他的对象数组,但是不能直接把集合转换成基本数据类型的数组,因为集合只能存放对象;
(二)注意如果在不同的对象上用错方法的话,可能会产生异常;
(三)注意Random包含在java.util包中,程序需要引入该包。
六、思考题
(一) 分别使用String类和StringBuffer类创建两个字符串对象,并将其内容输出打印。
(二)哪些接口存放的数据是有序的?
(三)哪些接口存放的数据可以重复?
(四)下列程序段执行后的结果是( )。
String s=new String(“abcdefg”);
for(int i=0;i<s.length();i+=3){
System.out.print(s.charAt(i));
}
A. adg B. ACEG C. abcdefg D. abcd

(五)下面的程序段执行后输出的结果。
StringBuffer buf=new StringBuffer(“helloqhd”);
buf.insert(5,“!”);
System.out.println(buf.toString());
输出结果: 。
一、实验学时
4学时
二、实验目的
(一)熟练掌握字符串的合并、拆开等处理操作;
(二)掌握在不同的日期格式之间熟练地实现转换;
(三)熟练掌握集合中元素的增删改查操作;
(四)熟练其它实用类例如Math类和Random类的使用。

三、预习要求
了解字符串类及其使用方法、了解有哪些时间及日期处理类及其所属的方法、了解集合的概念。
四、实验内容
(一)运行下列程序打印其输出结果,掌握字符串如何截取和定位。
class SubStringExample{
public static void main(String[] args) {
String s=“hello Java语言”;
int n1=s.indexOf(‘a’);
int n2=s.indexOf(“a语”);
System.out.println(“n1=”+n1+" n2=“+n2);
char c=s.charAt(2);
String s1=s.substring(6,10);
String s2=s.substring(4,7);
System.out.println(“c=”+c+” s1=“+s1+” s2="+s2);
}
}

(二)使用String类的public String concat(String str)方法可以把调用该方法的字符串与参数指定的字符串连接,把str指定的串连接到当前串的尾部获得一个新的串。编写一个程序通过连接两个串得到一个新串,并输出这个新串。

(三)编写一个程序,打印系统当前日期并以不同格式化输出。(掌握Date类和SimpleDateFormat类的使用)

(四)补充下列程序,使其完整并运行。
package sample;
import java.util.*;
public class ArrayListTest {

public static void main(String[] args) {

List l = new ArrayList();
l.add(new Integer(1));
l.add(new Integer(4));
l.add(new Integer(3));
l.add(new Integer(2));

Iterator it = l.iterator();
//完善你的代码                
  //完善你的代码                
  System.out.println("Element in list is: " + i);
}

}
}
使用泛型改进上面代码并打印结果,理解使用泛型带来的好处

(五)修改下列程序:(1) 使用泛型 (2)使数据倒序排列

import java.util.;
public class TreeSetTest {
public static void main(String[] args) {
Set c = new TreeSet(new MyComparator());
c.add(new Integer(2)); c.add(new Integer(4)); c.add(new Integer(1));
c.add(new Integer(5)); c.add(new Integer(3)); c.add(new Integer(4));
Iterator it = c.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
int j1 = ((Integer)o1).intValue();
int j2 = ((Integer)o2).intValue();
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
(六)补充下列程序,使其完整并运行。
import java.util.
;
public class HashMapExample{
public static void main(String[] args){
//创建了HashMap对象
HashMap hm=new HashMap();
//向HashMap对象中添加内容不同的键值对
hm.put(1,“A”);
hm.put(3,“B”);
hm.put(4,“C”);
hm.put(2,“D”);
hm.put(5,“E”);
System.out.println("添加元素后的结果为: ");
System.out.println(hm);
//移除了HashMap对象中键为3的值

	//替换键值4对应的值为F
	                            
	//打印输出HashMap中的内容
	System.out.print("删除和替换元素后结果为:"
	System.out.println(hm);
	//取出键2对应的值
	                      
	String s=(String)o;
	System.out.println("键2对应的值为:"+s);
	}

}
Map类中数据的存放和List类中数据的存放有什么区别?

(七)补充下列程序,使其完整并运行。
public class GenericMe {
//添加代码段处
}

public static void main(String args[]) {
	// 创建不同类型的数组,Integer和String类型
	Integer[] intArray = { 1, 2, 3, 4, 5 };
	String[] stringArray = { "one", "two", "three", "four", "five" };

	System.out.println("整型数组元素为:");
	genericMethods(intArray); // 输出整型数组

	System.out.println("\n字符串型数组元素为:");
	genericMethods(stringArray); // 输出字符串型数组
}

}
程序输出结果:
整型数组元素为:
1,2,3,4,5,
字符串型数组元素为:
one,two,three,four,five,

(八)随机产生一个包含10个整数的数组,并求其中的最大值,最小值,以及平均值。(使用Random和Math类中的方法编写程序)

五、实验注意事项
(一)集合转换成任何其他的对象数组,但是不能直接把集合转换成基本数据类型的数组,因为集合只能存放对象;
(二)注意如果在不同的对象上用错方法的话,可能会产生异常;
(三)注意Random包含在java.util包中,程序需要引入该包。
六、思考题
(一) 分别使用String类和StringBuffer类创建两个字符串对象,并将其内容输出打印。
(二)哪些接口存放的数据是有序的?
(三)哪些接口存放的数据可以重复?
(四)下列程序段执行后的结果是( )。
String s=new String(“abcdefg”);
for(int i=0;i<s.length();i+=3){
System.out.print(s.charAt(i));
}
A. adg B. ACEG C. abcdefg D. abcd

(五)下面的程序段执行后输出的结果。
StringBuffer buf=new StringBuffer(“helloqhd”);
buf.insert(5,“!”);
System.out.println(buf.toString());
输出结果: 。

  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁的BUG最难改

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值