杨玲 徐思 《面向对象程序设计(java)》第十一周学习总结

《面向对象程序设计(java)》第十一周学习总结html

第一部分:理论知识学习部分java

一、通常将数据结构分为两大类:线性数据结构和非线性数据结构。线性数据结构:线性表、栈、队列、串、数组和文件。非线性数据结构:树和图。二、线性表按其存储结构可分为顺序表和链表;用顺序存储结构存储的线性表称为顺序表;顺序表将线性表中的数据元素依次存放在某个存储区域中。一维数组就是用顺序方式存储的线性表。用链式存储结构存储的线性表称为链表。三、栈(Stack)也是一种特殊的线性表,是一种后进先出 (LIFO)的结构。栈是限定仅在表尾进行插入和删除运算的线性表,表尾称为栈顶(top),表头称为栈底(bottom)。栈的物理存储能够用顺序存储结构,也能够用链式存储结构。四、队列(Queue)是限定全部的插入只能在表的一端进行 ,而全部的删除都在表的另外一端进行的线性表。表中容许插入的一端称为队尾(Rear),容许删除的一端称为队头(Front)。队列的操做是按先进先出(FIFO)的原则进行的。队列的物理存储能够用顺序存储结构,也能够用链式存储结构。算法

五、散列表又称为哈希表。散列表算法的基本思想是:以结点的关键字为自变量,经过必定的函数关系(散列函数)计算出对应的函数值,以这个值做为该结点存储在散列表中的地址。当散列表中的元素存放太满,就必须进行再散列,将产生一个新的散列表,全部元素存放到新的散列表中,原先的散列表将被删除。在Java语言中,经过负载因子(load factor)来决定什么时候对散列表进行再散列。负载因子越高 (越接近1.0),内存的使用效率越高,元素的寻找时间越长。负载因子越低 (越接近0.0),元素的寻找时间越短,内存浪费越多。HashSet类的缺省负载因子是0.75。
六、JAVA的集合框架实现对各类数据结构的封装,以下降对数据管理与处理的难度。所谓框架就是一个类库的集合,框架中包含不少超类,编程者建立这些超类的子类可较方便的设计设计程序所需的类。
七、集合框架:JAVA集合类库的统一架构。
八、集合类的做用:Java的集合类提供了一些基本数据结构的支持。
九、集合类的使用: Java的集合类包含在java.util包中。
十、集合类的特色一:只容纳对象。注意:数组能够容纳基本数据类型数据和对象。若是集合类中想使用基本数据类型,又想利用集合类的灵活性,能够把基本数据类型数据封装成该数据类型的包装器对象,而后放入集合中处理。
              特色二:集合类容纳的对象都是Object类的实例,一旦把一个对象置入集合类中,它的类信息将丢失,这样设计的目的是为了集合类的通用性。由于Object类是全部类的祖先,因此能够在这些集合中存听任 何类的对象而不受限制,但在使用集合成员以前必须对它从新造型。
