Java 机试题目

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

[html]  view plain  copy
  1. package demo03;  
  2. /**  
  3.  * 将字符串「a-b-c-d-e-f」按 「-」 切割,找到 「c」字符,  
  4.  * 替换为大写,然后倒序输出 「f-e-d-C-b-a」  
  5.  * @author 26966  
  6.  *  
  7.  */  
  8. public class Test1 {  
  9.     public static void main(String[] args) {  
  10.         //定义初始字符串  
  11.         String s="a-b-c-d-e-f";  
  12.         //将字符串转换为数组  
  13.         String[] str=s.split("-");  
  14.         //遍历数组  
  15.         for(int i=0;i<str.length;i++) {  
  16.         //找到"c"转换为大写  
  17.         if(str[i].equals("c")) {  
  18.             str[i]=str[i].toUpperCase();  
  19.          }    
  20.         }  
  21.         //定义空字符串temp,利用反向遍历和累加生成反转字符串。  
  22.         String temp = "";  
  23.         for (int i = str.length - 1; i >= 0; i--) {  
  24.             temp += str[i] + "-";  
  25.         }  
  26.         // 利用substring方法来输出字符串  
  27.         System.out.println(temp.substring(0, temp.length()-1));  
  28.     }  
  29. } //运行结果:f-e-d-C-b-a  

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

[html]  view plain  copy
  1. package demo03;  
  2. /**  
  3.  * 定义学生类(包含学号、姓名、年龄),将你所在小组组员添加到一个集合  
  4.  * 中,并按学号排序后输出  
  5.  * @author 26966  
  6.  *  
  7.  */  
  8. public class Student implements Comparable<Student> {  
  9.   //定义Student属性学号,姓名,年龄  
  10.   private int code;  
  11.   private String name;  
  12.   private int age;  
  13.   //封装  
  14. public int getCode() {  
  15.     return code;  
  16. }  
  17. public void setCode(int code) {  
  18.     this.code = code;  
  19. }  
  20. public String getName() {  
  21.     return name;  
  22. }  
  23. public void setName(String name) {  
  24.     this.name = name;  
  25. }  
  26. public int getAge() {  
  27.     return age;  
  28. }  
  29. public void setAge(int age) {  
  30.     this.age = age;  
  31. }  
  32. //建立有参构造器  
  33. public Student(int code, String name, int age) {  
  34.     super();  
  35.     this.code = code;  
  36.     this.name = name;  
  37.     this.age = age;  
  38. }  
  39. //重写比较方法  
  40.   public int compareTo(Student o) {  
  41.       int flag = 0;  
  42.       if(this.code>o.code) {  
  43.           flag=-1;  
  44.       }else if(this.code==o.code) {  
  45.           flag=0;  
  46.       }else if(this.code<o.code) {  
  47.           flag=1;  
  48.       }  
  49.       return flag;  
  50.   }  
  51. //重写toString方法  
  52. @Override  
  53. public String toString() {  
  54.     return "Student [code=" + code + "name=" + name + "age=" + age + "]";  
  55. }    
  56. }  

建立测试类

[html]  view plain  copy
  1. package demo03;  
  2. /**  
  3.  * 建立测试类,将小组组员添加到一个集合  
  4.  * 中,并按学号排序后输出  
  5.  */  
  6. import java.util.ArrayList;  
  7. import java.util.Collections;  
  8. import java.util.List;  
  9. public class Test2 {  
  10.     public static void main(String[] args) {  
  11.         //new出集合  
  12.         List<Student> java103=new ArrayList<Student>();  
  13.         //new出学生对象  
  14.         Student stu1 = new Student(1, "Tom", 15);  
  15.         Student stu2 = new Student(3, "Jack", 13);  
  16.         Student stu3 = new Student(2, "Helen", 18);  
  17.         Student stu4 = new Student(4, "May", 12);  
  18.         //把学生对象放入集合  
  19.         java103.add(stu1);  
  20.         java103.add(stu2);  
  21.         java103.add(stu3);  
  22.         java103.add(stu4);  
  23.         // 按学号倒叙排列  
  24.         Collections.sort(java103);  
  25.         // 遍历学生,输出结果  
  26.         for (Student stu : java103) {  
  27.                     System.out.println(stu);  
  28.         }  
  29.     }  
  30. }//运行结果:Student [code=4name=Mayage=12]  
  31.             Student [code=3name=Jackage=13]  
  32.             Student [code=2name=Helenage=18]  
  33.             Student [code=1name=Tomage=15]                          

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

单例服务类

[html]  view plain  copy
  1. package demo03;  
  2. import java.util.List;  
  3. /**  
  4.  * 单例服务类,里面有随机选择方法  
  5.  * @author 26966  
  6.  *  
  7.  */  
  8. public class Service {  
  9.     //new出单例对象  
  10. private static Service service = new Service();  
  11.     //构造器  
  12.     private Service() {  
  13.           
  14.     }  
  15.     //getInstance方法,以便获得实例  
  16.     public static Service getInstance() {  
  17.         return service;  
  18.     }  
  19.     //建立随机获得集合属性的方法  
  20.     public Student randomStu(List<Student> stus) {  
  21.         //非空判断  
  22.         if (null != stus && !stus.isEmpty()) {  
  23.             //Math.random() * stus.size()获得随机数  
  24.             return stus.get((int)(Math.random() * stus.size()));  
  25.         } else {  
  26.             return null;  
  27.         }  
  28.     }  
  29. }  

测试类,实现随机选取

[html]  view plain  copy
  1. package demo03;  
  2. /**  
  3.  * 主类用来随机选选取学生  
  4.  */  
  5. import java.util.ArrayList;  
  6. import java.util.List;  
  7. public class Test3 {  
  8.     public static void main(String[] args) {  
  9.         //建立学生集合  
  10.         List<Student> teams = new ArrayList<Student>();  
  11.         //new出学生对象  
  12.         Student stu1 = new Student(1, "Tom", 15);  
  13.         Student stu2 = new Student(3, "Jack", 13);  
  14.         Student stu3 = new Student(2, "Helen", 18);  
  15.         Student stu4 = new Student(4, "May", 12);  
  16.         //将对象插入集合  
  17.         teams.add(stu1);  
  18.         teams.add(stu2);  
  19.         teams.add(stu3);  
  20.         teams.add(stu4);  
  21.         // 获取单例对象  
  22.         Service service = Service.getInstance();  
  23.         System.out.println("==========随机抽取学生=============");  
  24.         //调用随机选取方法  
  25.         Student randomStu = service.randomStu(teams);  
  26.         System.out.println(randomStu);  
  27.     }  
  28. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值