張季躍 201771010139《面向對象程序設計(java)》第十一周學習總結html
第一部分:理論知識學習部分java
一.數據結構:算法
(1)線性數據結構:線性表、棧、隊列、串、數組和文件。編程
(2)非線性數據結構:樹和圖。數組
(1)線性表的邏輯結構是n個數據元素的有限序列: (a1,a 2,a3,…an) n爲線性表的長度(n≥0),n=0的表稱爲空表。 l數據結構
(2)數據元素呈線性關係:必存在惟一的稱爲「第一 個」的數據元素;必存在惟一的稱爲「後一個 」的數據元素;除第一個元素外,每一個元素都有 且只有一個前驅元素;除後一個元素外,每一個 元素都有且只有一個後繼元素。 l架構
(3)全部數據元素在同一個線性表中必須是相同的數 據類型。框架
(4)線性表按其存儲結構可分爲順序表和鏈表dom
(5)用順序存儲結構存儲的線性表稱爲 順序表 ;函數
(6)順序表將線性表中的數據元素依次存放在某個存儲區域中。 一維數組 就是用順序方式存儲的線性表。
(7)用鏈式存儲結構存儲的線性表稱爲鏈表 。
l(1)棧(Stack)也是一種特殊的 線性表,是一種後進先出 (LIFO)的結構。 l
(2)棧是限定僅在表尾進行插入 和刪除運算的線性表,表尾 稱爲棧頂(top),表頭稱爲 棧底(bottom)。 l
(3)棧的物理存儲能夠用順序存 儲結構,也能夠用鏈式存儲 結構
(1)隊列(Queue)是限定全部的插入只能在表的一端進行 ,而全部的刪除都在表的另外一端進行的線性表。 l
(2)表中容許插入的一端稱爲隊尾(Rear),容許刪除的一端稱爲隊頭(Front)。 l
(3)隊列的操做是按先進先出(FIFO)的原則進行的。 l
(4)隊列的物理存儲能夠用順序存儲結構,也能夠用鏈 式存儲結構
(1)散列表又稱爲哈希表。散列表算法的基本思想是:以結點 的關鍵字爲自變量,經過必定的函數關係(散列函數)計 算出對應的函數值,以這個值做爲該結點存儲在散列表中的地址。 l
(2)當散列表中的元素存放太滿,就必須進行再散列,將產生 一個新的散列表,全部元素存放到新的散列表中,原先的 散列表將被刪除。在Java語言中,經過負載因子(load factor)來決定什麼時候對散列表進行再散列。例如:若是負載 因子是0.75,當散列表中已經有75%的位置已經放滿,那麼 將進行再散列。 l
(3)負載因子越高(越接近1.0),內存的使用效率越高,元素的 尋找時間越長。負載因子越低(越接近0.0),元素的尋找時 間越短,內存浪費越多。
(4)HashSet類的缺省負載因子是0.75。
二.JAVA的集合框架:
(1)JAVA的集合框架實現對各類數據結構的封裝,以 下降對數據管理與處理的難度。 l
(2)所謂框架就是一個類庫的集合,框架中包含不少 超類,編程者建立這些超類的子類可較方便的設 計設計程序所需的類。例如:Swing類包 l
(3)集合(Collection或稱爲容器)是一種包含多個元素 並提供對所包含元素操做方法的類,其包含的元 素能夠由同一類型的對象組成,也能夠由不一樣類 型的對象組成。 l
(4)集合框架:JAVA集合類庫的統一架構。
(1)l集合類的做用: –Java的集合類提供了一些基本數據結構的支持。 –例如Vector、Hashtable、Stack等。
(2)集合類的使用: –Java的集合類包含在java.util包中。 –import java.util.*;
(1)–只容納對象。 注意:數組能夠容納基本數據類型數據和對象。 –若是集合類中想使用基本數據類型,又想利用 集合類的靈活性,能夠把基本數據類型數據封 裝成該數據類型的包裝器對象,而後放入集合 中處理
(2)–集合類容納的對象都是Object類的實例,一旦 把一個對象置入集合類中,它的類信息將丟失 ,這樣設計的目的是爲了集合類的通用性。 –由於Object類是全部類的祖先,因此能夠在這 些集合中存聽任何類的對象而不受限制,但切 記在使用集合成員以前必須對它從新造型
(1)l在JDK1.0和JDK1.1中提供了Vector(矢量), Hashtable(哈希表),Stack(堆棧), Properties(屬性集)等集合類,儘管這些類非 常有用,但卻彼此獨立,缺乏一個統一集中的機 制。 l
(2)在JDK1.2中,JAVA設計了一個統一的類集,並 對上述類進行了改寫,使其統一歸入JAVA的集 合框架
三.JDK1.1版本中的集合類:
(1)lVector類相似長度可變的數組。 l
(2)Vector中只能存放對象。 l
(3)Vector的元素經過下標進行訪問。 l
(4)Vector類關鍵屬性: –capacity表示集合多能容納的元素個數。 –capacityIncrement表示每次增長多少容量。 –size表示集合當前元素個數
(1)lStack類是Vector的子類。 l
(2)Stack類描述堆棧數據結構,即LIFO。 l
(3)Stack類的關鍵方法: –public void push(Object item) //把項壓入棧頂
–public Object pop()//移除棧頂對象並做爲此函數的值返回該對象
–public Object peek()//查看棧頂對象而不移除它
–public boolean empty()//測試堆棧是否爲空
(1)lHashtable經過鍵來查找元素。
(2)Hashtable用散列碼(hashcode)來肯定鍵。所 有對象都有一個散列碼,能夠經過Object類的 hashCode()方法得到
四.JDK1.2之後版本中的集合類:
集合層次中的根接口,JDK未提供這個 接口的直接實現類。
是一個有序的集合,能夠包含重複的元素。提 供了按索引訪問的方式。
不能包含重複的元素。對象可能不是按存放的 次序存放,也就是說不能像數組同樣按索引的方式進 行訪問,SortedSet是一個按照升序排列元素的Set。
包含了key-value對。Map不能包含重複的key 。 lSortedMap是一個按照升序排列key的Map
第二部分:實驗部分
一、實驗目的與要求
(1) 掌握Vetor、Stack、Hashtable三個類的用途及經常使用API;
(2) 瞭解java集合框架體系組成;
(3) 掌握ArrayList、LinkList兩個類的用途及經常使用API。
(4) 瞭解HashSet類、TreeSet類的用途及經常使用API。
(5)瞭解HashMap、TreeMap兩個類的用途及經常使用API;
(6) 結對編程(Pair programming)練習,體驗程序開發中的兩人合做。
二、實驗內容和步驟
實驗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();
}
}
測試結果:
修改測試程序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++) {
if (cats.elementAt(i) instanceof Cat) {
((Cat) cats.elementAt(i)).print();
}else {
((Dog) 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);
}
}
測試結果:
測試程序2:
l 使用JDK命令編輯運行ArrayListDemo和LinkedListDemo兩個程序,結合程序運行結果理解程序;
l 在Elipse環境下編輯運行調試教材360頁程序9-1,結合程序運行結果理解程序;
l 掌握ArrayList、LinkList兩個類的用途及經常使用API。
ArrayListDemo:
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));
}
}
}
測試結果:
LinkedListDemo:
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");
}
}
測試結果:
程序9-1:
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:
l 運行SetDemo程序,結合運行結果理解程序;
l 在Elipse環境下調試教材365頁程序9-2,結合運行結果理解程序;瞭解HashSet類的用途及經常使用API。
l 在Elipse環境下調試教材367頁-368程序9-三、9-4,結合程序運行結果理解程序;瞭解TreeSet類的用途及經常使用API。
測試程序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());
}
}
}
測試結果:
程序9-2:
package set;
import java.util.*;
/**
* This program uses a set to print all unique words in System.in.
* @version 1.12 2015-06-21
* @author Cay Horstmann
*/
public class SetTest
{
public static void main(String[] args)
{
Set<String> words = new HashSet<>(); // HashSet implements Set
long totalTime = 0;
try (Scanner in = new Scanner(System.in))
{
while (in.hasNext())
{
String word = in.next();
long callTime = System.currentTimeMillis();
words.add(word);
callTime = System.currentTimeMillis() - callTime;
totalTime += callTime;
}
}
Iterator<String> iter = words.iterator();
for (int i = 1; i <= 20 && iter.hasNext(); i++)
System.out.println(iter.next());
System.out.println(". . .");
System.out.println(words.size() + " distinct words. " + totalTime + " milliseconds.");
}
}
程序9-三、9-4:
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);
}
}
package treeSet;
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));
sortByDescription.addAll(parts);
System.out.println(sortByDescription);
}
}
測試結果:
測試程序4:
l 使用JDK命令運行HashMapDemo程序,結合程序運行結果理解程序;
l 在Elipse環境下調試教材373頁程序9-6,結合程序運行結果理解程序;
l 瞭解HashMap、TreeMap兩個類的用途及經常使用API。
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);
}
}
測試結果:
程序9-6:
package map;
/**
* A minimalist employee class for testing purposes.
*/
public class Employee
{
private String name;
private double salary;
/**
* Constructs an employee with $0 salary.
* @param n the employee name
*/
public Employee(String name)
{
this.name = name;
salary = 0;
}
public String toString()
{
return "[name=" + name + ", salary=" + salary + "]";
}
}
package map;
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
{
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"));
// print all entries
System.out.println(staff);
// remove an entry
staff.remove("567-24-2546");
// replace an entry
staff.put("456-62-5527", new Employee("Francesca Miller"));
// look up a value
System.out.println(staff.get("157-62-7935"));
// iterate through all entries
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,結合使用體驗對所運行程序提出完善建議;
合做夥伴的程序:
Main:
package 身份證;
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.List;
import java.util.Scanner;
public class Main{
private static ArrayList<student> studentlist;
public static void main(String[] args) {
studentlist = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
File file = new File("C:\\Users\\身份證號.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
String temp = null;
while ((temp = in.readLine()) != null) {
Scanner linescanner = new Scanner(temp);
linescanner.useDelimiter(" ");
String name = linescanner.next();
String number = linescanner.next();
String sex = linescanner.next();
String year = linescanner.next();
String province =linescanner.nextLine();
student student = student();
student.setname(name);
student.setnumber(number);
student.setsex(sex);
int a = Integer.parseInt(year);
student.setyear(year);
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("1.按姓名字典序輸出人員信息:");
System.out.println("2.查詢最大年齡的人員信息:");
System.out.println("3.查詢最小年齡人員信息:");
System.out.println("4.輸入你的年齡,查詢身份證號.txt中年齡與你最近人的信息:");
System.out.println("5.查詢人員中是否有你的同鄉:");
int nextInt = scanner.nextInt();
switch (nextInt) {
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).getyear();
if(j>MAX)
{
MAX=j;
k1=i;
}
System.out.println("年齡最大:"+ studentlist.get(k1));
}
break;
case 3:
int max=0,min=100;int L,m1 = 0,m2=0;
for(int i=1;i< studentlist.size();i++)
{
L= studentlist.get(i).getyear();
if(L>max)
{
max=L;
m1=i;
}
if(L<min)
{
min=L;
m2=i;
}
}
System.out.println("年齡最小:"+ studentlist.get(m2));
break;
case 4:
System.out.println("province?");
String find = scanner.next();
String province=find.substring(0,3);
String province2=find.substring(0,3);
for (int i = 0; i < studentlist.size(); i++)
{
if( studentlist.get(i).getprovince().substring(1,4).equals(province))
System.out.println(studentlist.get(i));
}
break;
case 5:
System.out.println("年齡:");
int yourage = scanner.nextInt();
int near=yearnear(yourage);
int d_value=yourage-studentlist.get(near).getyear();
System.out.println(""+studentlist.get(near));
/* for (int i = 0; i < Peoplelist.size(); i++)
{
int p=Personlist.get(i).getage()-yourage;
if(p<0) p=-p;
if(p==d_value) System.out.println(Peoplelist.get(i));
} */
break;
case 6:
isTrue = false;
System.out.println("退出程序!");
break;
default:
System.out.println("輸入有誤");
}
}
}
private static student student() {
// TODO Auto-generated method stub
return null;
}
public static int yearnear(int year) {
int min=25,d_value=0,k=0;
for (int i = 0; i < studentlist.size(); i++)
{
d_value= studentlist.get(i).getyear()-year;
if(d_value<0) d_value=-d_value;
if (d_value<min)
{
min=d_value;
k=i;
}
} return k;
}
}
Student:
package 身份證;
public abstract class student implements Comparable<student> {
private String name;
private String number ;
private String sex ;
private String year;
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 String getyaer()
{
return year;
}
public void setyear(String year )
{
this.year=year ;
}
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"+year+"\t"+number+"\t"+province+"\n";
}
public int getyear()
{
// TODO Auto-generated method stub
return 0;
}
public void setname(String name2)
{
// TODO Auto-generated method stub
}
}
測試結果:
完善建議:
程序運行無明顯錯誤,但程序行與行之間空格太大,顯得程序太長,建議再從新將程序排列一下。
l 各自運行合做夥伴實驗十編程練習2,結合使用體驗對所運行程序提出完善建議;
合做夥伴的程序:
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;
public class Test{
int sum;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = null;
try {
out = new PrintWriter("C:\\Users\\Administrator\\Desktop\\test.txt");
} catch (FileNotFoundException e) {
System.out.println("文件夾輸出失敗");
e.printStackTrace();
}
Test t=new Test();
System.out.println("本次測試共十道題,每題十分,滿分一百分");
t.sum=0;
Random r=new Random();
for(int i=0;i<10;i++) {
t.core();
}
System.out.println("考試結束");
System.out.println("你的總分爲"+t.sum);
}
private void core() {
Random r=new Random();
int m,n;
m=r.nextInt(11);
n=m%4;
switch(n) {
case 0:
int a ,b,c;
a=r.nextInt(101);
b=r.nextInt(101);
System.out.println(a+"+"+"("+b+")"+"=");
Scanner x=new Scanner(System.in);
c=x.nextInt();
if(c!=a+b)
System.out.println("回答錯誤");
else {
System.out.println("回答正確");
sum=sum+10;
}
break;
case 1:
int h,g,f;
h=r.nextInt(101);
g=r.nextInt(101);
System.out.println(h+"-"+"("+g+")"+"= ");
Scanner u=new Scanner(System.in);
f=u.nextInt();
if(f!=h-g)
System.out.println("回答錯誤");
else {
System.out.println("回答正確");
sum=sum+10;
}
break;
case 2:
int q,w,e;
q=r.nextInt(101);
w=r.nextInt(101);
System.out.println(q+"*"+"("+w+")"+"= ");
Scanner y=new Scanner(System.in);
e=y.nextInt();
if(e!=q*w)
System.out.println("回答錯誤");
else {
System.out.println("回答正確");
sum=sum+10;
}
break;
case 3:
int j,k,l;
j=r.nextInt(101);
k=r.nextInt(101);
if(k==0)
k++;
System.out.println(j+"/"+"("+k+")"+"= ");
Scanner z=new Scanner(System.in);
l=z.nextInt();
if(l!=(j/k)/1.00)
System.out.println("回答錯誤");
else {
System.out.println("回答正常");
sum=sum+10;
}
break;
}
}
}
測試結果:
完善建議:
行數太多,但程序總數少,建議簡化程序。
l 採用結對編程方式,與學習夥伴合做完成實驗九編程練習1;
測試程序:
package 實驗3;
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.Scanner;
import java.util.Collections;
public class 實驗 {
public static People findPeopleByname(String name) {
People flag = null;
for (People people : peoplelist) {
if(people.getName().equals(name)) {
flag = people;
}
}
return flag;
}
public static People findPeopleByid(String id) {
People flag = null;
for (People people : peoplelist) {
if(people.getnumber().equals(id)) {
flag = people;
}
}
return flag;
}
private static ArrayList<People> agenear(int yourage) {
// TODO Auto-generated method stub
int j=0,min=53,d_value=0,k = 0;
ArrayList<People> plist = new ArrayList<People>();
for (int i = 0; i < peoplelist.size(); i++) {
d_value = peoplelist.get(i).getage() > yourage ?
peoplelist.get(i).getage() - yourage : yourage - peoplelist.get(i).getage() ;
k = d_value < min ? i : k;
min = d_value < min ? d_value : min;
}
for(People people : peoplelist) {
if(people.getage() == peoplelist.get(k).getage()) {
plist.add(people);
}
}
return plist;
}
private static ArrayList<People> peoplelist;
public static void main(String[] args) {
peoplelist = new ArrayList<People>();
Scanner scanner = new Scanner(System.in);
File file = new File("C:\\Users\\張季躍\\Desktop\\第十週實驗報告\\身份證號.txt");
try {
FileInputStream files = new FileInputStream(file);
BufferedReader in = new BufferedReader(new InputStreamReader(files));
String temp = null;
while ((temp = in.readLine()) != null) {
String[] information = temp.split("[ ]+");
People people = new People();
people.setName(information[0]);
people.setnumber(information[1]);
int A = Integer.parseInt(information[3]);
people.setage(A);
people.setsex(information[2]);
for(int j = 4; j<information.length;j++) {
people.setplace(information[j]);
}
peoplelist.add(people);
}
} 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.查詢身份證號.txt中年齡與輸入年齡最近的人");
System.out.println(" 5.查詢人員中是否有輸入地址的同鄉");
System.out.println(" 6.退出");
System.out.println("******************************************");
int nextInt = scanner.nextInt();
switch (nextInt) {
case 1:
Collections.sort(peoplelist);
System.out.println(peoplelist.toString());
break;
case 2:
int max=0;
int j,k1 = 0;
for(int i=1;i<peoplelist.size();i++)
{
j = peoplelist.get(i).getage();
if(j>max)
{
max = j;
k1 = i;
}
}
System.out.println("年齡最大:"+peoplelist.get(k1));
break;
case 3:
int min = 100;
int j1,k2 = 0;
for(int i=1;i<peoplelist.size();i++)
{
j1 = peoplelist.get(i).getage();
if(j1<min)
{
min = j1;
k2 = i;
}
}
System.out.println("年齡最小:"+peoplelist.get(k2));
break;
case 4:
System.out.println("年齡:");
int input_age = scanner.nextInt();
ArrayList<People> plist = new ArrayList<People>();
plist = agenear(input_age);
for(People people : plist) {
System.out.println(people.toString());
}
break;
case 5:
System.out.println("請輸入省份");
String find = scanner.next();
for (int i = 0; i <peoplelist.size(); i++)
{
String [] place = peoplelist.get(i).getplace().split("\t");
for(String temp : place) {
if(find.equals(temp)) {
System.out.println(" "+peoplelist.get(i));
break;
}
}
}
break;
case 6:
isTrue = false;
System.out.println("再見!");
break;
default:
System.out.println("輸入有誤");
}
}
}
}
package 實驗3;
public class People implements Comparable<People> {
private String name = null;
private String number = null;
private int age = 0;
private String sex = null;
private String place = null;
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 int getage()
{
return age;
}
public void setage(int age )
{
this.age = age;
}
public String getsex()
{
return sex;
}
public void setsex(String sex )
{
this.sex = sex;
}
public String getplace()
{
return place;
}
public void setplace(String place)
{
if(this.place == null) {
this.place = place;
}else {
this.place = this.place+ "\t" +place;
}
}
public int compareTo(People o)
{
return this.name.compareTo(o.getName());
}
public String toString()
{
return name+"\t"+sex+"\t"+age+"\t"+number+"\t"+place+"\n";
}
}
實驗結果:
l 採用結對編程方式,與學習夥伴合做完成實驗十編程練習2。
測試程序:
import java.util.Random;
import java.util.Scanner;
public class 實驗3{
int sum;
public static void main(String[] args) {
實驗3 t=new 實驗3();
System.out.println("考試開始");
t.sum=0;
Random r=new Random();
for(int i=0;i<10;i++) {
t.core();
}
System.out.println("考試結束");
System.out.println("你的總分爲"+t.sum);
}
private void core() {
Random r=new Random();
int m,n;
m=r.nextInt(11);
n=m%4;
switch(n) {
case 0:
int a ,b,c;
a=r.nextInt(101);
b=r.nextInt(101);
System.out.println(a+"+"+"("+b+")"+"=");
Scanner x=new Scanner(System.in);
c=x.nextInt();
if(c!=a+b)
System.out.println("計算失誤");
else {
System.out.println("計算正確");
sum=sum+10;
}
break;
case 1:
int h,g,f;
h=r.nextInt(101);
g=r.nextInt(101);
System.out.println(h+"-"+"("+g+")"+"= ");
Scanner u=new Scanner(System.in);
f=u.nextInt();
if(f!=h-g)
System.out.println("計算失誤");
else {
System.out.println("計算正確");
sum=sum+10;
}
break;
case 2:
int q,w,e;
q=r.nextInt(101);
w=r.nextInt(101);
System.out.println(q+"*"+"("+w+")"+"= ");
Scanner y=new Scanner(System.in);
e=y.nextInt();
if(e!=q*w)
System.out.println("計算失誤");
else {
System.out.println("計算正確");
sum=sum+10;
}
break;
case 3:
double j,k,l;
j=r.nextInt(101);
k=r.nextInt(101);
if(k==0)
k++;
System.out.println(j+"/"+"("+k+")"+"= ");
Scanner z=new Scanner(System.in);
l=z.nextDouble();
if(l!=(j/k)/1.00)
System.out.println("計算失誤");
else {
System.out.println("計算正確");
sum=sum+10;
}
break;
}
}
}
實驗結果:
第三部分:實驗總結:
這一週我主要學習了Java中的數據結構和Java中有關集合的知識,經過和數據結構可的相互驗證,關於Java中的數據結構我有了初步的瞭解,但對於集合我感受仍是有不會的東西須要請教同窗,並且,這一週經過結對編程練習,我對於前幾周編寫的程序有了更深刻的瞭解,鞏固了之前學習的知識。