十一、Vector类相似长度可变的数组。Vector中只能存放对象。Vector的元素经过下标进行访问。 Vector类关键属性:capacity表示集合最多能容纳的元素个数。capacityIncrement表示每次增长多少容量。size表示集合当前元素个数。
十二、Stack类是Vector的子类。Stack类描述堆栈数据结构,即LIFO。
1三、Hashtable经过键来查找元素。Hashtable用散列码(hashcode)来肯定键。全部对象都有一个散列码,能够经过Object类的hashCode()方法得到。
1四、集合框架中的基本接口:Collection:集合层次中的根接口,JDK未提供这个接口的直接实现类。
1五、Set:不能包含重复的元素。对象可能不是按存放的次序存放,也就是说不能像数组同样按索引的方式进行访问,SortedSet是一个按照升序排列元素的Set。
List:是一个有序的集合,能够包含重复的元素。提供了按索引访问的方式。
Map:包含了key-value对。Map不能包含重复的key。
SortedMap是一个按照升序排列key的Map。
Collection接口:构造类集框架的基础。
1六、List的明显特征是它的元素都有一个肯定的顺序。实现它的类有ArrayList和LinkedList。ArrayList中的元素在内存中是顺序存储的。LinkedList中的元素在内存中是以链表方式存储的。
ArrayList:能够将其看做是可以自动增加容量的数组。利用ArrayList的toArray()返回一个数组。Arrays.asList()返回一个列表。LinkedList是采用双向循环链表实现的。利用LinkedList实现栈(stack)、队列(queue)、双向队列 (double-ended queue )。ArrayList底层采用数组完成,而LinkedList则是以通常的双向链表(double-linked list)完成,其内每一个对象除了数据自己外,还有两个引用,分别指向前一个元素和后一个元素。若是常常在 List 中进行插入和删除操做,应该使用LinkedList,不然,使用ArrayList将更加快速。
Set中的元素必须惟一。添加到Set中的对象元素必须定义equals方法,以提供算法来判断欲添加进来的对象是否与已经存在的某对象相等,从而创建对象的惟一性。实现Set接口的类有HashSet,TreeSet。

1七、TreeSet是一个有序集合,TreeSet中元素将按照升序排列,缺省是按照天然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。能够在构造TreeSet对象时,传递实现了 Comparator接口的比较器对象。HashSet是基于Hash算法实现的,其性能一般都优于TreeSet。一般使用HashSet,须要排序的功能时,使用TreeSet。
Map接口用来维持不少“键-值”对,以便经过键来查找相应的值。HashMap基于散列表实现(替代Hashtable)。TreeMap在一个二叉树的基础上实现。
映射(map)是一个存储关键字和值的关联或关键字/值对的对象。给定一个关键字,能够获得它的值。关键字和值都是对象。关键字必须是惟一的。但值是能够被复制的。
Map接口映射惟一关键字到值。关键字(key)是之后用于检索值的对象。给定一个关键字和一个值,能够存储这个值到一个Map对象中。当这个值被存储之后,就可使用它的关键字来检索它
Map循环使用两个基本操做:get( )和put( )。使用 put( )方法能够将一个指定了关键字和值的值加入映射。为了获得值,能够经过将关键字做为参数来调用 get( )方法。调用返回该值。
Map接口的实现类主要有HashMap,TreeMap,Hashtable,Properties。HashMap对key进行散列。TreeMap按照key进行排序。和Set相似,HashMap的速度一般都比TreeMap快,只有在须要排序的功能的时候,才使用TreeMap。

第二部分:实验部分编程

1.实验名称:实验十一   集合数组

2.  实验目的:数据结构

(1) 掌握Vetor、Stack、Hashtable三个类的用途及经常使用API;架构

(2) 了解java集合框架体系组成;框架

(3) 掌握ArrayList、LinkList两个类的用途及经常使用API。dom

(4) 了解HashSet类、TreeSet类的用途及经常使用API。函数

(5)了解HashMap、TreeMap两个类的用途及经常使用API;

(6) 结对编程(Pair programming)练习,体验程序开发中的两人合做。

3. 实验步骤与内容:

实验1: 导入第9章示例程序,测试程序并进行代码注释。

测试程序1:

l 使用JDK命令运行编辑、运行如下三个示例程序,结合运行结果理解程序;

l 掌握Vetor、Stack、Hashtable三个类的用途及经常使用API。 

 

注意:示例程序1出现强制类型转换异常:

代码修改以下:

方法1、

 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 Cat(7)); 34         for (int i = 0; i < cats.size(); i++) 35  ((Cat) cats.elementAt(i)).print(); 36  } 37 }

运行结果以下:

方法2、

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         
32         for (int i = 0; i < 7; i++) 33             cats.addElement(new Cat(i)); 34         cats.addElement(new Dog(7)); 35         
36         for(int i = 0;i < cats.size(); i++) { 37 if(cats.elementAt(i) instanceof Cat) { 38 39 ((Cat) cats.elementAt(i)).print(); 40 41 }else { 42 43 ((Dog) cats.elementAt(i)).print(); 44 45 } 46 } 47             
48         /*for (int i = 0; i < cats.size(); i++) 49 ((Cat) cats.elementAt(i)).print(); 50 */
51 } 52 }

 运行结果以下:

 

 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));//随机生成一个【0,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:

