一:理論部分。html
1.數據結構:分爲a.線性數據結構,如線性表、棧、隊列、串、數組和文件。java
b.非線性數據結構,如樹和圖。編程
1)全部數據元素在同一個線性表中必須是相同的數據類型。數組
線性表按其存儲結構可分爲順序表和鏈表。數據結構
2)棧:也是一種特殊的線性表,是一種後進先出(LIFO)的結構。架構
棧是限定僅在表尾進行插入和刪除運算的線性表,表尾稱爲棧頂,表頭稱爲棧底。框架
3)隊列:限定全部的插入只能在表的一端進行,而全部的刪除都在表的另外一端進行的線性表。是一種先進先出(FIFO)的結構。dom
表中容許插入的一端稱爲隊尾,容許刪除的一端稱爲隊頭。oop
2.集合:(容器)是一種包含多個元素並提供對所包含元素操做方法的類,其包含的元素能夠由同一類型的對象組成,也能夠由不一樣類型的對象組成。學習
1)集合框架:JAVA集合類庫的統一架構。
2)集合類的做用(包含在java.util包中):提供一些基本數據結構的支持,如Vector、Hashtable、Stack等。
3)集合類的特色:a.只容納對象;
b.集合類容納的對象都是Object類的實例(一旦把一個對象置入集合類中,它的類信息將丟失)
4)Vector類:相似長度可變的數組。它只能存放對象,其元素經過下標進行訪問。
5)Stack類(Vector的子類):它描述堆棧數據結構。(全部對象都有一個散列碼,能夠經過Object類的hashCode方法得到。)
3.集合框架中的基本接口:a.Collection(構造類集框架的基礎):集合層次中的根接口,JDK未提供這個接口的直接實現類。
b.Set:不能包含重複的元素,即元素必須惟一。對象可能不是按存放的次序存放。(實 現 Set 接口的類有HashSet,TreeSet)
c.List:有序的集合,能夠包含重複的元素。提供了按索引訪問的方式。實現它的類有ArrayList和LinkedLis(如ArrayList:可以自動增加容量的數組)
d.Map:Map接口映射惟一關鍵字到值。包含了key-value對。Map不能包含重複的key。SortedMap是一個按照升序排列key的Map。
二:實驗部分。
1、實驗目的與要求
(1) 掌握Vetor、Stack、Hashtable三個類的用途及經常使用API;
(2) 瞭解java集合框架體系組成;
(3) 掌握ArrayList、LinkList兩個類的用途及經常使用API。
(4) 瞭解HashSet類、TreeSet類的用途及經常使用API。
(5)瞭解HashMap、TreeMap兩個類的用途及經常使用API;
(6) 結對編程(Pair programming)練習,體驗程序開發中的兩人合做。
2、實驗內容和步驟
實驗1: 導入第9章示例程序,測試程序並進行代碼註釋。
測試程序1:
l 使用JDK命令運行編輯、運行如下三個示例程序,結合運行結果理解程序;
l 掌握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();//進行強制類型轉化 } }
程序運行結果以下:
由程序運行結果可知,程序在強制轉換類型時出現異常,更改後程序以下:
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++) if (cats.elementAt(i) instanceof Cat) //判斷是否能進行強制類型轉換 { ((Cat) cats.elementAt(i)).print();//能進行強制類型轉換,輸出爲Cat型 } else { ((Dog) cats.elementAt(i)).print();//不能進行強制類型轉化,輸出爲Dog型 } } }
程序運行結果以下:
示例程序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;//不加權限修飾符:friendly型 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));//生成0到20(不包括20)的整型隨機數 if (ht.containsKey(r))//判斷r是不是哈希表中一個元素的鍵值 ((Counter) ht.get(r)).i++;//經過get方法得到其值 else ht.put(r, new Counter());//ht不存在 } System.out.println(ht); } }
程序運行結果以下:
測試程序2:
使用JDK命令編輯運行ArrayListDemo和LinkedListDemo兩個程序,結合程序運行結果理解程序;
ArrayListDemo:
import java.util.*; public class ArrayListDemo//ArrayList使用了數組的實現 { public static void main(String[] argv) { ArrayList al = new ArrayList(); //在ArrayList中添加大量元素 al.add(new Integer(11)); al.add(new Integer(12)); al.add(new Integer(13)); al.add(new String("hello"));//下標從0開始,添加4個元素 // 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)); } } }
程序運行結果以下:
LinkedListDemo:
程序運行結果以下:
l 在Elipse環境下編輯運行調試教材360頁程序9-1,結合程序運行結果理解程序;
l 掌握ArrayList、LinkList兩個類的用途及經常使用API。
程序以下:
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) { //建立a和b兩個鏈表 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"); //合併a和b中的詞 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); //從第二個鏈表中每隔一個元素刪除一個元素 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);//經過AbstractCollection類中的toString方法打印出鏈表a中的全部元素 } }
程序運行結果以下:
測試程序3:
l 運行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"); // 複製 h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
程序運行結果以下:
l 在Elipse環境下調試教材367頁-368程序9-3、9-4,結合程序運行結果理解程序;瞭解TreeSet類的用途及經常使用API。
9—3:
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); } }
程序運行結果以下:
9—4:
import java.util.*; /** * This program sorts a set of item by comparing their descriptions. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class TreeSetTest { public static void main(String[] args) { SortedSet<Item> parts = new TreeSet<>(); parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912)); System.out.println(parts); NavigableSet<Item> sortByDescription = new TreeSet<>( Comparator.comparing(Item::getDescription));//把自定義類對象存入TreeSet進行排序 sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
程序運行結果如圖:
測試程序4:
l 使用JDK命令運行HashMapDemo程序,結合程序運行結果理解程序;
程序以下:
import java.util.*; public class HashMapDemo //基於哈希表的 Map接口的實現,提供全部可選的映射操做 { public static void main(String[] argv) { HashMap h = new HashMap(); // 哈希映射從公司名稱到地址 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); } }
程序運行結果:
l 在Elipse環境下調試教材373頁程序9-6,結合程序運行結果理解程序;
瞭解HashMap、TreeMap兩個類的用途及經常使用API。
程序以下:
import java.util.*; /** * This program demonstrates the use of a map with key type String and value type Employee. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class MapTest//Map在有映射關係時,能夠優先考慮 { public static void main(String[] args) { Map<String, Employee> staff = new HashMap<>(); staff.put("144-25-5464", new Employee("Amy Lee")); staff.put("567-24-2546", new Employee("Harry Hacker")); staff.put("157-62-7935", new Employee("Gary Cooper")); staff.put("456-62-5527", new Employee("Francesca Cruz")); // 打印全部條目 System.out.println(staff); // 刪除一個條目 staff.remove("567-24-2546"); // 替換一個條目 staff.put("456-62-5527", new Employee("Francesca Miller")); // 查找一個值 System.out.println(staff.get("157-62-7935")); // 遍歷全部條目 staff.forEach((k, v) -> System.out.println("key=" + k + ", value=" + v)); } }
程序運行結果以下:
實驗2:結對編程練習:
l 關於結對編程:如下圖片是一個結對編程場景:兩位學習夥伴坐在一塊兒,面對着同一臺顯示器,使用着同一鍵盤,同一個鼠標,他們一塊兒思考問題,一塊兒分析問題,一塊兒編寫程序。
l 關於結對編程的闡述可參見如下連接:
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,結合使用體驗對所運行程序提出完善建議;
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Test{ private static ArrayList<Person> Personlist1; public static void main(String[] args) { Personlist1 = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("C:\\Users\\lenovo\\Documents\\身份證"); try { FileInputStream F = new FileInputStream(file); BufferedReader in = new BufferedReader(new InputStreamReader(F)); String temp = null; while ((temp = in.readLine()) != null) { Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String id = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String place =linescanner.nextLine(); Person Person = new Person(); Person.setname(name); Person.setid(id); Person.setsex(sex); int a = Integer.parseInt(age); Person.setage(a); Person.setbirthplace(place); Personlist1.add(Person); } } catch (FileNotFoundException e) { System.out.println("查找不到信息"); e.printStackTrace(); } catch (IOException e) { System.out.println("信息讀取有誤"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("1:按姓名字典序輸出人員信息;"); System.out.println("2:查詢最大年齡與最小年齡人員信息;"); System.out.println("3.輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地"); System.out.println("4:按省份找你的同鄉;"); System.out.println("5:退出"); int type = scanner.nextInt(); switch (type) { case 1: Collections.sort(Personlist1); System.out.println(Personlist1.toString()); break; case 2: int max=0,min=100;int j,k1 = 0,k2=0; for(int i=1;i<Personlist1.size();i++) { j=Personlist1.get(i).getage(); if(j>max) { max=j; k1=i; } if(j<min) { min=j; k2=i; } } System.out.println("年齡最大:"+Personlist1.get(k1)); System.out.println("年齡最小:"+Personlist1.get(k2)); break; case 3: System.out.println("place?"); String find = scanner.next(); String place=find.substring(0,3); String place2=find.substring(0,3); for (int i = 0; i <Personlist1.size(); i++) { if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) { System.out.println("你的同鄉:"+Personlist1.get(i)); } } break; case 4: System.out.println("年齡:"); int yourage = scanner.nextInt(); int close=ageclose(yourage); int d_value=yourage-Personlist1.get(close).getage(); System.out.println(""+Personlist1.get(close)); break; case 5: isTrue = false; System.out.println("再見!"); break; default: System.out.println("輸入有誤"); } } } public static int ageclose(int age) { int m=0; int max=53; int d_value=0; int k=0; for (int i = 0; i < Personlist1.size(); i++) { d_value=Personlist1.get(i).getage()-age; if(d_value<0) d_value=-d_value; if (d_value<max) { max=d_value; k=i; } } return k; } }
public class Person implements Comparable<Person> { private String name; private String id; private int age; private String sex; private String birthplace; public String getname() { return name; } public void setname(String name) { this.name = name; } public String getid() { return id; } public void setid(String id) { this.id= id; } public int getage() { return age; } public void setage(int age) { // int a = Integer.parseInt(age); this.age= age; } public String getsex() { return sex; } public void setsex(String sex) { this.sex= sex; } public String getbirthplace() { return birthplace; } public void setbirthplace(String birthplace) { this.birthplace= birthplace; } public int compareTo(Person o) { return this.name.compareTo(o.getname()); } public String toString() { return name+"\t"+sex+"\t"+age+"\t"+id+"\t"; } }
各自運行合做夥伴實驗十編程練習2,結合使用體驗對所運行程序提出完善建議;
package Demo; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner in = new Scanner(System.in); Number counter = new Number(); PrintWriter out = null; try { out = new PrintWriter("test.txt"); } catch (FileNotFoundException e) { System.out.println("Error!"); e.printStackTrace(); } int sum = 0; for (int i = 1; i <= 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m; Random rand = new Random(); m = (int) rand.nextInt(4) + 1; switch (m) { case 1: a = b + (int) Math.round(Math.random() * 100); while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } //a大於b,a%b爲0(保證能整除) System.out.println(i + ": " + a + "/" + b + "="); int c0 = in.nextInt(); out.println(a + "/" + b + "=" + c0); if (c0 == counter.division(a, b)) { sum += 10; System.out.println("恭喜答案正確!"); } else { System.out.println("抱歉答案錯誤!"); } break; case 2: System.out.println(i + ": " + a + "*" + b + "="); int c = in.nextInt(); out.println(a + "*" + b + "=" + c); if (c == counter.multiplication(a, b)) { sum += 10; System.out.println("恭喜答案正確!"); } else { System.out.println("抱歉答案錯誤!"); } break; case 3: System.out.println(i + ": " + a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == counter.add(a, b)) { sum += 10; System.out.println("恭喜答案正確!"); } else { System.out.println("抱歉答案錯誤!"); } break; case 4: while (a < b) { b = (int) Math.round(Math.random() * 100); } //若a<b,則從新生成b(避免出現負數) System.out.println(i + ": " + a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == counter.reduce(a, b)) { sum += 10; System.out.println("恭喜答案正確!"); } else { System.out.println("抱歉答案錯誤!"); } break; } } System.out.println("成績" + sum); out.println("成績:" + sum); out.close(); } }
package Demo; public class Number<T> { private T a; private T b; public Number() { a = null; b = null; } public Number(T a, T b) { this.a = a; this.b = b; } public int add(int a,int b) { return a + b; } public int reduce(int a, int b) { return a - b; } public int multiplication(int a, int b) { return a * b; } public int division(int a, int b) { if (b != 0 && a%b==0) return a / b; else return 0; } }
在運行對方的程序時,大部分代碼差很少,但在她的程序中有部分代碼比個人要簡潔,值得我在之後的實驗中學習。
採用結對編程方式,與學習夥伴合做完成實驗九編程練習1;
package xinxi; public class Student implements Comparable<Student> { private String name; private String number ; private String sex ; private int age; private String province; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getnumber() { return number; } public void setnumber(String number) { this.number = number; } public String getsex() { return sex ; } public void setsex(String sex ) { this.sex =sex ; } public int getage() { return age; } public void setage(int age) { this.age= age; } public String getprovince() { return province; } public void setprovince(String province) { this.province=province ; } public int compareTo(Student o) { return this.name.compareTo(o.getName()); } public String toString() { return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n"; } }
package xinxi; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class xinxi{ private static ArrayList<Student> studentlist; public static void main(String[] args) { studentlist = new ArrayList<>(); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); File file = new File("F:\\身份證號.txt"); try { FileInputStream fis = new FileInputStream(file); @SuppressWarnings("resource") BufferedReader in = new BufferedReader(new InputStreamReader(fis)); String temp = null; while ((temp = in.readLine()) != null) { @SuppressWarnings("resource") Scanner linescanner = new Scanner(temp); linescanner.useDelimiter(" "); String name = linescanner.next(); String number = linescanner.next(); String sex = linescanner.next(); String age = linescanner.next(); String province =linescanner.nextLine(); Student student = new Student(); student.setName(name); student.setnumber(number); student.setsex(sex); int a = Integer.parseInt(age); student.setage(a); student.setprovince(province); studentlist.add(student); } } catch (FileNotFoundException e) { System.out.println("學生信息文件找不到"); e.printStackTrace(); } catch (IOException e) { System.out.println("學生信息文件讀取錯誤"); e.printStackTrace(); } boolean isTrue = true; while (isTrue) { System.out.println("輸入序號進行相應的操做"); System.out.println("1.字典排序"); System.out.println("2.輸出年齡最大和年齡最小的人"); System.out.println("3.查找與你年齡相近的人"); System.out.println("4.查找你的同鄉"); String n = scanner.next(); switch (n) { case "1": Collections.sort(studentlist); System.out.println(studentlist.toString()); break; case "2": int max=0,min=100; int j,k1 = 0,k2=0; for(int i=1;i<studentlist.size();i++) { j=studentlist.get(i).getage(); if(j>max) { max=j; k1=i; } if(j<min) { min=j; k2=i; } } System.out.println("年齡最大:"+studentlist.get(k1)); System.out.println("年齡最小:"+studentlist.get(k2)); break; case "3": System.out.println("年齡:"); int yourage = scanner.nextInt(); int near=agenear(yourage); @SuppressWarnings("unused") int value=yourage-studentlist.get(near).getage(); System.out.println("和你年齡相近的是"+studentlist.get(near)); break; case "4": System.out.println("輸入你的家鄉"); String find = scanner.next(); String place=find.substring(0,3); for (int i = 0; i <studentlist.size(); i++) { if(studentlist.get(i).getprovince().substring(1,4).equals(place)) System.out.println("你的同鄉是 "+studentlist.get(i)); } break; } } } public static int agenear(int age) { @SuppressWarnings("unused") int j=0,min=53,value=0,k=0; for (int i = 0; i < studentlist.size(); i++) { value=studentlist.get(i).getage()-age; if(value<0) value=-value; if (value<min) { min=value; k=i; } } return k; } }
l 採用結對編程方式,與學習夥伴合做完成實驗十編程練習2。
package jisuan; import java.io.*; import java.io.PrintWriter; import java.util.Scanner; public class Jisuan { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter output = null; try { output = new PrintWriter("text.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } int sum = 0; for (int i = 1; i <= 10; i++) { int a = (int) Math.round(Math.random() * 100); int b = (int) Math.round(Math.random() * 100); int m = (int) Math.round(Math.random() * 4); switch (m) { case 1: while (b == 0) { b = (int) Math.round(Math.random() * 100); } while (a % b != 0) { a = (int) Math.round(Math.random() * 100); } System.out.println(a + "/" + b + "="); int c1 = in.nextInt(); output.println(a + "/" + b + "=" + c1); if (c1 == a / b) { System.out.println("恭喜答案正確"); sum += 10; } else { System.out.println("抱歉,答案錯誤"); } break; case 2: System.out.println( a + "*" + b + "="); int c2 = in.nextInt(); output.println(a + "*" + b + "=" + c2); if (c2 == a * b) { System.out.println("恭喜答案正確"); sum += 10; } else { System.out.println("抱歉,答案錯誤"); } break; case 3: System.out.println( a + "+" + b + "="); int c3 = in.nextInt(); output.println(a + "+" + b + "=" + c3); if (c3 == a + b) { System.out.println("恭喜答案正確"); sum += 10; } else { System.out.println("抱歉,答案錯誤"); } break; case 4: while (a < b) { a = (int) Math.round(Math.random() * 100); } System.out.println( a + "-" + b + "="); int c4 = in.nextInt(); output.println(a + "-" + b + "=" + c4); if (c4 == a - b) { System.out.println("恭喜答案正確"); sum += 10; } else { System.out.println("抱歉,答案錯誤"); } break; } } System.out.println("成績" + sum); output.println("成績" + sum); output.close(); } }
package jisuan; public class math<T> { private T a; private T b; public int add(int a, int b) { return a + b; } public int reduce(int a, int b) { return a - b; } public int multiplication(int a, int b) { return a * b; } public int division(int a, int b) { if (b != 0 && a % b == 0) return a / b; else return 0; } }
實驗總結:
經過本週學習,我進一步複習了一些有關數據結構的知識,另外初步瞭解了java集合類,也瞭解了Vector類,Stack類以及Hashtable類。除此之外,這次實驗第一次採用結對編程的方法,經過和合做夥伴互相運行程序,相互討論交流,從中學到了不少東西。但願之後多采起這種方法,在合做中互相學習進步。