王之泰/王志成《面向对象程序设计(java)》第十一周学习总结
第一部分:理论知识学习部分
第十一章理论知识主要为集合类的介绍,在实验中都有所体现且本周主要复习回顾上周的泛型程序设计
第二部分:实验部分 ——集合
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://en.wikipedia.org/wiki/pair_programming
3.对于结对编程中代码设计规范的要求参考:
以下实验,就让我们来体验一下结对编程的魅力。
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的用途。在实验方面通过结对编程,在小伙伴的帮助下认识了自己的不足,提升了自己的阅读代码的能力。
上一篇: 统计信息搜集禁用/启用
下一篇: 简介make命令和makefile文件