l 使用JDK命令编辑运行ArrayListDemo和LinkedListDemo两个程序,结合程序运行结果理解程序;

 

 1 import java.util.*;  2 
 3  
 4 
 5 public class ArrayListDemo {  6 
 7 public static void main(String[] argv) {  8 
 9 ArrayList al = new ArrayList(); 10 
11 // Add lots of elements to the ArrayList...
12 
13 al.add(new Integer(11)); 14 
15 al.add(new Integer(12)); 16 
17 al.add(new Integer(13)); 18 
19 al.add(new String("hello")); 20 
21 // First print them out using a for loop.
22 
23 System.out.println("Retrieving by index:"); 24 
25 for (int i = 0; i < al.size(); i++) { 26 
27 System.out.println("Element " + i + " = " + al.get(i)); 28 
29 } 30 
31 } 32 
33 }

 运行结果以下:

 

 1 import java.util.*;  2 
 3 public class LinkedListDemo {  4 
 5     public static void main(String[] argv) {  6 
 7         LinkedList l = new LinkedList();  8 
 9         l.add(new Object()); 10 
11         l.add("Hello"); 12 
13         l.add("zhangsan"); 14 
15         ListIterator li = l.listIterator(0); 16 
17         while (li.hasNext()) 18 
19  System.out.println(li.next()); 20 
21         if (l.indexOf("Hello") < 0) 22 
23             System.err.println("Lookup does not work"); 24 
25         else
26 
27             System.err.println("Lookup works"); 28 
29  } 30 
31 }

 运行结果以下:

 

l 在Elipse环境下编辑运行调试教材360页程序9-1,结合程序运行结果理解程序;

 l 掌握ArrayList、LinkList两个类的用途及经常使用API。

 

 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       // merge the words from b into 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       // remove every second word from b
39 
40       bIter = b.iterator(); 41       while (bIter.hasNext()) 42  { 43          bIter.next(); // skip one element
44          if (bIter.hasNext()) 45  { 46             bIter.next(); // skip next element
47             bIter.remove(); // remove that element
48  } 49  } 50 
51  System.out.println(b); 52 
53       // bulk operation: remove all words in b from a
54 
55  a.removeAll(b); 56 
57  System.out.println(a); 58  } 59 }

 运行结果以下:

 

 测试程序3:

l 运行SetDemo程序,结合运行结果理解程序;

 

 1 package linkedList;  2 import java.util.*;  3 
 4 public class SetDemo {  5 
 6     public static void main(String[] argv) {  7 
 8         HashSet h = new HashSet(); //也能够 Set h=new HashSet()
 9 
10         h.add("One"); 11 
12         h.add("Two"); 13 
14         h.add("One"); // DUPLICATE
15 
16         h.add("Three"); 17 
18         Iterator it = h.iterator(); 19 
20         while (it.hasNext()) { 21 
22  System.out.println(it.next()); 23 
24  } 25 
26  } 27 
28 }

 运行结果以下:

 

 

l 在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<>(); // HashSet implements Set
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 }

 运行结果以下:

 

l 在Elipse环境下调试教材367页-368程序9-三、9-4,结合程序运行结果理解程序;了解TreeSet类的用途及经常使用API。

 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 }

 运行结果以下:

 

package treeSet; import java.util.*; /** * An item with a description and a part number. */
public class Item implements Comparable<Item> { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item's description * @param aPartNumber * the item's part number */
   public Item(String aDescription, int aPartNumber) { description = aDescription; partNumber = aPartNumber; } /** * Gets the description of this item. * * @return the description */
   public String getDescription() { return description; } public String toString() { return "[description=" + description + ", partNumber=" + partNumber + "]"; } public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; Item other = (Item) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(Item other) { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }

