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

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

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

5、散列表又称为哈希表。散列表算法的基本思想是:以结点的关键字为自变量,通过一定的函数关系(散列函数)计算出对应的函数值,以这个值作为该结点存储在散列表中的地址。当散列表中的元素存放太满,就必须进行再散列,将产生一个新的散列表,所有元素存放到新的散列表中,原先的散列表将被删除。在Java语言中,通过负载因子(load factor)来决定何时对散列表进行再散列。负载因子越高 (越接近1.0),内存的使用效率越高,元素的寻找时间越长。负载因子越低 (越接近0.0),元素的寻找时间越短,内存浪费越多。HashSet类的缺省负载因子是0.75。
6、JAVA的集合框架实现对各种数据结构的封装,以降低对数据管理与处理的难度。所谓框架就是一个类库的集合,框架中包含很多超类,编程者创建这些超类的子类可较方便的设计设计程序所需的类。
7、集合框架:JAVA集合类库的统一架构。
8、集合类的作用:Java的集合类提供了一些基本数据结构的支持。
9、集合类的使用: Java的集合类包含在java.util包中。
10、集合类的特点一:只容纳对象。注意:数组可以容纳基本数据类型数据和对象。如果集合类中想使用基本数据类型,又想利用集合类的灵活性,可以把基本数据类型数据封装成该数据类型的包装器对象,然后放入集合中处理。
              特点二:集合类容纳的对象都是Object类的实例,一旦把一个对象置入集合类中,它的类信息将丢失,这样设计的目的是为了集合类的通用性。因为Object类是所有类的祖先,所以可以在这些集合中存放任 何类的对象而不受限制,但在使用集合成员之前必须对它重新造型。
11、Vector类类似长度可变的数组。Vector中只能存放对象。Vector的元素通过下标进行访问。 Vector类关键属性:capacity表示集合最多能容纳的元素个数。capacityIncrement表示每次增加多少容量。size表示集合当前元素个数。
12、Stack类是Vector的子类。Stack类描述堆栈数据结构,即LIFO。
13、Hashtable通过键来查找元素。Hashtable用散列码(hashcode)来确定键。所有对象都有一个散列码,可以通过Object类的hashCode()方法获得。
14、集合框架中的基本接口:Collection:集合层次中的根接口,JDK未提供这个接口的直接实现类。
15、Set:不能包含重复的元素。对象可能不是按存放的次序存放,也就是说不能像数组一样按索引的方式进行访问,SortedSet是一个按照升序排列元素的Set。
List:是一个有序的集合,可以包含重复的元素。提供了按索引访问的方式。
Map:包含了key-value对。Map不能包含重复的key。
SortedMap是一个按照升序排列key的Map。
Collection接口:构造类集框架的基础。
16、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。

17、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。

(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
 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 }

运行结果如下:

方法二、

 1 //示例程序12 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-3、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。

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

转载于:https://www.cnblogs.com/yanglinga/p/9930145.html

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

  1. 201871010115——马北《面向对象程序设计JAVA》第二周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  2. 201621123080《Java程序设计》第十一周学习总结

    201621123080<Java程序设计>第十一周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 ...

  3. 《数据结构与面向对象程序设计》第1周学习总结

    20182316胡泊 2019-2020-1 <数据结构与面向对象程序设计>第1周学习总结 教材学习内容总结 简单java程序是有哪些部分组成的 Java程序好的排版布局是怎样的 程序开发 ...

  4. 《Java程序设计》第十一周学习总结

    20175334 <Java程序设计>第十一周学习总结 教材学习内容总结 第十三章 URL类 一个URL对象通常包含最基本的三部分信息:协议.地址.资源. URL对象调用 InputStr ...

  5. 20175227张雪莹 2018-2019-2 《Java程序设计》第十一周学习总结

    20175227张雪莹 2018-2019-2 <Java程序设计>第十一周学习总结 教材学习内容总结 第十三章 Java网络编程 URL类 一个URL对象通常包含最基本的三部分信息:协议 ...

  6. 20175208 《Java程序设计》第十一周学习总结

    20175208张家华第十一周学习总结 教材学习内容总结 第十三章java网络编程: 主要内容: URL类 InetAdress类 套接字 UDP数据报 广播数据报 Java远程调用(RMI) 1.U ...

  7. 杨玲 201771010133《面向对象程序设计(java)》第十六周学习总结

    <面向对象程序设计(java)>第十六周学习总结 第一部分:理论知识学习部分 1.程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕 ...

  8. 四十八.面向对象程序设计——Java语言第一周编程题:分数

    题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这个类要 ...

  9. 201621123069 《Java程序设计》第十一周学习总结

    ---恢复内容开始--- 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThr ...

  10. 201621123053《Java程序设计》第十一周学习笔记文章

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

最新文章

  1. AJAX+JQUERRY实现分页
  2. 乘法器的verilog实现(并行、移位相加、查找表)
  3. 产品经理装逼词汇大全
  4. 数字图像处理 空间域锐化 MATLAB实验
  5. OceanBase数据库开发和运维漫谈
  6. python/socket编程之粘包
  7. php删除记录图片吗,php 删除记录同时删除图片文件的实现代码_PHP教程
  8. IIS6配置后仍然无法解析json文件解决办法
  9. 页面加载完就执行的设置?
  10. 光电LED玻璃屏成商显新势力 优势颠覆传统LED灯条屏
  11. bzoj1597: 土地购买
  12. Java应用中CPU使用率过高该怎么解决
  13. 离散数学 2.3 Functions
  14. [矩阵论] 谱半径小于1,则I-A可逆
  15. JAVA计算机毕业设计电影视频在线点播系统Mybatis+系统+数据库+调试部署
  16. 用python画篮球场_如何使用 Python 创建一个 NBA 得分图
  17. Delphi @ ^
  18. vue3 props 声明默认值
  19. 基于SSM实现在线考试系统
  20. 如何将SOCKS5代理转换成HTTP代理?

热门文章

  1. 云计算安全需求分析与网络
  2. 搜狗浏览器安装第三方插件
  3. php扩展引擎手册,模板引擎-THINKPHP 5.0 手册最新版
  4. 【ArcGIS Pro微课1000例】0009:ArcGIS Pro地理配准完整教程(建议收藏)
  5. 黑客泄露澳大利亚大量求职者信息,其中包含11家重要公司名单
  6. 从抖音到“奶头乐“,它是如何一步步毁掉我们的?
  7. shell脚本加密工具shc
  8. 如何把证件照压缩到20k?证件照太大如何压缩?
  9. c# 两行代码合并pdf文件
  10. 网络基础(五)------静态路由--实操