EXP3
计划学时: 2 | ||
实验类型: 1.演示性□ 2.验证性□ 3.综合性□ 4.设计性□ 5.其它□ | ||
授课日期: 年 月 日 | 第 周 星期 第 节 | |
实验目的 1. 验证集合的使用 2. 学习集合的应用技术 实验要求 1. 掌握List的使用 2. 掌握Set的使用 3. 掌握Map的使用 4. 理解集合应用技术 实验内容与步骤 1. 集合基本操作 (1)编写存入集合的对象类 class Person { String id; String name; Person(String id,String name) { this.id=id; this.name=name; } public void sleep() { System.out.println("I am Person,I am sleeping"); } public void eat() { System.out.println("I am Person,I am eating"); } } class Student extends Person { String sno; Student(String id,String name,String sno) { super(id,name); this.sno=sno; } public void study() { System.out.println("I am Student,I am studying"); } public void eat() { System.out.println("I am Student"); System.out.println("id:"+id); System.out.println("name:"+name); System.out.println("sno:"+sno); System.out.println("eating .........."); } public String toString(){ return "Student "+"ID:"+this.id +" Name:"+this.name +" SNo:"+this.sno; } } class Teacher extends Person { String tid; Teacher(String id,String name,String tid) { super(id,name); this.tid=tid; } public void tech() { System.out.println("I am Student,I am taching"); } public void eat() { System.out.println("I am Teacher"); System.out.println("id:"+id); System.out.println("name:"+name); System.out.println("tid:"+tid); System.out.println("eating .........."); } public String toString(){ return "Teacher "+"ID:"+this.id +" Name:"+this.name +" Tid:"+this.tid; } } (2)创建测试类,完成以下操作 a.创建3个Student的对象,2个Teacher对象 b.创建一个List和一个Set c.将a中创建的5个对象加入到List中 d.将a中创建的5个对象加入到Set中 e.编写一个static方法,可以对b中中创始的集合进行遍历,将集合中每个元素读出来转型为Person对象,并调用eat()方法 public static void travel(Collection collection){ Iterator it = collection.iterator(); } f.尝试删除List和Set中的元素,并在删除前后都调用e中编写的方法对List,Set进行遍历. HashMap h.美麟编写一个static 方法,实现对g中创建Map进行遍历,并调用study()方法 public static void travel(Map map){ } i.编写代码测试Set中不能加入重复元素 package Exercise1; import java.util.*; public class Persontest { public static void main(String[] args) { Student stu1=new Student("100", "张三", "2015"); Student stu2=new Student("101", "李四", "2016"); Student stu3=new Student("102", "王五", "2017"); Teacher tea1=new Teacher("001", "李响", "0123"); List list =new ArrayList(); list.add(stu1); Student student =((0); student.eat(); } } 2. 集合应用 完成以下代码的阅读,并编写测试类完成此类的测试 public class Student { private String id; private String name; private String address; public Student(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public boolean equals(Object arg0) { return super.equals(arg0); } @Override public int hashCode() { return super.hashCode(); 2021立秋 } @Override public String toString() { return super.toString(); } } import java.util.ArrayList; import java.util.HashMap; import java.util.List; 泉州旅游景点大全import java.util.Map; public class ClassDB { private Map<String, List<Student>> container; public ClassDB() { this.container = new HashMap<String, List<Student>>(); } //添加组 public void addGroup(String groupNmae){ if(container.containsKey(groupNmae)){ System.out.println("此组已存在"); return; } container.put(groupNmae, new ArrayList<Student>()); } public void showGroupInfo(String groupName){ } //显示所有组信息,可调用showGroupInfo(String groupName) public void showAllGroupInfo(){ } //将指定学生加入到指定组 public void addStudentToGroup(String groupName, Student stu){ } //显示指定组中的学生信息 public void showStudentOfGroup(String groupName){ } //显示所有学生信息 public void showAllStudents(){ } //通过id查学生 public Student findStudentById(String id){ return null; } //通过id删除学生 public Student deleteStudentById(String id){ return null; } //通过id修改学生信息,修改地址 public boolean updateStudent(String id){ return true; } //通过组名删除组 public boolean deleteGroupByName(String groupName){ return 电脑升级true; } } public class ClassDBDemo { private ClassDB db; public ClassDBDemo(){ db=new ClassDB(); } public static void main(String[] args) { ClassDBDemo app = new ClassDBDemo(); EXIT_FLAG:while (true) { app.showMenu(); int cmd = InputInt(); switch (cmd) { case 0: break EXIT_FLAG; case 1: app.addGroup(); app.pause(); break; case 2: app.addStudent(); app.pause(); break; case 3: app.showStudentOfGroup(); app.pause(); break; case 4: app.showAllStudnet(); app.pause(); 北京十大律师事务所 break; case 5: app.findStudent(); app.pause(); break; case 6: app.delStudent(); app.pause(); break; } } } public void delStudent(){ pr("Delete Id"); int id=getInputInt(); Student s=db.deleteStudentById(id); prln(s==null?"没有到":"删除了"+s); } public void findStudent(){ pr("Found Id"); int id=getInputInt(); Student s=db.findStudentById(id); prln(s==null?"没有到":"到了"+s); } public void showStudentOfGroup(){ pr("GroupName:"); String gn=getInputStr(); db.showStudentsOfGroup(gn); } public void showAllStudnet(){ db.showAllStudents(); } public void addStudent(){ pr("Student_id:"); int id=getInputInt(); pr("Studetn_Name:"); String name=getInputStr(); pr("GroupName:"); String groupName=getInputStr(); db.addStudentToGroup(new Student(id, name), groupName); } public void addGroup(){ pr("GroupName:"); String groupName=getInputStr(); db.addGroup(groupName); } public void showMenu() { prln("+========================================+"); prln("| 1.Add Group |"); prln("| 2.Add Student to Group |"); prln("| 3.Show Students of Group |"); prln("| 4.Show All Students |"); prln("| 5.Find Student |"); prln("| 6.Delete Students |"); prln("| 0.Exit |"); prln("+========================================+"); prln("请你选择(1,2,3,4,5,6,0):"); } private void prln(String msg) { System.out.println(msg); } private void pr(String msg) { System.out.print(msg); } public void pause() { System.out.println("Press any key to continue."); InputStreamReader isr=new InputStreamReader(System.in); try { ad(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public String getInputStr() { Scanner scr = new Scanner(System.in); return (); } public int getInputInt() { Scanner scr = new Scanner(System.in); return Int(); } } | ||
怎么唱歌好听 |
发布评论