 运行结果以下:

 

 测试程序4:

l 使用JDK命令运行HashMapDemo程序,结合程序运行结果理解程序;

 1 import java.util.*;  2 
 3 public class HashMapDemo {  4 
 5    public static void main(String[] argv) {  6 
 7       HashMap h = new HashMap();  8 
 9       // The hash maps from company name to address.
10 
11       h.put("Adobe", "Mountain View, CA"); 12 
13       h.put("IBM", "White Plains, NY"); 14 
15       h.put("Sun", "Mountain View, CA"); 16 
17       String queryString = "Adobe"; 18 
19       String resultString = (String)h.get(queryString); 20 
21       System.out.println("They are located in: " + resultString); 22 
23  } 24 
25 }

 运行结果以下:

 

l 在Elipse环境下调试教材373页程序9-6,结合程序运行结果理解程序;

l 了解HashMap、TreeMap两个类的用途及经常使用API。

 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 }

 运行结果以下:

 

 

 

实验2:结对编程练习:

 

http://www.cnblogs.com/xinz/archive/2011/08/07/2130332.html

http://en.wikipedia.org/wiki/Pair_programming

l 对于结对编程中代码设计规范的要求参考:

http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html

 

如下实验,就让咱们来体验一下结对编程的魅力。

l 肯定本次实验结对编程合做伙伴;

