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

实验十一   集合

实验时间 2018-11-8

1、实验目的与要求

(1) 掌握VetorStackHashtable三个类的用途及常用API

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

(3) 掌握ArrayListLinkList两个类的用途及常用API

(4) 了解HashSet类、TreeSet类的用途及常用API

(5)了解HashMapTreeMap两个类的用途及常用API

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

2、实验内容和步骤

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

测试程序1:

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

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

//示例程序1

import java.util.Vector;

 

class Cat {

private int catNumber;

 

Cat(int i) {

       catNumber = i;

}

 

void print() {

       System.out.println("Cat #" + catNumber);

}

}

 

class Dog {

private int dogNumber;

 

Dog(int i) {

       dogNumber = i;

}

 

void print() {

       System.out.println("Dog #" + dogNumber);

}

}

 

public class CatsAndDogs {

public static void main(String[] args) {

       Vector cats = new Vector();

       for (int i = 0; i < 7; i++)

             cats.addElement(new Cat(i));

       cats.addElement(new Dog(7));

       for (int i = 0; i < cats.size(); i++)

             ((Cat) cats.elementAt(i)).print();

}

}

//示例程序2

import java.util.*;

 

public class Stacks {

   static String[] months = { "1", "2", "3", "4" };

 

   public static void main(String[] args) {

      Stack stk = new Stack();

      for (int i = 0; i < months.length; i++)

          stk.push(months[i]);

      System.out.println(stk);

      System.out.println("element 2=" + stk.elementAt(2));

      while (!stk.empty())

          System.out.println(stk.pop());

   }

}

//示例程序3

import java.util.*;

 

class Counter {

      int i = 1;

 

      public String toString() {

            return Integer.toString(i);

      }

}

 

public class Statistics {

      public static void main(String[] args) {

            Hashtable ht = new Hashtable();

            for (int i = 0; i < 10000; i++) {

                  Integer r = new Integer((int) (Math.random() * 20));

                  if (ht.containsKey(r))

                       ((Counter) ht.get(r)).i++;

                  else

                       ht.put(r, new Counter());

            }

            System.out.println(ht);

      }

}

实验结果1

 

实验结果2

 实验结果3

 

测试程序2:

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

import java.util.*;

 

public class ArrayListDemo {

public static void main(String[] argv) {

       ArrayList al = new ArrayList();

       // Add lots of elements to the ArrayList...

       al.add(new Integer(11));

       al.add(new Integer(12));

       al.add(new Integer(13));

       al.add(new String("hello"));

       // First print them out using a for loop.

       System.out.println("Retrieving by index:");

       for (int i = 0; i < al.size(); i++) {

             System.out.println("Element " + i + " = " + al.get(i));

       }

}

}

import java.util.*;

public class LinkedListDemo {

    public static void main(String[] argv) {

        LinkedList l = new LinkedList();

        l.add(new Object());

        l.add("Hello");

        l.add("zhangsan");

        ListIterator li = l.listIterator(0);

        while (li.hasNext())

            System.out.println(li.next());

        if (l.indexOf("Hello") < 0)  

            System.err.println("Lookup does not work");

        else

            System.err.println("Lookup works");

   }

}

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

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

package linkedList;

import java.util.*;

/**
 * This program demonstrates operations on linked lists.
 * @version 1.11 2012-01-26
 * @author Cay Horstmann
 */
public class LinkedListTest
{
   public static void main(String[] args)
   {
      List<String> a = new LinkedList<>();
      a.add("Amy");
      a.add("Carl");
      a.add("Erica");

      List<String> b = new LinkedList<>();
      b.add("Bob");
      b.add("Doug");
      b.add("Frances");
      b.add("Gloria");

      // merge the words from b into a

      ListIterator<String> aIter = a.listIterator();
      Iterator<String> bIter = b.iterator();

      while (bIter.hasNext())
      {
         if (aIter.hasNext()) aIter.next();
         aIter.add(bIter.next());
      }

      System.out.println(a);

      // remove every second word from b

      bIter = b.iterator();
      while (bIter.hasNext())
      {
         bIter.next(); // skip one element
         if (bIter.hasNext())
         {
            bIter.next(); // skip next element
            bIter.remove(); // remove that element
         }
      }

      System.out.println(b);

      // bulk operation: remove all words in b from a

      a.removeAll(b);

      System.out.println(a);
   }
}

测试程序3:

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

import java.util.*;

public class SetDemo {

