101班机试题

1.将字符串「a-b-c-d-e-f」按 「-」 切割,找到 「c」字符, 替换为大写,然后倒序输出 「f-e-d-C-b-a」

package demo03;
/**
 * 将字符串「a-b-c-d-e-f」按 「-」 切割,找到 「c」字符,
 * 替换为大写,然后倒序输出 「f-e-d-C-b-a」
 * @author 26966
 *
 */
public class Test1 {
	public static void main(String[] args) {
		//定义初始字符串
		String s="a-b-c-d-e-f";
		//将字符串转换为数组
		String[] str=s.split("-");
		//遍历数组
		for(int i=0;i<str.length;i++) {
		//找到"c"转换为大写
		if(str[i].equals("c")) {
			str[i]=str[i].toUpperCase();
		 }	
		}
		//定义空字符串temp,利用反向遍历和累加生成反转字符串。
		String temp = "";
		for (int i = str.length - 1; i >= 0; i--) {
			temp += str[i] + "-";
		}
		// 利用substring方法来输出字符串
		System.out.println(temp.substring(0, temp.length()-1));
	}
} //运行结果:f-e-d-C-b-a

2. 定义学生类(包含学号、姓名、年龄),将你所在小组组员添加到一个集合中,并按学号排序后输出。   

package demo03;
/**
 * 定义学生类(包含学号、姓名、年龄),将你所在小组组员添加到一个集合
 * 中,并按学号排序后输出
 * @author 26966
 *
 */
public class Student implements Comparable<Student> {
  //定义Student属性学号,姓名,年龄
  private int code;
  private String name;
  private int age;
  //封装
public int getCode() {
	return code;
}
public void setCode(int code) {
	this.code = code;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
//建立有参构造器
public Student(int code, String name, int age) {
	super();
	this.code = code;
	this.name = name;
	this.age = age;
}
//重写比较方法
  public int compareTo(Student o) {
	  int flag = 0;
	  if(this.code>o.code) {
		  flag=-1;
	  }else if(this.code==o.code) {
		  flag=0;
	  }else if(this.code<o.code) {
		  flag=1;
	  }
	  return flag;
  }
//重写toString方法
@Override
public String toString() {
	return "Student [code=" + code + ", name=" + name + ", age=" + age + "]";
}  
}

建立测试类

package demo03;
/**
 * 建立测试类,将小组组员添加到一个集合
 * 中,并按学号排序后输出
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Test2 {
	public static void main(String[] args) {
		//new出集合
		List<Student> java103=new ArrayList<Student>();
		//new出学生对象
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		//把学生对象放入集合
		java103.add(stu1);
		java103.add(stu2);
		java103.add(stu3);
		java103.add(stu4);
		// 按学号倒叙排列
		Collections.sort(java103);
     	// 遍历学生,输出结果
		for (Student stu : java103) {
					System.out.println(stu);
		}
	}
}//运行结果:Student [code=4, name=May, age=12]
            Student [code=3, name=Jack, age=13]
            Student [code=2, name=Helen, age=18]
            Student [code=1, name=Tom, age=15]                        

3.紧接第二题,用单例设计一个服务类,并定义一个方法,可以随机抽取集合中的某个学生对象,并打印输出。

单例服务类

package demo03;
import java.util.List;
/**
 * 单例服务类,里面有随机选择方法
 * @author 26966
 *
 */
public class Service {
	//new出单例对象
private static Service service = new Service();
	//构造器
	private Service() {
		
	}
	//getInstance方法,以便获得实例
	public static Service getInstance() {
		return service;
	}
	//建立随机获得集合属性的方法
	public Student randomStu(List<Student> stus) {
		//非空判断
		if (null != stus && !stus.isEmpty()) {
			//Math.random() * stus.size()获得随机数
			return stus.get((int)(Math.random() * stus.size()));
		} else {
			return null;
		}
	}
}

测试类,实现随机选取

package demo03;
/**
 * 主类用来随机选选取学生
 */
import java.util.ArrayList;
import java.util.List;
public class Test3 {
	public static void main(String[] args) {
		//建立学生集合
		List<Student> teams = new ArrayList<Student>();
		//new出学生对象
		Student stu1 = new Student(1, "Tom", 15);
		Student stu2 = new Student(3, "Jack", 13);
		Student stu3 = new Student(2, "Helen", 18);
		Student stu4 = new Student(4, "May", 12);
		//将对象插入集合
		teams.add(stu1);
		teams.add(stu2);
		teams.add(stu3);
		teams.add(stu4);
     	// 获取单例对象
		Service service = Service.getInstance();
		System.out.println("==========随机抽取学生=============");
		//调用随机选取方法
		Student randomStu = service.randomStu(teams);
		System.out.println(randomStu);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值