l 各自运行合做伙伴实验九编程练习1,结合使用体验对所运行程序提出完善建议;

 1 import java.io.BufferedReader;  2 import java.io.File;  3 import java.io.FileInputStream;  4 import java.io.FileNotFoundException;  5 import java.io.IOException;  6 import java.io.InputStreamReader;  7 import java.util.ArrayList;  8 import java.util.Arrays;  9 import java.util.Collections;  10 import java.util.Scanner;  11 
 12 public class Main{  13     private static ArrayList<Person> Personlist;  14     public static void main(String[] args) {  15         Personlist = new ArrayList<>();  16         Scanner scanner = new Scanner(System.in);  17         File file = new File("D:\\身份证号.txt");  18         try {  19             FileInputStream fis = new FileInputStream(file);  20             BufferedReader in = new BufferedReader(new InputStreamReader(fis));  21             String temp = null;  22             while ((temp = in.readLine()) != null) {  23                 
 24                 Scanner linescanner = new Scanner(temp);  25                 
 26                 linescanner.useDelimiter(" ");  27                 String name = linescanner.next();  28                 String ID = linescanner.next();  29                 String sex = linescanner.next();  30                 String age = linescanner.next();  31                 String place =linescanner.nextLine();  32                 Person Person = new Person();  33  Person.setname(name);  34  Person.setID(ID);  35  Person.setsex(sex);  36                 int a = Integer.parseInt(age);  37  Person.setage(a);  38  Person.setbirthplace(place);  39  Personlist.add(Person);  40 
 41  }  42         } catch (FileNotFoundException e) {  43             System.out.println("查找不到信息");  44  e.printStackTrace();  45         } catch (IOException e) {  46             System.out.println("信息读取有误");  47  e.printStackTrace();  48  }  49         boolean isTrue = true;  50         while (isTrue) {  51             System.out.println("————————————————————————————————————————");  52             System.out.println("1:按姓名字典序输出人员信息");  53             System.out.println("2:查询最大年龄与最小年龄人员信息");  54             System.out.println("3:按省份找同乡");  55             System.out.println("4:输入你的年龄,查询年龄与你最近人的信息");  56             System.out.println("5:exit");  57             int nextInt = scanner.nextInt();  58             switch (nextInt) {  59             case 1:  60  Collections.sort(Personlist);  61  System.out.println(Personlist.toString());  62                 break;  63             case 2:  64                 
 65                 int max=0,min=100;int j,k1 = 0,k2=0;  66                 for(int i=1;i<Personlist.size();i++)  67  {  68                     j=Personlist.get(i).getage();  69                    if(j>max)  70  {  71                        max=j;  72                        k1=i;  73  }  74                    if(j<min)  75  {  76                        min=j;  77                        k2=i;  78  }  79 
 80  }  81                 System.out.println("年龄最大:"+Personlist.get(k1));  82                 System.out.println("年龄最小:"+Personlist.get(k2));  83                 break;  84             case 3:  85                 System.out.println("place?");  86                 String find = scanner.next();  87                 String place=find.substring(0,3);  88                 String place2=find.substring(0,3);  89                 for (int i = 0; i <Personlist.size(); i++)  90  {  91                     if(Personlist.get(i).getbirthplace().substring(1,4).equals(place))  92                         System.out.println("maybe is      "+Personlist.get(i));  93 
 94  }  95 
 96                 break;  97             case 4:  98                 System.out.println("年龄:");  99                 int yourage = scanner.nextInt(); 100                 int near=agenear(yourage); 101                 int d_value=yourage-Personlist.get(near).getage(); 102                 System.out.println(""+Personlist.get(near)); 103            /* for (int i = 0; i < Personlist.size(); i++) 104  { 105  int p=Personlist.get(i).getage()-yourage; 106  if(p<0) p=-p; 107  if(p==d_value) System.out.println(Personlist.get(i)); 108  } */
109                 break; 110             case 5: 111            isTrue = false; 112            System.out.println("退出程序!"); 113                 break; 114             default: 115                 System.out.println("输入有误"); 116  } 117  } 118  } 119     public static int agenear(int age) { 120      
121        int j=0,min=53,d_value=0,k=0; 122         for (int i = 0; i < Personlist.size(); i++) 123  { 124             d_value=Personlist.get(i).getage()-age; 125             if(d_value<0) d_value=-d_value; 126             if (d_value<min) 127  { 128                min=d_value; 129                k=i; 130  } 131 
132          }    return k; 133         
134  } 135 
136  
137 }

 

 1 public class Person implements Comparable<Person> {  2 private String name;  3 private String ID;  4 private int age;  5 private String sex;  6 private String birthplace;  7 
 8 public String getname() {  9 return name; 10 } 11 public void setname(String name) { 12 this.name = name; 13 } 14 public String getID() { 15 return ID; 16 } 17 public void setID(String ID) { 18 this.ID= ID; 19 } 20 public int getage() { 21 
22 return age; 23 } 24 public void setage(int age) { 25     // int a = Integer.parseInt(age);
26 this.age= age; 27 } 28 public String getsex() { 29 return sex; 30 } 31 public void setsex(String sex) { 32 this.sex= sex; 33 } 34 public String getbirthplace() { 35 return birthplace; 36 } 37 public void setbirthplace(String birthplace) { 38 this.birthplace= birthplace; 39 } 40 
41 public int compareTo(Person o) { 42    return this.name.compareTo(o.getname()); 43 
44 } 45 
46 public String toString() { 47     return  name+"\t"+sex+"\t"+age+"\t"+ID+"\t"+birthplace+"\n"; 48 
49 } 50 
51 
52 
53 }

 

 

 