    public static void main(String[] argv) {

        HashSet h = new HashSet(); //也可以 Set h=new HashSet()

        h.add("One");

        h.add("Two");

        h.add("One"); // DUPLICATE

        h.add("Three");

        Iterator it = h.iterator();

        while (it.hasNext()) {

             System.out.println(it.next());

        }

    }

}

 

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 }
View Code

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 }
View Code
 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 }
View Code

测试程序4:

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

import java.util.*;

public class HashMapDemo {

   public static void main(String[] argv) {

      HashMap h = new HashMap();

      // The hash maps from company name to address.

      h.put("Adobe", "Mountain View, CA");

      h.put("IBM", "White Plains, NY");

      h.put("Sun", "Mountain View, CA");

      String queryString = "Adobe";

      String resultString = (String)h.get(queryString);

      System.out.println("They are located in: " +  resultString);

  }

}

实验结果:

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

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

 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 }
View Code
 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 }
View Code

实验结果:

实验2:结对编程练习:

关于结对编程:以下图片是一个结对编程场景:两位学习伙伴坐在一起,面对着同一台显示器,使用着同一键盘,同一个鼠标,他们一起思考问题,一起分析问题,一起编写程序。

关于结对编程的阐述可参见以下链接:

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

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

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

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

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

确定本次实验结对编程合作伙伴;

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

  1 package test1;
  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.Collections;
 11 import java.util.Scanner;
 12 
 13 public class Main{
 14     private static ArrayList<Student> studentlist;
 15     public static void main(String[] args) {
 16         studentlist = new ArrayList<>();
 17         Scanner scanner = new Scanner(System.in);
 18         File file = new File("F:\身份证号.txt");
 19         try {
 20             FileInputStream fis = new FileInputStream(file);
 21             BufferedReader in = new BufferedReader(new InputStreamReader(fis));
 22             String temp = null;
 23             while ((temp = in.readLine()) != null) {
 24                 
 25                 Scanner linescanner = new Scanner(temp);
 26                 
 27                 linescanner.useDelimiter(" ");    
 28                 String name = linescanner.next();
 29                 String number = linescanner.next();
 30                 String sex = linescanner.next();
 31                 String age = linescanner.next();
 32                 String province =linescanner.nextLine();
 33                 Student student = new Student();
 34                 student.setName(name);
 35                 student.setnumber(number);
 36                 student.setsex(sex);
 37                 int a = Integer.parseInt(age);
 38                 student.setage(a);
 39                 student.setprovince(province);
 40                 studentlist.add(student);
 41 
 42             }
 43         } catch (FileNotFoundException e) {
 44             System.out.println("学生信息文件找不到");
 45             e.printStackTrace();
 46         } catch (IOException e) {
 47             System.out.println("学生信息文件读取错误");
 48             e.printStackTrace();
 49         }
 50         boolean isTrue = true;
 51         while (isTrue) {
 52             System.out.println("选择你的操作,输入正确格式的选项");
 53             System.out.println("A.按姓名字典排序");
 54             System.out.println("B.输出年龄最大和年龄最小的人");
 55             System.out.println("C.寻找老乡");
 56             System.out.println("D.寻找年龄相近的人");
 57             System.out.println("F.退出");
 58             String m = scanner.next();
 59             switch (m) {
 60             case "A":
 61                 Collections.sort(studentlist);              
 62                 System.out.println(studentlist.toString());
 63                 break;
 64             case "B":
 65                  int max=0,min=100;
 66                  int j,k1 = 0,k2=0;
 67                  for(int i=1;i<studentlist.size();i++)
 68                  {
 69                      j=studentlist.get(i).getage();
 70                  if(j>max)
 71                  {
 72                      max=j; 
 73                      k1=i;
 74                  }
 75                  if(j<min)
 76                  {
 77                    min=j; 
 78                    k2=i;
 79                  }
 80                  
 81                  }  
 82                  System.out.println("年龄最大:"+studentlist.get(k1));
 83                  System.out.println("年龄最小:"+studentlist.get(k2));
 84                 break;
 85             case "C":
 86                  System.out.println("老家?");
 87                  String find = scanner.next();        
 88                  String place=find.substring(0,3);
 89                  for (int i = 0; i <studentlist.size(); i++) 
 90                  {
 91                      if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
 92                          System.out.println("老乡"+studentlist.get(i));
 93                  }             
 94                  break;
 95                  
 96             case "D":
 97                 System.out.println("年龄:");
 98                 int yourage = scanner.nextInt();
 99                 int near=agenear(yourage);
100                 int value=yourage-studentlist.get(near).getage();
101                 System.out.println(""+studentlist.get(near));
102                 break;
103             case "F":
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         int j=0,min=53,value=0,k=0;
115          for (int i = 0; i < studentlist.size(); i++)
116          {
117              value=studentlist.get(i).getage()-age;
118              if(value<0) value=-value; 
119              if (value<min) 
120              {
121                 min=value;
122                 k=i;
123              } 
124           }    
125          return k;         
126       }
127 
128 }
View Code
 1 package test1;
 2 
 3 public class Student implements Comparable<Student> {
 4 
 5     private String name;
 6     private String number ;
 7     private String sex ;
 8     private int age;
 9     private String province;
10    
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getnumber() {
18         return number;
19     }
20     public void setnumber(String number) {
21         this.number = number;
22     }
23     public String getsex() {
24         return sex ;
25     }
26     public void setsex(String sex ) {
27         this.sex =sex ;
28     }
29     public int getage() {
30 
31         return age;
32         }
33         public void setage(int age) {
34             // int a = Integer.parseInt(age);
35         this.age= age;
36         }
37 
38     public String getprovince() {
39         return province;
40     }
41     public void setprovince(String province) {
42         this.province=province ;
43     }
44 
45     public int compareTo(Student o) {
46        return this.name.compareTo(o.getName());
47     }
48 
49     public String toString() {
50         return  name+"	"+sex+"	"+age+"	"+number+"	"+province+"
";
51     }    
52 }
View Code

完善建议:无

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

 1 package shiyan;
 2 import java.util.Scanner;
 3 import java.io.FileNotFoundException;
 4 import java.io.PrintWriter;
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         Scanner in=new Scanner(System.in);
 9         PrintWriter output = null;
10         try {
11             output = new PrintWriter("E:/test.txt");
12         } catch (FileNotFoundException e) {
13             // TODO 自动生成的 catch 块
14             System.out.println("文件输出失败");
15             e.printStackTrace();
16         }
17         int sum=0;
18         jisuanji js=new jisuanji();
19         for (int i = 0; i < 10; i++) {
20             int a = (int) Math.round(Math.random() * 100);
21             int b = (int) Math.round(Math.random() * 100);
22             int n = (int) Math.round(Math.random() * 4 );
23             
24             switch(n)
25                {
26                case 1:
27                    System.out.println(a+"/"+b+"=");
28                    while(b==0){  
29                        b = (int) Math.round(Math.random() * 100); 
30                        }
31                    while(a%b!=0) {
32                        a = (int) Math.round(Math.random() * 100);
33                        b = (int) Math.round(Math.random() * 100);
34                    }
35                    double c = in.nextDouble();
36                    output.println(a+"/"+b+"="+c);
37                    if (c == js.chu(a,b)) {
38                        sum += 10;
39                        System.out.println("答案正确");
40                    }
41                    else {
42                        System.out.println("答案错误");
43                    }
44                 
45                    break;
46                 
47                case 2:
48                    System.out.println(a+"*"+b+"=");
49                    int c1 = in.nextInt();
50                    output.println(a+"*"+b+"="+c1);
51                    if (c1 == js.chen(a, b)) {
52                        sum += 10;
53                        System.out.println("答案正确");
54                    }
55                    else {
56                        System.out.println("答案错误");
57                    }
58                    break;
59                case 3:
60                    System.out.println(a+"+"+b+"=");
61                    int c2 = in.nextInt();
62                    output.println(a+"+"+b+"="+c2);
63                    if (c2 == js.jia(a, b)) {
64                        sum += 10;
65                        System.out.println("答案正确");
66                    }
67                    else {
68                        System.out.println("答案错误");
69                    }
70                    
71                    break ;
72                case 4:
73                    System.out.println(a+"-"+b+"=");
74                    while(a<b) {
75                        a = (int) Math.round(Math.random() * 100);
76                        b = (int) Math.round(Math.random() * 100);
77                    }
78                    int c3 = in.nextInt();
79                    output.println(a+"-"+b+"="+c3);
80                    if (c3 == js.jian(a,b)) {
81                        sum += 10;
82                        System.out.println("答案正确");
83                    }
84                    else {
85                        System.out.println("答案错误");
86                    }
87                    break ;
88 
89                    } 
90         
91               }
92             System.out.println("成绩"+sum);
93             output.println("成绩:"+sum);
94             output.close();
95         }
96     }
View Code
 1 package shiyan;
 2 
 3 public class jisuanji<T> {
 4     private T a;
 5     private T b;
 6     public jisuanji() {
 7         a=null;
 8         b=null;
 9     }
10     public jisuanji(T a,T b) {
11         this.a=a;
12         this.b=b;
13     }
14     public int jia(int a,int b)
15     {
16         return a+b;
17     }
18     public int jian(int a,int b)
19     {
20         return a-b;
21     }
22     public int chen(int a,int b)
23     {
24         return a*b;
25     }
26     public int chu(int a,int b)
27     {
28         if(b!=0&&a%b==0) 
29             return a/b;
30         else 
31             return 0;
32     }
33 }
View Code

