千锋20200305

在千锋“逆战”学习第24天

      每日一句:非优秀的程序员常常把空间和时间消耗殆尽,优秀的程序员则总是有足够的空间和时间去完成编程任务,而且配合近乎完美。
      今天学习了泛型集合与Collections工具类的相关内容。
      明天继续加油。

作业

11.有如下代码:

interface IA {
	void ma();
}

class MyClass implements IA {
	public void ma() {
	}

	public String toString() {
		return "MyClass toString()";
	}
}

public class TestMyClass {
	public static void main(String[] args) {
		IA ia = new MyClass();
		System.out.println(ia);
	}
}

运行结果:

MyClass toString()

--------------------------------------------------------------------------
12.给定一个长度,随机产生一个该长度的字符串,有大小写字母数字组成 public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入字符串长度:"); int n = input.nextInt(); getString(n); }

public static void getString(int length) {
	Random r = new Random();
	StringBuilder str = new StringBuilder();
	for(int i=0;i<length;i++) {
		int a = r.nextInt(100);
		if(a>=48&&a<=57||a>=65&&a<=90||a>=97&&a<=122) {
			str.append((char)a+" ");
		}
		else {
			i--;
		}
	}
	System.out.println(str);
}

}
运行结果:

输入字符串长度:
10
8 a B c J Y N G 2 V 

--------------------------------------------------------------------------
13.给定一个字符串,判断该字符串中是否包含某个子串。如果包含,求出子串的所有出现位置。

public class Test {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个字符串");
		String fatherStr = input.next();
		System.out.println("请输入另一个字符串");
		String sonStr = input.next();
		include(fatherStr, sonStr);
	}
	
	public static void include(String str1,String str2) {
		if(str1.contains(str2)) {
			int length=0;
			System.out.println(str1+"包含"+str2);
			System.out.print(str2+"出现的位置为:");
			while(true) {
				int pos = str1.indexOf(str2);
				if(pos==-1) {
					continue;
				}
				System.out.print((pos+length)+",");
				str1 = str1.substring(pos+str2.length());
				length += pos+str2.length();
				
			}
		}else {
			System.out.println(str1+"不包含"+str2);
		}
	}
}

运行结果:

请输入一个字符串
abcd123abcd456bcd789
请输入另一个字符串
cd
abcd123abcd456bcd789包含cd
cd出现的位置为:2,9,15,

请输入一个字符串
abcdefg
请输入另一个字符串
hi
abcdefg不包含hi

--------------------------------------------------------------------------
14.有一个Student对象
      (1)计算所有学生平均年龄
      (2)计算各个班级平均分

public class Student {
	private String name;
	private int age;
	private double score;
	private String classNum;
	public Student(String name, int age, double score, String classNum) {
		this.name = name;
		this.age = age;
		this.score = score;
		this.classNum = classNum;
	}
	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 double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public String getClassNum() {
		return classNum;
	}
	public void setClassNum(String classNum) {
		this.classNum = classNum;
	}
}
public class TestMain {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("Tom", 18, 100, "class05"));
		list.add(new Student("Jerry", 22, 70, "class04"));
		list.add(new Student("Owen", 25, 90, "class05"));
		list.add(new Student("Jim", 30, 80, "class05"));
		list.add(new Student("Steve", 28, 66, "class06"));
		list.add(new Student("Kevin", 24, 100, "class04"));
		// 1.计算所有学生平均年龄
		double sum = 0;
		for (int i = 0; i < list.size(); i++) {
			sum += list.get(i).getAge();
		}
		double avgAge = sum / list.size();
		System.out.println("所有学生平均年龄" + avgAge);
		// 2.计算各个班级平均分
		int c4 = 0;// 4班人数
		double sum4 = 0;// 4班总分
		int c5 = 0;
		double sum5 = 0;
		int c6 = 0;
		double sum6 = 0;
		for(int i=0;i<list.size();i++) {
			if(list.get(i).getClassNum().equals("class04")) {
				sum4+=list.get(i).getScore();
				++c4;
			}
			if(list.get(i).getClassNum().equals("class05")) {
				sum5+=list.get(i).getScore();
				++c5;
			}
			if(list.get(i).getClassNum().equals("class06")) {
				sum6+=list.get(i).getScore();
				++c6;
			}
		}
		System.out.println("4班平均分:"+sum4/c4);
		System.out.println("5班平均分:"+sum5/c5);
		System.out.println("6班平均分:"+sum6/c6);
	}
}

运行结果:

所有学生平均年龄24.5
4班平均分:85.0
5班平均分:90.0
6班平均分:66.0
发布了40 篇原创文章 · 获赞 0 · 访问量 1146

猜你喜欢

转载自blog.csdn.net/qq_41841482/article/details/104683005
今日推荐