JAVA-实验三-集合类实验

一、实验目的:

熟练掌握List,Set,Map集合的用法

二、实验内容:

1.分别输入年月日,判断这一天是这一年的第几天。

2.向List集合添加10个整数,并将集合中索引位置是7的对象从集合中删除

3.向Set集合中添加“Aaa”、“abB”、“aaA”、“BaA”、“aaA”五个元素,输出显示

4.定义一个String类型的List集合,插入五个元素,迭代器输出集合元素

5.正则表达式练习

(1)验证用户输入的是否是数字?

(2)验证用户输入的是否是合法的IP地址?

(3)验证用户输入的是否是合法的URL网址?

6.定义一个交通工具(Vehicle)的类其中有:

属性:速度(speed)体积(size)

方法:移动(move())设置速度(setSpeed(int speed))加速speedUp(),减速speedDown(). 最后在测试类Test中的main()中实例化一个交通工具对象并通过方法给它初始化speed,size的值并且通过打印出来。另外调用加速减速的方法对速度进行改变。

三、实验代码:

1.分别输入年月日,判断这一天是这一年的第几天。

package edu.hubu;
import java.util.Scanner;

public class Demo1 {
	public static void main(String[] args) {
		int result = 0;//输入天数
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入年:");
		int year = scanner.nextInt();
		System.out.print("请输入月:");
		int month = scanner.nextInt();
		System.out.print("请输入日:");
		int day = scanner.nextInt();
		switch(month) {
		//此处不设置break
		//进入switch后直接向下执行天数累加,执行完毕后跳出
		case 12 :
			result += 30;//需要加上11月的天数
		case 11:
			result += 31;//需要加上10月的天数
		case 10:
			result += 30;//需要加上9月的天数
		case 9:
			result += 31;//需要加上8月的天数
		case 8:
			result += 31;//需要加上7月的天数
		case 7:
			result += 30;//需要加上6月的天数
		case 6:
			result += 31;//需要加上5月的天数
		case 5:
			result += 30;//需要加上4月的天数
		case 4:
			result += 31;//需要加上3月的天数
		case 3:
			//2月的天数随闰年、平年有变化
			if((year % 4 == 0 && year % 100 != 0) | year % 400 == 0)
				result += 29;//闰年2月有29天
			else
				result += 28;//平年2月有28天
		case 2:
			result += 31;//需要加上1月的天数
		case 1:
			result += day;//加上日份的天数
		}
		System.out.print(year + "年" + month + "月" + day + "日共有" + result + "天!"+ "\n");
	}
}

2.向List集合添加10个整数,并将集合中索引位置是7的对象从集合中删除

package edu.hubu;
import java.util.*;

public class Demo2 {
	public static void main(String[] args) {
		//list.add(n)是向集合中末尾添加数值n
		//list.remove(i)是删除集合中索引为i-1的数
		List<Integer> list1 = new ArrayList<Integer>();
		for(int i=1;i<=10;i++)
			list1.add(i);//使用循环向list1中添加数值
		Scanner scanner = new Scanner(System.in);
		System.out.print("集合list1为:" + list1 + "\n");
		System.out.print("请输入你想删除的数字索引:");
		int i = scanner.nextInt();
		list1.remove(i-1);//删除下标为i-1的数值
		System.out.print("删除后的集合list1为:"+list1);	
	}
}

3.向Set集合添加”Aaa”、”abB”、”aaA”、”BaA”、”aaA”五个元素,输出显示

package edu.hubu;
import java.util.*;
public class Demo3 {
	public static void main(String[] args) {
		HashSet<String> Set = new HashSet<String>();//接口为HashSet
		System.out.print("未添加元素后的集合Set为:" + Set + "\n");
		Set.add("Aaa");
		Set.add("abB");
		Set.add("aaA");
		Set.add("BaA");
		Set.add("aaA");
		System.out.print("添加新元素后的集合Set为:" + Set);
	}
}

4.定义一个String类型的List集合,插入五个元素,迭代器输出集合元素

package edu.hubu;
import java.util.*;
public class Demo4 {
	public static void main(String[] args) {
	    List<String> list2 = new ArrayList<String>();
	    Scanner scanner = new Scanner(System.in);
	    System.out.print("请向集合list2中输入五个元素:"+"\n");
	    for(int i=1;i<=5;i++) {
	    	//循环输入五个元素
	    	System.out.print("请输入第"+i+"个元素:");
	    	list2.add(scanner.next());//next()默认指String类型的数值
	    }
	    //迭代器输出集合元素
	    System.out.print("迭代器输出集合list2的元素如下:"+"\n");
	    Iterator iterator = list2.iterator();
	    while(iterator.hasNext()) 
	    	System.out.print(iterator.next() + " ");//以空格作为元素间隔
	}
}

5.正则表达式练习

(1)验证用户输入的是否是数字?

(2)验证用户输入的是否是合法的IP地址?

(3)验证用户输入的是否是合法的URL网址?