l 各自运行合做伙伴实验十编程练习2,结合使用体验对所运行程序提出完善建议;

 1 import java.io.FileNotFoundException;  2 import java.io.PrintWriter;  3 import java.util.Scanner;  4 
 5 /*
 6  * 该程序用来随机生成0到100之内的加减乘除题  7  */
 8 public class Demo {  9     public static  void main(String[] args) { 10         // 用户的答案要从键盘输入,所以须要一个键盘输入流
11         Scanner in = new Scanner(System.in); 12         Counter counter=new Counter(); 13         PrintWriter out = null; 14         try { 15             out = new PrintWriter("text.txt"); 16         } catch (FileNotFoundException e) { 17             // TODO Auto-generated catch block
18  e.printStackTrace(); 19  } 20         // 定义一个变量用来统计得分
21         int sum = 0; 22         int k=0; 23         // 经过循环生成10道题
24         for (int i = 0; i < 10; i++) { 25 
26             // 随机生成两个100之内的随机数做加减乘除
27             int a = (int) Math.round(Math.random() * 100); 28             int b = (int) Math.round(Math.random() * 100); 29             int d = (int) Math.round(Math.random() * 3); 30             
31             switch (d){ 32             
33             case 0: 34               if(a%b == 0) { 35               System.out.println(a + "/" + b + "="); 36               break; 37  } 38               //int c = in.nextInt(); 39               //out.println(a + "/" + b + "="+c);
40             case 1: 41               System.out.println(a + "*" + b + "="); 42               //int c1 = in.nextInt(); 43               //out.println(a + "*" + b + "="+c1);
44               break; 45             case 2: 46               System.out.println(a + "+" + b + "="); 47               //int c2 = in.nextInt(); 48               //out.println(a + "+" + b + "="+c2);
49               break; 50             case 3: 51             if(a>b) { 52             System.out.println(a + "-" + b + "="); 53             break; 54  } 55             //int c3 = in.nextInt(); 56             //out.println(a + "-" + b + "="+c3);
57             
58  } 59 
60             // 定义一个整数用来接收用户输入的答案
61             double c = in.nextDouble(); 62             
63             // 判断用户输入的答案是否正确,正确给10分,错误不给分
64             if (c == a / b | c == a * b | c == a + b | c == a - b) { 65                 sum += 10; 66                 System.out.println("恭喜答案正确"); 67  } 68             else { 69                 System.out.println("抱歉,答案错误"); 70             
71  } 72             out.println(a + "/" + b + "="+c ); 73             out.println(a + "*" + b + "="+c); 74             out.println(a + "+" + b + "="+c); 75             out.println(a + "-" + b + "="+c); 76         
77  } 78         //输出用户的成绩
79         System.out.println("你的得分为"+sum); 80         
81         out.println("成绩:"+sum); 82  out.close(); 83  } 84     }

 

 1 public class Counter {  2        private int a;  3        private int b;  4         public int  add(int a,int b)  5  {  6             return a+b;  7  }  8         public int   reduce(int a,int b)  9  { 10             return a-b; 11  } 12         public int   multiplication(int a,int b) 13  { 14             return a*b; 15  } 16         public int   division(int a,int b) 17  { 18             if(b!=0) 19             return a/b; 20             else return 0; 21  } 22 
23 }

 

 

 

l 采用结对编程方式,与学习伙伴合做完成实验九编程练习1;

l 采用结对编程方式,与学习伙伴合做完成实验十编程练习2。

1)    程序互测概述、心得;

  结对编程中在测试小伙伴儿的代码时发现了一些好的语句在我代码中没有运用到的,以及一些好的设计本身以前没有想到的,不少地方都值得我学习和借鉴。

2)    结对编程代码;

