王之泰/王志成《面向对象程序设计(java)》第十…
2018-11-12 06:52:36来源:博客园 阅读 ()
第一部分:理论知识学习部分
第十一章理论知识主要为集合类的介绍,在实验中都有所体现且本周主要复习回顾上周的泛型程序设计
第二部分:实验部分 ——集合
1、实验目的与要求
(1) 掌握Vetor、Stack、Hashtable三个类的用途及常用API;
(2) 了解java集合框架体系组成;
(3) 掌握ArrayList、LinkList两个类的用途及常用API。
(4) 了解HashSet类、TreeSet类的用途及常用API。
(5)了解HashMap、TreeMap两个类的用途及常用API;
(6) 结对编程(Pair programming)练习,体验程序开发中的两人合作。
2、实验内容和步骤
实验1: 导入第9章示例程序,测试程序并进行代码注释。
测试程序1:
1.使用JDK命令运行编辑、运行以下三个示例程序,结合运行结果理解程序;
2.掌握Vetor、Stack、Hashtable三个类的用途及常用API。
1 //示例程序1 2 import java.util.Vector; 3 4 class Cat { 5 private int catNumber; 6 7 Cat(int i) { 8 catNumber = i; 9 } 10 11 void print() { 12 System.out.println("Cat #" + catNumber); 13 } 14 } 15 16 class Dog { 17 private int dogNumber; 18 19 Dog(int i) { 20 dogNumber = i; 21 } 22 23 void print() { 24 System.out.println("Dog #" + dogNumber); 25 } 26 } 27 28 public class CatsAndDogs { 29 public static void main(String[] args) { 30 Vector cats = new Vector(); 31 for (int i = 0; i < 7; i++) 32 cats.addElement(new Cat(i)); 33 cats.addElement(new Dog(7)); 34 for (int i = 0; i < cats.size(); i++) 35 ((Cat) cats.elementAt(i)).print(); 36 } 37 }
结果如下:
处理后:
1 package shi_li; 2 3 import java.util.Vector; 4 5 class Cat { 6 private int catNumber; 7 8 Cat(int i) { 9 catNumber = i; 10 } 11 12 void print() { 13 System.out.println("Cat #" + catNumber); 14 } 15 } 16 17 class Dog { 18 private int dogNumber; 19 20 Dog(int i) { 21 dogNumber = i; 22 } 23 24 void print() { 25 System.out.println("Dog #" + dogNumber); 26 } 27 } 28 29 public class CatsAndDogs { 30 public static void main(String[] args) { 31 Vector cats = new Vector(); 32 for (int i = 0; i < 7; i++) 33 cats.addElement(new Cat(i)); 34 cats.addElement(new Dog(7)); 35 for (int i = 0; i <cats.size(); i++) { 36 if(cats.elementAt(i) instanceof Cat) { 37 ((Cat) cats.elementAt(i)).print(); 38 } 39 else { 40 ((Dog) cats.elementAt(i)).print(); 41 } 42 43 } 44 } 45 }
1 //示例程序2 2 import java.util.*; 3 4 public class Stacks { 5 static String[] months = { "1", "2", "3", "4" }; 6 7 public static void main(String[] args) { 8 Stack stk = new Stack(); 9 for (int i = 0; i < months.length; i++) 10 stk.push(months[i]); 11 System.out.println(stk); 12 System.out.println("element 2=" + stk.elementAt(2)); 13 while (!stk.empty()) 14 System.out.println(stk.pop()); 15 } 16 }
1 //示例程序3 2 import java.util.*; 3 4 class Counter { 5 int i = 1; 6 7 public String toString() { 8 return Integer.toString(i); 9 } 10 } 11 12 public class Statistics { 13 public static void main(String[] args) { 14 Hashtable ht = new Hashtable(); 15 for (int i = 0; i < 10000; i++) { 16 Integer r = new Integer((int) (Math.random() * 20)); 17 if (ht.containsKey(r)) 18 ((Counter) ht.get(r)).i++; 19 else 20 ht.put(r, new Counter()); 21 } 22 System.out.println(ht); 23 } 24 }
测试程序2:
1.使用JDK命令编辑运行ArrayListDemo和LinkedListDemo两个程序,结合程序运行结果理解程序;
1 import java.util.*; 2 3 public class ArrayListDemo { 4 public static void main(String[] argv) { 5 ArrayList al = new ArrayList(); 6 // Add lots of elements to the ArrayList... 7 al.add(new Integer(11)); 8 al.add(new Integer(12)); 9 al.add(new Integer(13)); 10 al.add(new String("hello")); 11 // First print them out using a for loop. 12 System.out.println("Retrieving by index:"); 13 for (int i = 0; i < al.size(); i++) { 14 System.out.println("Element " + i + " = " + al.get(i)); 15 } 16 } 17 }
1 import java.util.*; 2 public class LinkedListDemo { 3 public static void main(String[] argv) { 4 LinkedList l = new LinkedList(); 5 l.add(new Object()); 6 l.add("Hello"); 7 l.add("zhangsan"); 8 ListIterator li = l.listIterator(0); 9 while (li.hasNext()) 10 System.out.println(li.next()); 11 if (l.indexOf("Hello") < 0) 12 System.err.println("Lookup does not work"); 13 else 14 System.err.println("Lookup works"); 15 } 16 }
2.在Elipse环境下编辑运行调试教材360页程序9-1,结合程序运行结果理解程序;
1 package linkedList; 2 3 import java.util.*; 4 5 /** 6 * This program demonstrates operations on linked lists. 7 * @version 1.11 2012-01-26 8 * @author Cay Horstmann 9 */ 10 public class LinkedListTest 11 { 12 public static void main(String[] args) 13 { 14 List<String> a = new LinkedList<>(); 15 a.add("Amy"); 16 a.add("Carl"); 17 a.add("Erica"); 18 19 List<String> b = new LinkedList<>(); 20 b.add("Bob"); 21 b.add("Doug"); 22 b.add("Frances"); 23 b.add("Gloria"); 24 25 // 将单词从B合并为A 26 27 ListIterator<String> aIter = a.listIterator(); 28 Iterator<String> bIter = b.iterator(); 29 30 while (bIter.hasNext()) 31 { 32 if (aIter.hasNext()) aIter.next(); 33 aIter.add(bIter.next()); 34 } 35 36 System.out.println(a); 37 38 // 从B中删除每个第二个单词 39 40 bIter = b.iterator(); 41 while (bIter.hasNext()) 42 { 43 bIter.next(); // 跳过一个元素 44 if (bIter.hasNext()) 45 { 46 bIter.next(); // 跳过下一个元素 47 bIter.remove(); // 删除该元素 48 } 49 } 50 51 System.out.println(b); 52 53 // 批量操作:从A中删除B中的所有单词 54 55 a.removeAll(b); 56 57 System.out.println(a); 58 } 59 }
3.掌握ArrayList、LinkList两个类的用途及常用API。
测试程序3:
1.运行SetDemo程序,结合运行结果理解程序;
1 import java.util.*; 2 public class SetDemo { 3 public static void main(String[] argv) { 4 HashSet h = new HashSet(); //也可以 Set h=new HashSet() 5 h.add("One"); 6 h.add("Two"); 7 h.add("One"); // DUPLICATE 8 h.add("Three"); 9 Iterator it = h.iterator(); 10 while (it.hasNext()) { 11 System.out.println(it.next()); 12 } 13 } 14 }
2.在Elipse环境下调试教材365页程序9-2,结合运行结果理解程序;了解HashSet类的用途及常用API。
1 package set; 2 3 import java.util.*; 4 5 /** 6 * This program uses a set to print all unique words in System.in. 7 * @version 1.12 2015-06-21 8 * @author Cay Horstmann 9 */ 10 public class SetTest 11 { 12 public static void main(String[] args) 13 { 14 Set<String> words = new HashSet<>(); // SETHASSET实现 15 long totalTime = 0; 16 17 try (Scanner in = new Scanner(System.in)) 18 { 19 while (in.hasNext()) 20 { 21 String word = in.next(); 22 long callTime = System.currentTimeMillis(); 23 words.add(word); 24 callTime = System.currentTimeMillis() - callTime; 25 totalTime += callTime; 26 } 27 } 28 29 Iterator<String> iter = words.iterator(); 30 for (int i = 1; i <= 20 && iter.hasNext(); i++) 31 System.out.println(iter.next()); 32 System.out.println(". . ."); 33 System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds."); 34 } 35 }
3.在Elipse环境下调试教材367页-368程序9-3、9-4,结合程序运行结果理解程序;了解TreeSet类的用途及常用API。
1 package treeSet; 2 3 import java.util.*; 4 5 /** 6 * An item with a description and a part number. 7 */ 8 public class Item implements Comparable<Item> 9 { 10 private String description; 11 private int partNumber; 12 13 /** 14 * Constructs an item. 15 * 16 * @param aDescription 17 * the item's description 18 * @param aPartNumber 19 * the item's part number 20 */ 21 public Item(String aDescription, int aPartNumber) 22 { 23 description = aDescription; 24 partNumber = aPartNumber; 25 } 26 27 /** 28 * Gets the description of this item. 29 * 30 * @return the description 31 */ 32 public String getDescription() 33 { 34 return description; 35 } 36 37 public String toString() 38 { 39 return "[description=" + description + ", partNumber=" + partNumber + "]"; 40 } 41 42 public boolean equals(Object otherObject) 43 { 44 if (this == otherObject) return true; 45 if (otherObject == null) return false; 46 if (getClass() != otherObject.getClass()) return false; 47 Item other = (Item) otherObject; 48 return Objects.equals(description, other.description) && partNumber == other.partNumber; 49 } 50 51 public int hashCode() 52 { 53 return Objects.hash(description, partNumber); 54 } 55 56 public int compareTo(Item other) 57 { 58 int diff = Integer.compare(partNumber, other.partNumber); 59 return diff != 0 ? diff : description.compareTo(other.description); 60 } 61 }
1 package treeSet; 2 3 import java.util.*; 4 5 /** 6 * This program sorts a set of item by comparing their descriptions. 7 * @version 1.12 2015-06-21 8 * @author Cay Horstmann 9 */ 10 public class TreeSetTest 11 { 12 public static void main(String[] args) 13 { 14 SortedSet<Item> parts = new TreeSet<>(); 15 parts.add(new Item("Toaster", 1234)); 16 parts.add(new Item("Widget", 4562)); 17 parts.add(new Item("Modem", 9912)); 18 System.out.println(parts); 19 20 NavigableSet<Item> sortByDescription = new TreeSet<>( 21 Comparator.comparing(Item::getDescription)); 22 23 sortByDescription.addAll(parts); 24 System.out.println(sortByDescription); 25 } 26 }
测试程序4:
1.使用JDK命令运行HashMapDemo程序,结合程序运行结果理解程序;
1 import java.util.*; 2 public class HashMapDemo { 3 public static void main(String[] argv) { 4 HashMap h = new HashMap(); 5 // The hash maps from company name to address. 6 h.put("Adobe", "Mountain View, CA"); 7 h.put("IBM", "White Plains, NY"); 8 h.put("Sun", "Mountain View, CA"); 9 String queryString = "Adobe"; 10 String resultString = (String)h.get(queryString); 11 System.out.println("They are located in: " + resultString); 12 } 13 }
2.在Elipse环境下调试教材373页程序9-6,结合程序运行结果理解程序;
1 package map; 2 3 /** 4 * A minimalist employee class for testing purposes. 5 */ 6 public class Employee 7 { 8 private String name; 9 private double salary; 10 11 /** 12 * Constructs an employee with $0 salary. 13 * @param n the employee name 14 */ 15 public Employee(String name) 16 { 17 this.name = name; 18 salary = 0; 19 } 20 21 public String toString() 22 { 23 return "[name=" + name + ", salary=" + salary + "]"; 24 } 25 }
1 package map; 2 3 import java.util.*; 4 5 /** 6 * This program demonstrates the use of a map with key type String and value type Employee. 7 * @version 1.12 2015-06-21 8 * @author Cay Horstmann 9 */ 10 public class MapTest 11 { 12 public static void main(String[] args) 13 { 14 Map<String, Employee> staff = new HashMap<>(); 15 staff.put("144-25-5464", new Employee("Amy Lee")); 16 staff.put("567-24-2546", new Employee("Harry Hacker")); 17 staff.put("157-62-7935", new Employee("Gary Cooper")); 18 staff.put("456-62-5527", new Employee("Francesca Cruz")); 19 20 // print all entries 21 22 System.out.println(staff); 23 24 // remove an entry 25 26 staff.remove("567-24-2546"); 27 28 // replace an entry 29 30 staff.put("456-62-5527", new Employee("Francesca Miller")); 31 32 // look up a value 33 34 System.out.println(staff.get("157-62-7935")); 35 36 // iterate through all entries 37 38 staff.forEach((k, v) -> 39 System.out.println("key=" + k + ", value=" + v)); 40 } 41 }
3.了解HashMap、TreeMap两个类的用途及常用API。
实验2:结对编程练习:
1.关于结对编程:以下图片是一个结对编程场景:两位学习伙伴坐在一起,面对着同一台显示器,使用着同一键盘,同一个鼠标,他们一起思考问题,一起分析问题,一起编写程序。
2.关于结对编程的阐述可参见以下链接:
http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html
http://en.wikipedia.org/wiki/Pair_programming
3.对于结对编程中代码设计规范的要求参考:
http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html
以下实验,就让我们来体验一下结对编程的魅力。
1.确定本次实验结对编程合作伙伴;
我的小伙伴为:王志成
2.各自运行合作伙伴实验九编程练习1,结合使用体验对所运行程序提出完善建议;
3.各自运行合作伙伴实验十编程练习2,结合使用体验对所运行程序提出完善建议;
程序互测概述:
我和小伙伴互相测试了对方的实验九编程练习1程序,小伙伴的程序基本要求都能达到,就是在文件的读取上面还有些欠缺,但是在后面的共同学习中他很好的改了过来。实验十编程练习2中基本功能要求也同样能实现,只是在除法上面有点缺陷没有很好地实现实数运算。
程序互测心得:
通过本次和小伙伴的程序互测体验,其好处在于帮助别人发现问题的同时还可反思自己的程序,认识自己的不足。而且很有效的提升了自己阅读代码的能力。
4.采用结对编程方式,与学习伙伴合作完成实验九编程练习1;
结对编程代码;
1 package jiedui_bianchen; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.util.ArrayList; 10 import java.util.Scanner; 11 import java.util.Collections; 12 13 public class ID { 14 15 public static People findPeopleByname(String name) { 16 People flag = null; 17 for (People people : peoplelist) { 18 if(people.getName().equals(name)) { 19 flag = people; 20 } 21 } 22 return flag; 23 24 } 25 26 public static People findPeopleByid(String id) { 27 People flag = null; 28 for (People people : peoplelist) { 29 if(people.getnumber().equals(id)) { 30 flag = people; 31 } 32 } 33 return flag; 34 35 } 36 37 private static ArrayList<People> agenear(int yourage) { 38 // TODO Auto-generated method stub 39 int j=0,min=53,d_value=0,k = 0; 40 ArrayList<People> plist = new ArrayList<People>(); 41 for (int i = 0; i < peoplelist.size(); i++) { 42 d_value = peoplelist.get(i).getage() > yourage ? 43 peoplelist.get(i).getage() - yourage : yourage - peoplelist.get(i).getage() ; 44 k = d_value < min ? i : k; 45 min = d_value < min ? d_value : min; 46 } 47 for(People people : peoplelist) { 48 if(people.getage() == peoplelist.get(k).getage()) { 49 plist.add(people); 50 } 51 } 52 return plist; 53 } 54 55 private static ArrayList<People> peoplelist; 56 57 public static void main(String[] args) //throws IOException 58 { 59 peoplelist = new ArrayList<People>(); 60 Scanner scanner = new Scanner(System.in); 61 File file = new File("D:\\身份证号.txt"); 62 try { 63 FileInputStream files = new FileInputStream(file); 64 BufferedReader in = new BufferedReader(new InputStreamReader(files)); 65 String temp = null; 66 while ((temp = in.readLine()) != null) { 67 68 String[] information = temp.split("[ ]+"); 69 People people = new People(); 70 people.setName(information[0]); 71 people.setnumber(information[1]); 72 int A = Integer.parseInt(information[3]); 73 people.setage(A); 74 people.setsex(information[2]); 75 for(int j = 4; j<information.length;j++) { 76 people.setplace(information[j]); 77 } 78 peoplelist.add(people); 79 80 } 81 } catch (FileNotFoundException e) { 82 System.out.println("文件未找到"); 83 e.printStackTrace(); 84 } catch (IOException e) { 85 System.out.println("文件读取错误"); 86 e.printStackTrace(); 87 } 88 boolean isTrue = true; 89 while (isTrue) { 90 91 System.out.println("******************************************"); 92 System.out.println(" 1.按姓名典序输出人员信息"); 93 System.out.println(" 2.查询最大年龄人员信息"); 94 System.out.println(" 3.查询最小年龄人员信息"); 95 System.out.println(" 4.输入你的年龄,查询身份证号.txt中年龄与你最近的人"); 96 System.out.println(" 5.查询人员中是否有你的同乡"); 97 System.out.println(" 6.退出"); 98 System.out.println("******************************************"); 99 int nextInt = scanner.nextInt(); 100 switch (nextInt) { 101 case 1: 102 Collections.sort(peoplelist); 103 System.out.println(peoplelist.toString()); 104 break; 105 case 2: 106 int max=0; 107 int j,k1 = 0; 108 for(int i=1;i<peoplelist.size();i++) 109 { 110 j = peoplelist.get(i).getage(); 111 if(j>max) 112 { 113 max = j; 114 k1 = i; 115 } 116 117 } 118 System.out.println("年龄最大:"+peoplelist.get(k1)); 119 break; 120 case 3: 121 int min = 100; 122 int j1,k2 = 0; 123 for(int i=1;i<peoplelist.size();i++) 124 { 125 j1 = peoplelist.get(i).getage(); 126 if(j1<min) 127 { 128 min = j1; 129 k2 = i; 130 } 131 132 } 133 System.out.println("年龄最小:"+peoplelist.get(k2)); 134 break; 135 case 4: 136 System.out.println("年龄:"); 137 int input_age = scanner.nextInt(); 138 ArrayList<People> plist = new ArrayList<People>(); 139 plist = agenear(input_age); 140 for(People people : plist) { 141 System.out.println(people.toString()); 142 } 143 break; 144 case 5: 145 System.out.println("请输入省份"); 146 String find = scanner.next(); 147 for (int i = 0; i <peoplelist.size(); i++) 148 { 149 String [] place = peoplelist.get(i).getplace().split("\t"); 150 for(String temp : place) { 151 if(find.equals(temp)) { 152 System.out.println("你的同乡是 "+peoplelist.get(i)); 153 break; 154 } 155 } 156 157 } 158 break; 159 case 6: 160 isTrue = false; 161 System.out.println("byebye!"); 162 break; 163 default: 164 System.out.println("输入有误"); 165 } 166 } 167 } 168 169 }
结对程序运行功能界面截图;
结对过程描述,提供两人在讨论、细化和编程时的结对照片(非摆拍)。
5.采用结对编程方式,与学习伙伴合作完成实验十编程练习2。
结对编程代码;
1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.PrintWriter; 4 import java.math.BigDecimal; 5 import java.util.Scanner; 6 7 8 public class ss { 9 public static void main(String[] args) { 10 11 12 Scanner in = new Scanner(System.in); 13 Calculator<Integer> sf = new Calculator<Integer>(); 14 File file = new File("wzt.txt"); 15 if(file.exists()) { 16 System.out.println("文件已存在"); 17 } 18 PrintWriter output = null; 19 try { 20 output = new PrintWriter(new FileOutputStream(file)); 21 } catch (Exception e) { 22 //e.printStackTrace(); 23 } 24 int sum = 0; 25 26 System.out.println("计算结果保留两位小数"); 27 for (int i = 1; i < 11; i++) { 28 int a = (int) Math.round(Math.random() * 100); 29 int b = (int) Math.round(Math.random() * 100); 30 int s = (int) Math.round(Math.random() * 3); 31 32 33 switch(s) 34 { 35 case 1: 36 System.out.println(i+": "+a+"/"+b+"="); 37 Number c = in.nextDouble(); 38 output.println(a+"/"+b+"="+c); 39 Number g = sf.division(a, b); 40 BigDecimal division = new BigDecimal(g.doubleValue()); 41 g = division.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 42 if (c.equals(g)) { 43 sum += 10; 44 System.out.println("恭喜答案正确"); 45 } 46 else { 47 System.out.println("抱歉,答案错误"); 48 } 49 50 break; 51 52 case 2: 53 System.out.println(i+": "+a+"*"+b+"="); 54 Number c1 = in.nextDouble(); 55 output.println(a+"*"+b+"="+c1); 56 Number g1 = sf.mulitiplication(a, b); 57 BigDecimal mul = new BigDecimal(g1.doubleValue()); 58 g1 = mul.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 59 if (c1.equals(g1) ){ 60 sum += 10; 61 System.out.println("恭喜答案正确"); 62 } 63 else { 64 System.out.println("抱歉,答案错误"); 65 } 66 break; 67 case 3: 68 System.out.println(i+": "+a+"+"+b+"="); 69 Number c2 = in.nextDouble(); 70 output.println(a+"+"+b+"="+c2); 71 Number g2 =sf.addition(a, b); 72 BigDecimal add = new BigDecimal(g2.doubleValue()); 73 g2 = add.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 74 if (c2.equals(g2)) { 75 sum += 10; 76 System.out.println("恭喜答案正确"); 77 } 78 else { 79 System.out.println("抱歉,答案错误"); 80 } 81 82 break ; 83 case 4: 84 System.out.println(i+": "+a+"-"+b+"="); 85 Number c3 = in.nextDouble(); 86 output.println(a+"-"+b+"="+c3); 87 Number g3 = sf.subtraction(a, b); 88 BigDecimal sub = new BigDecimal(g3.doubleValue()); 89 g3 = sub.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 90 if (c3.equals(g3)) { 91 sum += 10; 92 System.out.println("恭喜答案正确"); 93 } 94 else { 95 System.out.println("抱歉,答案错误"); 96 } 97 break ; 98 99 } 100 101 } 102 System.out.println("成绩"+sum); 103 output.println("成绩:"+sum); 104 output.close(); 105 in.close(); 106 107 } 108 }
结对程序运行功能界面截图;
结对过程描述,提供两人在讨论、细化和编程时的结对照片(非摆拍)。
第三部分:总结
在本周的学习过程中,复习了上周内容即泛型程序设计的知识,学习了新的关于集合类的知识,理解了一些常用API的用途。在实验方面通过结对编程,在小伙伴的帮助下认识了自己的不足,提升了自己的阅读代码的能力。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:java----类拾遗
- 王之泰201771010131《面向对象程序设计(java)》第九周学习 2018-10-29
- 王之泰201771010131《面向对象程序设计(java)》第六周学习 2018-10-06
- 王之泰201771010131《面向对象程序设计(java)》第二周学习 2018-09-10
- 王之泰201771010131《面向对象程序设计(java)》第一周学习 2018-09-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash