学士后Java集合框架和泛型习题答案.doc
文本预览下载声明
package entity;
//学生类,属性为:姓名、性别、分数、年龄、班级
public class Student {
private String name;
private String gender;
private int score;
private int age;
private String classes;
public Student(String name,String gender, int score,String
classes) {
this.name = name;
this.gender = gender;
this.score = score;
this.classes = classes;
}
public Student(String name, String gender, int age, int score)
{
this.name = name;
this.gender = gender;
this.age = age;
this.score = score;
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public Student(String name) {
this.name = name;
}
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
protected int getAge() {
return age;
}
protected void setAge(int age) {
this.age = age;
}
protected String getGender() {
return gender;
}
protected void setGender(String gender) {
this.gender = gender;
}
protected int getScore() {
return score;
}
protected void setScore(int score) {
this.score = score;
}
protected String getClasses() {
return classes;
}
protected void setClasses(String classes) {
this.classes = classes;
}
}
第二题答案源码及运行效果截图:
package entity;
import java.util.HashMap;
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
HashMapString,Student students=new HashMapString,Student();
Scanner input=new Scanner(System.in);
Student stu1=new Student(杨过, 95);
Student stu2=new Student(小龙女, 99);
Student stu3=new Student(张无忌, 88);
Student stu4=new Student(赵敏, 90);
students.put(杨过, stu1);
students.put(小龙女, stu2);
students.put(张无忌, stu3);
students.put(赵敏, stu4);
System.out.println(请输入学生姓名:);
String key=input.next();
if(students.containsKey(key)){
Student stu= students.get(key);
System.out.println(stu.getName()+的成绩是:+stu.getScore());
}
System.out.println(----------所有学生的成绩是-----------);
f
显示全部