实验九编程练习1

 1 package ID;  2 
 3 public class Person implements Comparable<Person> {  4     private String name;  5     private String ID;  6     private int age;  7     private String sex;  8     private String birthplace;  9 
10     public String getname() { 11         return name; 12  } 13 
14     public void setname(String name) { 15         this.name = name; 16  } 17 
18     public String getID() { 19         return ID; 20  } 21 
22     public void setID(String ID) { 23         this.ID = ID; 24  } 25 
26     public int getage() { 27 
28         return age; 29  } 30 
31     public void setage(int age) { 32 
33         this.age = age; 34  } 35 
36     public String getsex() { 37         return sex; 38  } 39 
40     public void setsex(String sex) { 41         this.sex = sex; 42  } 43 
44     public String getbirthplace() { 45         return birthplace; 46  } 47 
48     public void setbirthplace(String birthplace) { 49         this.birthplace = birthplace; 50  } 51 
52     public int compareTo(Person o) { 53         return this.name.compareTo(o.getname()); 54  } 55 
56     public String toString() { 57         return name + "\t" + sex + "\t" + age + "\t" + ID + "\t" + birthplace + "\n"; 58  } 59 }

 

 1 package ID;  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.Arrays;  11 import java.util.Collections;  12 import java.util.Scanner;  13 
 14 public class Main {  15 private static ArrayList<Person> Personlist;  16 
 17 public static void main(String[] args) {  18 Personlist = new ArrayList<>();  19 Scanner scanner = new Scanner(System.in);  20 File file = new File("身份证号.txt");  21 try {  22 FileInputStream fis = new FileInputStream(file);  23 BufferedReader in = new BufferedReader(new InputStreamReader(fis));  24 String temp = null;  25 while ((temp = in.readLine()) != null) {  26 
 27 Scanner linescanner = new Scanner(temp);  28 
 29 linescanner.useDelimiter(" ");  30 String name = linescanner.next();  31 String ID = linescanner.next();  32 String sex = linescanner.next();  33 String age = linescanner.next();  34 String place = linescanner.nextLine();  35 Person Person = new Person();  36 Person.setname(name);  37 Person.setID(ID);  38 Person.setsex(sex);  39 int a = Integer.parseInt(age);  40 Person.setage(a);  41 Person.setbirthplace(place);  42 Personlist.add(Person);  43 
 44 }  45 } catch (FileNotFoundException e) {  46 System.out.println("查找不到信息");  47 e.printStackTrace();  48 } catch (IOException e) {  49 System.out.println("信息读取有误");  50 e.printStackTrace();  51 }  52 boolean isTrue = true;  53 while (isTrue) {  54 System.out.println("1:按姓名字典序输出人员信息");  55 System.out.println("2:查询最大年龄与最小年龄人员信息");  56 System.out.println("3:按省份找同乡");  57 System.out.println("4:输入你的年龄,查询年龄与你最近人的信息");  58 System.out.println("5:退出");  59 int nextInt = scanner.nextInt();  60 switch (nextInt) {  61 case 1:  62 Collections.sort(Personlist);  63 System.out.println(Personlist.toString());  64 break;  65 case 2:  66 int max = 0, min = 100;  67 int j, k1 = 0, k2 = 0;  68 for (int i = 1; i < Personlist.size(); i++) {  69 j = Personlist.get(i).getage();  70 if (j > max) {  71 max = j;  72 k1 = i;  73 }  74 if (j < min) {  75 min = j;  76 k2 = i;  77 }  78 
 79 }  80 System.out.println("年龄最大:" + Personlist.get(k1));  81 System.out.println("年龄最小:" + Personlist.get(k2));  82 break;  83 case 3:  84 System.out.println("省份?");  85 String find = scanner.next();  86 String place = find.substring(0, 3);  87 String place2 = find.substring(0, 3);  88 for (int i = 0; i < Personlist.size(); i++) {  89 if (Personlist.get(i).getbirthplace().substring(1, 4).equals(place))  90 System.out.println("同乡 " + Personlist.get(i));  91 
 92 }  93 
 94 break;  95 case 4:  96 System.out.println("年龄:");  97 int yourage = scanner.nextInt();  98 int near = agenear(yourage);  99 int d_value = yourage - Personlist.get(near).getage(); 100 System.out.println("" + Personlist.get(near)); 101 
102 break; 103 case 5: 104 isTrue = false; 105 System.out.println("欢迎使用!"); 106 break; 107 default: 108 System.out.println("输入有误"); 109 } 110 } 111 } 112 
113 public static int agenear(int age) { 114 
115 int j = 0, min = 53, d_value = 0, k = 0; 116 for (int i = 0; i < Personlist.size(); i++) { 117 d_value = Personlist.get(i).getage() - age; 118 if (d_value < 0) 119 d_value = -d_value; 120 if (d_value < min) { 121 min = d_value; 122 k = i; 123 } 124 } 125 return k; 126 } 127 }

 

 