完善建议:jisuanji类的除法的一些限制条件多余,可以不写,简化代码。

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

  1 package test1;
  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.Collections;
 11 import java.util.Scanner;
 12 
 13 public class Main{
 14     private static ArrayList<Student> studentlist;
 15     public static void main(String[] args) {
 16         studentlist = new ArrayList<>();
 17         Scanner scanner = new Scanner(System.in);
 18         File file = new File("F:\身份证号.txt");
 19         try {
 20             FileInputStream fis = new FileInputStream(file);
 21             BufferedReader in = new BufferedReader(new InputStreamReader(fis));
 22             String temp = null;
 23             while ((temp = in.readLine()) != null) {
 24                 
 25                 Scanner linescanner = new Scanner(temp);
 26                 
 27                 linescanner.useDelimiter(" ");    
 28                 String name = linescanner.next();
 29                 String number = linescanner.next();
 30                 String sex = linescanner.next();
 31                 String age = linescanner.next();
 32                 String province =linescanner.nextLine();
 33                 Student student = new Student();
 34                 student.setName(name);
 35                 student.setnumber(number);
 36                 student.setsex(sex);
 37                 int a = Integer.parseInt(age);
 38                 student.setage(a);
 39                 student.setprovince(province);
 40                 studentlist.add(student);
 41 
 42             }
 43         } catch (FileNotFoundException e) {
 44             System.out.println("学生信息文件找不到");
 45             e.printStackTrace();
 46         } catch (IOException e) {
 47             System.out.println("学生信息文件读取错误");
 48             e.printStackTrace();
 49         }
 50         boolean isTrue = true;
 51         while (isTrue) {
 52             System.out.println("选择你的操作,输入正确格式的选项");
 53             System.out.println("A.按姓名字典排序");
 54             System.out.println("B.输出年龄最大和年龄最小的人");
 55             System.out.println("C.寻找老乡");
 56             System.out.println("D.寻找年龄相近的人");
 57             System.out.println("F.退出");
 58             String m = scanner.next();
 59             switch (m) {
 60             case "A":
 61                 Collections.sort(studentlist);              
 62                 System.out.println(studentlist.toString());
 63                 break;
 64             case "B":
 65                  int max=0,min=100;
 66                  int j,k1 = 0,k2=0;
 67                  for(int i=1;i<studentlist.size();i++)
 68                  {
 69                      j=studentlist.get(i).getage();
 70                  if(j>max)
 71                  {
 72                      max=j; 
 73                      k1=i;
 74                  }
 75                  if(j<min)
 76                  {
 77                    min=j; 
 78                    k2=i;
 79                  }
 80                  
 81                  }  
 82                  System.out.println("年龄最大:"+studentlist.get(k1));
 83                  System.out.println("年龄最小:"+studentlist.get(k2));
 84                 break;
 85             case "C":
 86                  System.out.println("老家?");
 87                  String find = scanner.next();        
 88                  String place=find.substring(0,3);
 89                  for (int i = 0; i <studentlist.size(); i++) 
 90                  {
 91                      if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
 92                          System.out.println("老乡"+studentlist.get(i));
 93                  }             
 94                  break;
 95                  
 96             case "D":
 97                 System.out.println("年龄:");
 98                 int yourage = scanner.nextInt();
 99                 int near=agenear(yourage);
100                 int value=yourage-studentlist.get(near).getage();
101                 System.out.println(""+studentlist.get(near));
102                 break;
103             case "F":
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         int j=0,min=53,value=0,k=0;
115          for (int i = 0; i < studentlist.size(); i++)
116          {
117              value=studentlist.get(i).getage()-age;
118              if(value<0) value=-value; 
119              if (value<min) 
120              {
121                 min=value;
122                 k=i;
123              } 
124           }    
125          return k;         
126       }
127 
128 }
View Code
 1 package test1;
 2 
 3 public class Student implements Comparable<Student> {
 4 
 5     private String name;
 6     private String number ;
 7     private String sex ;
 8     private int age;
 9     private String province;
10    
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getnumber() {
18         return number;
19     }
20     public void setnumber(String number) {
21         this.number = number;
22     }
23     public String getsex() {
24         return sex ;
25     }
26     public void setsex(String sex ) {
27         this.sex =sex ;
28     }
29     public int getage() {
30 
31         return age;
32         }
33         public void setage(int age) {
34             // int a = Integer.parseInt(age);
35         this.age= age;
36         }
37 
38     public String getprovince() {
39         return province;
40     }
41     public void setprovince(String province) {
42         this.province=province ;
43     }
44 
45     public int compareTo(Student o) {
46        return this.name.compareTo(o.getName());
47     }
48 
49     public String toString() {
50         return  name+"	"+sex+"	"+age+"	"+number+"	"+province+"
";
51     }    
52 }
View Code

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

 1 package shiyan;
 2 import java.util.Scanner;
 3 import java.io.FileNotFoundException;
 4 import java.io.PrintWriter;
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         Scanner in=new Scanner(System.in);
 9         PrintWriter output = null;
10         try {
11             output = new PrintWriter("E:/test.txt");
12         } catch (FileNotFoundException e) {
13             // TODO 自动生成的 catch 块
14             System.out.println("文件输出失败");
15             e.printStackTrace();
16         }
17         int sum=0;
18         jisuanji js=new jisuanji();
19         for (int i = 0; i < 10; i++) {
20             int a = (int) Math.round(Math.random() * 100);
21             int b = (int) Math.round(Math.random() * 100);
22             int n = (int) Math.round(Math.random() * 4 );
23             
24             switch(n)
25                {
26                case 1:
27                    System.out.println(a+"/"+b+"=");
28                    while(b==0){  
29                        b = (int) Math.round(Math.random() * 100); 
30                        }
31                    while(a%b!=0) {
32                        a = (int) Math.round(Math.random() * 100);
33                        b = (int) Math.round(Math.random() * 100);
34                    }
35                    double c = in.nextDouble();
36                    output.println(a+"/"+b+"="+c);
37                    if (c == js.chu(a,b)) {
38                        sum += 10;
39                        System.out.println("答案正确");
40                    }
41                    else {
42                        System.out.println("答案错误");
43                    }
44                 
45                    break;
46                 
47                case 2:
48                    System.out.println(a+"*"+b+"=");
49                    int c1 = in.nextInt();
50                    output.println(a+"*"+b+"="+c1);
51                    if (c1 == js.chen(a, b)) {
52                        sum += 10;
53                        System.out.println("答案正确");
54                    }
55                    else {
56                        System.out.println("答案错误");
57                    }
58                    break;
59                case 3:
60                    System.out.println(a+"+"+b+"=");
61                    int c2 = in.nextInt();
62                    output.println(a+"+"+b+"="+c2);
63                    if (c2 == js.jia(a, b)) {
64                        sum += 10;
65                        System.out.println("答案正确");
66                    }
67                    else {
68                        System.out.println("答案错误");
69                    }
70                    
71                    break ;
72                case 4:
73                    System.out.println(a+"-"+b+"=");
74                    while(a<b) {
75                        a = (int) Math.round(Math.random() * 100);
76                        b = (int) Math.round(Math.random() * 100);
77                    }
78                    int c3 = in.nextInt();
79                    output.println(a+"-"+b+"="+c3);
80                    if (c3 == js.jian(a,b)) {
81                        sum += 10;
82                        System.out.println("答案正确");
83                    }
84                    else {
85                        System.out.println("答案错误");
86                    }
87                    break ;
88 
89                    } 
90         
91               }
92             System.out.println("成绩"+sum);
93             output.println("成绩:"+sum);
94             output.close();
95         }
96     }
View Code
 1 package shiyan;
 2 
 3 public class jisuanji<T> {
 4     private T a;
 5     private T b;
 6     public jisuanji() {
 7         a=null;
 8         b=null;
 9     }
10     public jisuanji(T a,T b) {
11         this.a=a;
12         this.b=b;
13     }
14     public int jia(int a,int b)
15     {
16         return a+b;
17     }
18     public int jian(int a,int b)
19     {
20         return a-b;
21     }
22     public int chen(int a,int b)
23     {
24         return a*b;
25     }
26     public int chu(int a,int b)
27     {
28         if(b!=0&&a%b==0) 
29             return a/b;
30         else 
31             return 0;
32     }
33 }
View Code

 本周实验总结:

学会了一些简单的数据结构,例如链,栈,队列,散列表等。还有Java的集合框架,集合类的内容。

复习了前面学过的知识细节,例如强制转换类型时进行判断的instanceof语句。初步体会了合作编程,互相提高的过程和乐趣。

原文地址:https://www.cnblogs.com/bmwb/p/9942232.html