周测练习

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

public class demo1 {
	public static void main(String[] args) {
		String s1 = "a-b-c-d-e-f";
		String[] s2 = s1.split("-");
		for(String s3:s2) {
			System.out.println(s3);
		}
	
		String s4 = "";
		for(int i = 0;i < s2.length;i++) {
			s4 += s2[i] + ",";
		}
		
		String s5 = s4.substring(0,s4.length()-1);
		s5 = s5.replace("c", "C");
		
		System.out.println(new StringBuffer(s5).reverse().toString());
	}
}	

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

public class Student1 {
	 String Code,Name;  
     int Age;  
     
     public Student1(String a,String b,int c){  
         Code=a;  
         Name=b;  
         Age=c;  
        }
     String getCode() {
		return Code;
	}
	
    String getName() {
		return Name;
	}
	
	
    int getAge() {
		return Age;
	}
	
	 
	void show(){  
        System.out.println("姓名:"+getName());  
        System.out.println("学号:"+getCode());   
        System.out.println("年龄:"+getAge());  
	}
}
public class Student2 {
	public static void main(String[] args){  
		Student1 A1=new Student1("01","马镭",18);  
		Student1 A2=new Student1("03","何浩",19);  
		Student1 A3=new Student1("04","王超",19);  
		Student1 A4=new Student1("02","马宋磊",16);
		
		 A1.show();  
	        System.out.println();  
	        A2.show();  
	        System.out.println();  
	        A3.show();  
	        System.out.println();  
	        A4.show();  
	        System.out.println(); 
	       
	}
	public int compare(Student o1, Student o2) {
        // 按照学生的年龄进行升序排列
        if (o1.getAge() > o2.getAge()) {
            return 1;
        }
        if (o1.getAge() == o2.getAge()) {
            return 0;
        }
        return -1;
    }
}
3、紧接第二题,用 单例设计 一个服务类,并定义一个方法,可以 随机抽取 集合中的某个学生对象,并打印输出。
package com.ntqn.test.title;

import java.util.List;

public class Service {
	private static Service service = new Service();
	
	private Service() {
		
	}
	
	public static Service getInstance() {
		return service;
	}
	
	public Student randomStu(List<Student> stus) {
		if (null != stus && !stus.isEmpty()) {
			return stus.get((int)(Math.random() * stus.size()));
		} else {
			return null;
		}
	}
}
package com.ntqn.test.title;

import java.util.ArrayList;
import java.util.List;

public class Test3 {
	public static void main(String[] args) {
		List<Student> teams = new ArrayList<Student>();
		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);
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值