实验十编程练习2

 1 import java.io.*;  2 import java.io.PrintWriter;  3 import java.util.Scanner;  4 
 5 public class Main {  6     public static void main(String[] args) {  7 
 8         Scanner in = new Scanner(System.in);  9 
10         PrintWriter output = null; 11         try { 12             output = new PrintWriter("text.txt"); 13         } catch (FileNotFoundException e) { 14             // TODO Auto-generated catch block
15  e.printStackTrace(); 16  } 17         int sum = 0; 18         for (int i = 1; i <= 10; i++) { 19             int a = (int) Math.round(Math.random() * 100); 20             int b = (int) Math.round(Math.random() * 100); 21             int m = (int) Math.round(Math.random() * 4); 22             switch (m) { 23             case 1: 24                 while (b == 0) { 25                     b = (int) Math.round(Math.random() * 100); 26  } 27                 while (a % b != 0) { 28                     a = (int) Math.round(Math.random() * 100); 29  } 30                 System.out.println(i + ": " + a + "/" + b + "="); 31                 int c1 = in.nextInt(); 32                 output.println(a + "/" + b + "=" + c1); 33                 if (c1 == a / b) { 34                     System.out.println("恭喜答案正确"); 35                     sum += 10; 36                 } else { 37                     System.out.println("抱歉,答案错误"); 38  } 39 
40                 break; 41 
42             case 2: 43                 System.out.println(i + ": " + a + "*" + b + "="); 44                 int c2 = in.nextInt(); 45                 output.println(a + "*" + b + "=" + c2); 46                 if (c2 == a * b) { 47                     System.out.println("恭喜答案正确"); 48                     sum += 10; 49                 } else { 50                     System.out.println("抱歉,答案错误"); 51  } 52                 break; 53             case 3: 54                 System.out.println(i + ": " + a + "+" + b + "="); 55                 int c3 = in.nextInt(); 56                 output.println(a + "+" + b + "=" + c3); 57                 if (c3 == a + b) { 58                     System.out.println("恭喜答案正确"); 59                     sum += 10; 60                 } else { 61                     System.out.println("抱歉,答案错误"); 62  } 63 
64                 break; 65             case 4: 66                 while (a < b) { 67                     a = (int) Math.round(Math.random() * 100); 68  } 69                 System.out.println(i + ": " + a + "-" + b + "="); 70                 int c4 = in.nextInt(); 71                 output.println(a + "-" + b + "=" + c4); 72                 if (c4 == a - b) { 73                     System.out.println("恭喜答案正确"); 74                     sum += 10; 75                 } else { 76                     System.out.println("抱歉,答案错误"); 77  } 78                 break; 79 
80  } 81 
82  } 83         System.out.println("成绩" + sum); 84         output.println("成绩" + sum); 85  output.close(); 86  } 87 }

 

 1 public class math<T> {  2     private T a;  3     private T b;  4 
 5     public int add(int a, int b) {  6         return a + b;  7  }  8 
 9     public int reduce(int a, int b) { 10         return a - b; 11  } 12 
13     public int multiplication(int a, int b) { 14         return a * b; 15  } 16 
17     public int division(int a, int b) { 18         if (b != 0 && a % b == 0) 19             return a / b; 20         else
21             return 0; 22  } 23 }

 

3)    结对程序运行功能界面截图;

 

 

4)    结对过程描述。

   咱们分别运行了对方的程序,得出实验运行结果,互相阅读学习了对方代码中好的语句、想法。并将不懂的地方向对方提出疑问,并获得解答。学到了不少,受益不浅。

 

4. 实验总结:

   经过本次实验我掌握了Vetor、Stack、Hashtable三个类的用途及经常使用的API;了解了java集合框架体系的组成;掌握了ArrayList、LinkList两个类的用途及经常使用的API;了解了HashSet类、TreeSet类的用途及经常使用的API;还了解了HashMap、TreeMap两个类的用途及经常使用的API。

  经过结对编程练习,我体验到了程序开发中与小伙伴合做的乐趣,也懂得了合做学习的重要性,受益不浅。

相关文章
相关标签/搜索