java_day11练习题

java_day11练习题

练习1

一、需求说明:自定义一个学生类,给出成员变量name和age,使用Collection集合存储自定
义对象并遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。
1.2.操作步骤描述
1.创建学生类。
2.创建集合对象。
3.创建元素对象。
4.把元素添加到集合。
5.遍历集合。
package com.scy11;

public class Student {
    private String name;
    private int age;
    public Student(){}
    public Student(String name,int age){
        this.name = name;
        this.age = age;
    }

    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;
    }

}
package com.scy11;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionDemo {
    public static void main(String[] args) {
        //创建集合对象
        Collection<Student> c = new ArrayList<Student>();
        //创建元素对象
        Student s1 = new Student("林青霞",20);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",50);
        //把元素添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);
        //遍历集合
        Iterator<Student> it = c.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

练习2

一、需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并
遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。要求使用两种方式进行遍历(迭代
器、普通for)。
2.2.操作步骤描述
1.创建学生类。
2.创建集合对象。
3.创建元素对象。
4.把元素添加到集合。
5.遍历集合。
public class ListDemo {
    public static void main(String[] args) {
        //创建List集合对象
        List<Student> list = new ArrayList<Student>();
        //创建元素对象
        Student s1 = new Student("林青霞",20);
        Student s2 = new Student("张曼玉",10);
        Student s3 = new Student("王祖贤",25);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("-------------------");
        for (int i=0;i<list.size();i++){
            Student s = list.get(i);
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

练习3

一、需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并
使用增强for进行遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。
3.2.操作步骤描述
1.创建学生类。
2.创建集合对象。
3.创建元素对象。
4.把元素添加到集合。
5.遍历集合。
package com.scy11;

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

public class ForTest {
    public static void main(String[] args) {
        List<Student> studentList = new ArrayList<Student>();
        Student s1 = new Student("林青霞",30);
        Student s2 = new Student("张曼玉",36);
        Student s3 = new Student("王祖贤",32);
        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        for (Student s:studentList){
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

练习4

二、需求说明:自定义一个学生类,给出成员变量name和age,使用List集合存储自定义对象并行遍历,遍历集合的时候,在控制台输出学生对象的成员变量值。要求使用三种方式进行遍历(迭代器、普通for、增强for)。
4.2.操作步骤描述
1.创建学生类。
2.创建集合对象。
3.创建元素对象。
4.把元素添加到集合。
5.遍历集合。
package com.scy11;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListDemo {
    public static void main(String[] args) {
        //创建集合对象
        List<Student> array = new ArrayList<Student>();
        //添加元素
        Student s1 = new Student("林青霞",20);
        Student s2 = new Student("张曼玉",30);
        Student s3 = new Student("王祖贤",40);
        array.add(s1);
        array.add(s2);
        array.add(s3);
        //迭代器遍历
        Iterator<Student> it = array.iterator();
        while (it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+s.getAge());
        }
        System.out.println("------------------");
        //普通for
        for (int i=0;i<array.size();i++){
            Student s = array.get(i);
            System.out.println(s.getName()+s.getAge());
        }
        System.out.println("------------------");
        //增强for
        for (Student s:array){
            System.out.println(s.getName()+s.getAge());
        }
    }
}

练习5

一、分析以下需求,并用代码实现: 
1.按照以下描述完成类的定义。
学生类
属性:
	姓名name
	年龄age
	成绩score
行为:
	吃饭eat()
	stuty(String content)(content:表示学习的内容)
2.定义学生工具StudentsTool,有四个方法,描述如下
	public void listStudents(Student[] arr):遍历打印学生信息
	public int getMaxScore(Student[] arr):获取学生成绩的最高分
	public Student getMaxStudent(Student[] arr):获取成绩最高的学员
	public int getAverageScore(Student[] arr):获取学生成绩的平均值
	public int getCount(Student[] arr):获取不及格的学员数量
3.定义测试类TestStudentTool,在main方法中首先创建长度为5的Student数组并初始化数
据,再创建StudentsTool类的对象,并调用以上方法
package com.scy11;	//Student 类

public class Student {
    private String name;
    private int age;
    private int score;
    public Student(){}
    public Student(String name,int age,int score){
        this.name = name;
        this.age = age;
        this.score =score;
    }

    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 int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public void eat(){
        System.out.println("吃饭");
    }

    public void study(String content){
        System.out.println("学生要学习"+content);
    }

}
package com.scy11;	//工具类

public class StudentsTool {
    private StudentsTool(){}
    public static void listStudents(Student[] arr){
        for (Student s:arr){
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }

    public static int getMaxScore(Student[] arr){
        int maxScore = Integer.MIN_VALUE;
        for (Student s:arr){
            int score = s.getScore();
            if (score>maxScore){
                maxScore = score;
            }
        }
        return maxScore;
    }

    public static Student getMaxStudent(Student[] arr){
        int maxScore= getMaxScore(arr);
        for (Student s:arr){
            int score = s.getScore();
            if (maxScore==score){
                return s;
            }
        }
        return null;
    }

    public static int getAverageScore(Student[] arr){
        int sum = 0;
        for (Student s:arr){
            int score = s.getScore();
            sum +=score;
        }
        int avg = sum/(arr.length);
        return avg;
    }

    public static int getCount(Student[] arr){
        int count=0;
        for (Student s:arr){
            int score = s.getScore();
            if (score<60){
                count++;
            }
        }
        return count;
    }
}
package com.scy11;	//测试类

public class TestStudentTool {
    public static void main(String[] args) {
        Student[] std = new Student[5];
        std[0] = new Student("scy",15,80);
        std[1] = new Student("lyx",20,100);
        std[2] = new Student("www",16,50);
        std[3] = new Student("wng",17,85);
        std[4] = new Student("liu",16,58);
        StudentsTool.listStudents(std);
        int maxScore = StudentsTool.getMaxScore(std);
        System.out.println("最高分:"+maxScore);
        Student s = StudentsTool.getMaxStudent(std);
        System.out.println("最高分成员:"+s.getName());
        int avgScore = StudentsTool.getAverageScore(std);
        System.out.println("平均分:"+avgScore);
        int count = StudentsTool.getCount(std);
        System.out.println("不及格人数:"+count);
    }
}

练习6

一、分析以下需求,并用代码实现
1.定义ArrayList集合,存入多个字符串"abc" "def"  "efg" "def" "def" "qwe" "def" "def" "swd" "wwe" "def" "def"
2.使用普通for循环获取集合中索引为3的元素并打印
3.定义方法public static boolean myContains(ArrayList list,String str)
			(1)参数
				a.ArrayList list: 表示存储多个String数据的集合
				b.String str: 表示一个字符串
			(2)返回值
				true: 表示list集合中包含字符串str
				false: 表示list集合中不包含字符串str
4.利用上面定义的mycontains方法统计集合中包含字符串"def"的数量
5.删除集合中的所有字符串"def"(思路:循环判断集合中是否包含"def"字符串,包含就删除)
6.将集合中每个元素中的小写字母变成大写字母
package com.scy11;

import java.util.ArrayList;
import java.util.ListIterator;

public class ArrayTestDemo {

    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<String>();
        array.add("abc");
        array.add("def");
        array.add("efg");
        array.add("def");
        array.add("def");
        array.add("qwe");
        array.add("def");
        array.add("def");
        array.add("swd");
        array.add("wwe");
        array.add("def");
        array.add("def");
        for (int i=0;i<array.size();i++){
            if (i==3){
                System.out.println(array.get(i));
            }
        }
        int count = getStringTime(array,"def");
        System.out.println("def出现的次数为:"+count);
        toUpperCaseForList(array);
        System.out.println(array);

    }

    public static int getStringTime(ArrayList list,String str){
        int count ;
        for (count=0;myContains(list,str);count++){
            list.remove(str);
        }
        return count;
    }

    public static boolean myContains(ArrayList list,String str){
        return list.contains(str);
    }

    public static void toUpperCaseForList(ArrayList list)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++)
        {
            String s = (String)list.get(i);
            list.set(i, s.toUpperCase());
            sb.delete(0, s.length());
        }
    }
}

练习7

一、分析以下需求,并用代码实现
1.定义Student类
			属性:
				姓名:String name
				年龄:int age
				成绩:int score
			行为:
				空参构造方法
				有参构造方法
				set和get方法
				toString方法
2.定义测试类,进行测试
			(1)创建10个学生对象存入ArrayList集合中
			(2)打印最高分的学员姓名、年龄、成绩  [要求封装1个方法  参数是集合对象   返回值类型为Student]
			(3)打印10个学生的总成绩和平均分		 [要求封装两个方法完成]
			(4)打印不及格的学员信息及数量		 [要求封装一个方法完成]
package com.scy11;	//Student 类

public class Student {
    private String name;
    private int age;
    private int score;
    public Student(){}
    public Student(String name,int age,int score){
        this.name = name;
        this.age = age;
        this.score =score;
    }

    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 int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
	@Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package com.scy11;	//工具类

public class StudentsTool {
    private StudentsTool(){}
    public static void listStudents(Student[] arr){
        for (Student s:arr){
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }

    public static int getMaxScore(Student[] arr){
        int maxScore = Integer.MIN_VALUE;
        for (Student s:arr){
            int score = s.getScore();
            if (score>maxScore){
                maxScore = score;
            }
        }
        return maxScore;
    }

    public static Student getMaxStudent(Student[] arr){
        int maxScore= getMaxScore(arr);
        for (Student s:arr){
            int score = s.getScore();
            if (maxScore==score){
                return s;
            }
        }
        return null;
    }

    public static int getAverageScore(Student[] arr){
        int sum = 0;
        for (Student s:arr){
            int score = s.getScore();
            sum +=score;
        }
        int avg = sum/(arr.length);
        return avg;
    }

    public static int getCount(Student[] arr){
        int count=0;
        for (Student s:arr){
            int score = s.getScore();
            if (score<60){
                count++;
            }
        }
        return count;
    }
}
package com.scy11;	//测试类

public class TestStudentTool {
    public static void main(String[] args) {
        Student[] std = new Student[5];
        std[0] = new Student("scy",15,80);
        std[1] = new Student("lyx",20,100);
        std[2] = new Student("www",16,50);
        std[3] = new Student("wng",17,85);
        std[4] = new Student("liu",16,58);
        std[5] = new Student("eee",15,60);
        std[6] = new Student("sds",15,59);
        std[7] = new Student("aaa",15,85);
        std[8] = new Student("cca",15,87);
        std[9] = new Student("pdf",15,92);
        StudentsTool.listStudents(std);
        int maxScore = StudentsTool.getMaxScore(std);
        System.out.println("最高分:"+maxScore);
        Student s = StudentsTool.getMaxStudent(std);
        System.out.println("最高分成员:"+s.getName());
        int avgScore = StudentsTool.getAverageScore(std);
        System.out.println("平均分:"+avgScore);
        int count = StudentsTool.getCount(std);
        System.out.println("不及格人数:"+count);
    }
}

练习8

一、分析以下需求,并用代码实现
1.定义ArrayList集合,存入多个字符串
如:"ab1" "123ad"  "bca" "dadfadf"  "dddaaa"  "你好啊"  "我来啦"  "别跑啊"
2.遍历集合,删除长度大于5的字符串,打印删除后的集合对象
提示:可以将原集合中所有长度大于5的字符串放入到新集合中,遍历新集合,新集合中的元素就是要删除的元素
3.基于上一步,删除集合中元素包含0-9数字的字符串(只要字符串中包含0-9中的任意一个数字就需要删除此整个字符串)
			提示:
				(1)定义public static boolean myContains(String str)方法
					功能:
						判断str中是否包含0到9的数字
						包含:返回true
						不包含:返回false
				(2)遍历原集合利用myContains方法将所有包含0-9的字符串放入新集合中
				(3)新集合中的元素就是要删除的元素
package com.scy11;

import java.util.ArrayList;
import java.util.ListIterator;

public class ArrayTestDemo {

    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<String>();
        array.add("ab1");
        array.add("123ad");
        array.add("bca");
        array.add("dadfadf");
        array.add("dddaaa");
        array.add("你好啊");
        array.add("我来啦");
        array.add("别跑啊");
        ArrayList<String> arrayDelete = new ArrayList<String>();

        for (int i=0;i<array.size();i++){
            if (array.get(i).length()>5){
                arrayDelete.add(array.get(i));
                array.remove(i);
                i--;
            }
        }
        System.out.println(arrayDelete);
        System.out.println("---------------");
        ArrayList<String> arrayDelete2 = new ArrayList<String>();

        for (int i=0;i<array.size();i++){
            if (myContains(array.get(i))){
                arrayDelete2.add(array.get(i));
                array.remove(i);
                i--;
            }
        }
        System.out.println(arrayDelete2);

    }

    public static boolean myContains(String str){
        for (int i=0;i<10;i++){
            if (str.contains(String.valueOf(i))){
                return true;
            }
        }
        return false;
}}

练习9

一、分析以下需求,并用代码实现
定义MyArrays工具类,该工具类中有以下方法,方法描述如下:
1.public static void reverse(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象		
要求:对list集合对象中的元素进行反转(第一个和最后一个交换,第二个和倒数第二个交换,第三个和倒数第三个交换...)
2.public static Integer max(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象		
要求:求出list集合对象中的最大值并返回
3.public static Integer min(ArrayList<Integer> list);
参数ArrayList<Integer> list:要进行操作的集合对象		
要求:求出list集合对象中的最小值并返回
4.public static int indexOf(ArrayList<Integer> list,Integer i);
参数ArrayList<Integer> list:要进行操作的集合对象
参数Integer i:需要在集合中查找的元素
要求:求出元素i在list集合中第一次出现的索引,如果没有返回-1
5.public static void replaceAll(ArrayList<Integer> list,Integer oldValue,Integer newValue);
参数ArrayList<Integer> list:要进行操作的集合对象
参数Integer oldValue:需要被替换掉的原值
					参数Integer newValue:替换后的新值
	要求:将list集合中的所有值为oldValue的元素替换为newValue
package com.scy11;

import java.util.ArrayList;

public class MyArrays {
    private MyArrays(){}

    public static void reverse(ArrayList<Integer> list){
        int start = 0;
        for (int i=list.size()-1;start<i;i--){
            Integer in = (Integer) list.get(start);
            list.set(start,(Integer)list.get(i));
            list.set(i,in);
            start++;
        }
    }
    public static Integer max(ArrayList<Integer> list){
        Integer max = list.get(0);

        for (int i=0;i<list.size();i++){
            if (max.intValue() < list.get(i).intValue()){
                max = list.get(i);
            }
        }
        return max;
    }

    public static Integer min(ArrayList<Integer> list){
        Integer min = list.get(0);

        for (int i=0;i<list.size();i++){
            if (min.intValue() > list.get(i).intValue()){
                min = list.get(i);
            }
        }
        return min;
    }

    public static int indexOf(ArrayList<Integer> list,Integer i){
        for (int x=0;x<list.size();x++){
            Integer i1 = list.get(x);
            if (i1.intValue()==i.intValue()){
                return x;
            }
        }
        return -1;
    }

    public static void replaceAll(ArrayList<Integer> list,Integer oldValue,Integer newValue){
        for (int i=0;i<list.size();i++){
            Integer in = list.get(i);
            if (in==oldValue){
                list.set(i,newValue);
            }
        }
    }

}

版权声明:本文为rinima438原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。