package edu.hubu;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Demo5 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入字符串(数字、IP地址或URL网址):");
		String str = scanner.next();
		if(Pattern.matches("^\\-?([1-9]\\d*|0)(\\.\\d+)?$", str)) {
			System.out.print(str + "是数字!");
		}
		else
			if(Pattern.matches("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$", str))
				System.out.print(str + "是合法的IP地址!");
		else
	if(Pattern.matches("^((ht|f)tps?):\\/\\/([\\w-]+(\\.[\\w-]+)*\\/?)+(\\?([\\w\\-\\.,@?^=%&:\\/~\\+#]*)+)?$", str))
				System.out.print(str + "是合法的URL网址!");
		else
			System.out.print("您的输入有误!");
	}
}

6.定义一个交通工具(Vehicle)的类其中有:

属性:速度(speed);体积(size)

方法:移动(move()) 设置速度(setSpeed(int speed))加速speedUp()减速speedDown(). 最后在测试类Test中的main()中实例化一个交通工具对象并通过方法给它初始化speed,size的值并且通过打印出来。另外调用加速减速的方法对速度进行改变。

package edu.hubu;
import java.util.Scanner;

public class Demo6 {
	public static void main(String[] args) {
		Vehicle car = new Vehicle();
		Scanner scanner = new Scanner(System.in);
		System.out.println("正在初始化小车....");
		
		System.out.print("请输入小车的速度:");
		int i = scanner.nextInt();
		car.setSpeed(i);//对速度进行初始化,设置速度
		System.out.print("请输入小车的体积:");
		int j = scanner.nextInt();
		car.setSize(j);//对体积进行初始化,设置体积
		System.out.print("初始化成功!");
		System.out.println("小车速度为"+car.getSpeed()+",体积为"+car.getSize()+"!");
		
		car.move();//实现小车的加速、减速等移动操作
	}
}


class Vehicle{
	private int speed;//定义小车的速度
	private int size;//定义小车的体积
	
	//定义默认构造函数
	public Vehicle() {
		speed=10;
		size=30;
	}
	

	//定义速度设置函数
	public void setSpeed(int v) {
		this.speed=v;
		System.out.println("小车速度为"+this.speed);	
	}
	//定义返回速度值函数
	public int getSpeed() {
		return this.speed;
	}
	

	//定义体积设置函数
	public void setSize(int v) {
		this.size=v;
		System.out.println("小车体积为"+this.size);	
	}
	//定义返回体积值函数
	public int getSize() {
		return this.size;
	}
	

	//定义移动函数:加速,减速
	public void move() {
		Scanner scanner = new Scanner(System.in);
		int result ;
		while(true) {
			System.out.print("\n"+"请输入你想选择的操作(1.加速;2.减速;3.退出):");
			result = scanner.nextInt();
			if(result==1) {
				System.out.print("请输入小车的目标速度:");
				int i = scanner.nextInt();
				this.speedUp(i);
			}
			else if(result==2) {
				System.out.print("请输入小车的目标速度:");
				int j = scanner.nextInt();
				this.speedDown(j);
			}
			else if(result==3) {
				System.out.println("正在退出...");
				break;
			}
			else {
				System.out.println("输入错误!请重新输入...");
			}
		}
	}
	

	//定义加速函数
	public void speedUp(int v) {
		this.speed=v;
		System.out.println("小车正在加速...");
		System.out.println("加速后小车速度为"+this.speed+",体积为"+this.size);	
	}
	
	//定义减速函数
	public void speedDown(int v) {
		this.speed=v;
		System.out.println("小车正在减速...");
		System.out.println("减速后小车速度为"+this.speed+",体积为"+this.size);	
	}

}

四、实验结果:

第一问输出结果:

2020年为闰年:31+29+1=61天

2022年为平年:31+28+31+3=93天

第二问输出结果:

第三问输出结果:

第四问输出结果:

第五问输出结果:

第六问输出结果:

五、实验小结:

对于第一题,在计算天数的累加时,在程序中使用switch()结构进行设计,switch结构中设有case=12,case=11,...,case=1共12个case,每个case中都不设置break,直接向下继续执行所有的case语句,执行完毕后跳出,从而达到天数累加的效果。

对于第二题,在程序中选择接口为List,向里面放Integer类型的元素。使用list.add(n)语句实现向集合中末尾添加元素n的效果,使用list.remove(i)语句实现删除集合中索引为i-1的元素的效果。

对于第三题,在程序中选择接口为HashSet,向里面放String类型的元素。使用Set.add(n)语句实现向集合末尾添加元素n的效果。

对于第四题,在程序中选择接口为List,向里面放String类型的元素。使用迭代器完成输出,使用Iterator iterator=list.iterator()语句定义迭代器iterator,调用iterator.hasNext()方法用来判断下一个元素是否存在,调用iterator.next()方法向下移动指针,并返回指针指向的元素的值。当iterator.hasNext()为false时表明集合读取完毕,跳出while()循环。

对于第五题,用户输入数字、IP地址或URL网址,在程序中使用Pattern.matches()进行正则表达式匹配识别,识别到对应类型则输出,如未识别到则说明用户输入的非数字、IP地址与URL网址,设为输入错误。

对于第六题,设置小车类Vehicle,在类中设置默认构造函数Vehicle()、速度设置函数setSpeed()、速度返回函数getSpeed()、体积设置函数setSize()、体积返回函数getSize()、移动函数move()、加速函数speedUp()、减速函数speedDown(),并在主函数中新建Vehicle类对象car,分别调用上述函数实现对于功能。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值