201771010102 常惠琢 《2018面向对象程序设计(Java)》第11周学习总结
Posted hongyanohongyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了201771010102 常惠琢 《2018面向对象程序设计(Java)》第11周学习总结相关的知识,希望对你有一定的参考价值。
实验十一 集合
实验时间 2018-11-8
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(); } } |
//示例程序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 import java.util.Vector; 2 3 public class CatsAndDogs { 4 public static void main(String[] args) { 5 Vector cats = new Vector(); 6 for (int i = 0; i < 7; i++) 7 cats.addElement(new Cat(i)); 8 cats.addElement(new Dog(7)); 9 for (int i = 0; i < cats.size(); i++) 10 ((Cat) cats.elementAt(i)).print(); 11 } 12 }
1 import java.util.Vector; 2 3 class Cat { 4 private int catNumber; 5 6 Cat(int i) { 7 catNumber = i; 8 } 9 10 void print() { 11 System.out.println("Cat #" + catNumber); 12 } 13 }
1 class Dog { 2 private int dogNumber; 3 4 Dog(int i) { 5 dogNumber = i; 6 } 7 8 void print() { 9 System.out.println("Dog #" + dogNumber); 10 } 11 }
示例程序1异常处理:
1 import java.util.Vector; 2 3 class Cat { 4 private int catNumber; 5 6 Cat(int i) { 7 catNumber = i; 8 } 9 10 void print() { 11 System.out.println("Cat #" + catNumber); 12 } 13 } 14 15 class Dog { 16 private int dogNumber; 17 18 Dog(int i) { 19 dogNumber = i; 20 } 21 22 void print() { 23 System.out.println("Dog #" + dogNumber); 24 } 25 } 26 27 public class CatsAndDogs { 28 public static void main(String[] args) { 29 Vector cats = new Vector(); 30 for (int i = 0; i < 7; i++) 31 cats.addElement(new Cat(i)); 32 cats.addElement(new Dog(7)); 33 for (int i = 0; i < cats.size(); i++) 34 if (cats.elementAt(i) instanceof Cat) { 35 ((Cat) cats.elementAt(i)).print(); 36 } 37 else { 38 ((Dog) cats.elementAt(i)).print(); 39 } 40 } 41 }
示例程序2:
1 import java.util.*; 2 3 public class Stacks { 4 static String[] months = { "1", "2", "3", "4" }; 5 6 public static void main(String[] args) { 7 Stack stk = new Stack(); 8 for (int i = 0; i < months.length; i++) 9 stk.push(months[i]); 10 System.out.println(stk); 11 System.out.println("element 2=" + stk.elementAt(2)); 12 while (!stk.empty()) 13 System.out.println(stk.pop()); 14 } 15 }
1 import java.util.Hashtable; 2 3 public class Statistics { 4 public static void main(String[] args) { 5 Hashtable ht = new Hashtable(); 6 for (int i = 0; i < 10000; i++) { 7 Integer r = new Integer((int) (Math.random() * 20)); 8 if (ht.containsKey(r)) 9 ((Counter) ht.get(r)).i++; 10 else 11 ht.put(r, new Counter()); 12 } 13 System.out.println(ht); 14 } 15 }
1 import java.util.*; 2 3 class Counter { 4 int i = 1; 5 6 public String toString() { 7 return Integer.toString(i); 8 } 9 }
1 import java.util.*; 2 3 class Counter { 4 int i = 1;//i未加访问权限修饰符。权限修饰符共有四种权限修饰符,分别是public,protected,默认和private。 5 6 public String toString() { 7 return Integer.toString(i); 8 } 9 } 10 11 public class Statistics { 12 public static void main(String[] args) { 13 Hashtable ht = new Hashtable(); //创建了一个哈希表类对象 14 for (int i = 0; i < 10000; i++) { 15 Integer r = new Integer((int) (Math.random() * 20)); //生成20个整型随机数 16 if (ht.containsKey(r))//通过ht调用containsKey的方法 17 ((Counter) ht.get(r)).i++;//通过Counter类对象引用i的属性 18 else 19 ht.put(r, new Counter());//生成了一个Counter类对象,属性值为初始值。 20 } 21 System.out.println(ht); 22 } 23 }
测试程序2:
l 使用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"); } } |
1 import java.util.*; 2 3 public class ArrayListDemo { 4 public static void main(String[] argv) { 5 ArrayList al = new ArrayList(); 6 // 向ARARYLIST中添加大量元素。 7 al.add(new Integer(11)); 8 al.add(new Integer(12)); 9 al.add(new Integer(13)); 10 al.add(new String("hello")); 11 // 首先用一个for循环打印出来。 12 System.out.println("Retrieving by index:"); 13 for (int i = 0; i < al.size(); i++) { 14 System.out.println("Element " + i + " = " + al.get(i)); 15 } 16 } 17 }
1 import java.util.*; 2 public class LinkedListDemo { 3 public static void main(String[] argv) { 4 LinkedList l = new LinkedList(); 5 l.add(new Object()); 6 l.add("Hello"); 7 l.add("zhangsan"); 8 ListIterator li = l.listIterator(0); 9 while (li.hasNext()) 10 System.out.println(li.next()); 11 if (l.indexOf("Hello") < 0) 12 System.err.println("Lookup does not work"); 13 else 14 System.err.println("Lookup works"); 15 } 16 }
1 import java.util.*; 2 3 public class ArrayListDemo { 4 public static void main(String[] argv) { 5 ArrayList al = new ArrayList(); 6 // 向ARARYLIST中添加大量元素。 7 al.add(new Integer(11)); 8 al.add(new Integer(12)); 9 al.add(new Integer(13)); 10 al.add(new String("hello"));//集合中的元素可以是不同类对象 11 System.out.println(al.size()); 12 // 首先用一个for循环打印出来。 13 System.out.println("Retrieving by index:"); 14 for (int i = 0; i < al.size(); i++) { 15 System.out.println("Element " + i + " = " + al.get(i)); 16 } 17 } 18 }
1 import java.util.*; 2 public class LinkedListDemo { 3 public static void main(String[] argv) { 4 LinkedList l = new LinkedList(); 5 l.add(new Object()); 6 l.add("Hello"); 7 l.add("zhangsan"); 8 ListIterator li = l.listIterator(0); 9 while (li.hasNext()) 10 System.out.println(li.next()); 11 if (l.indexOf("Hello") < 0) //是否为l中的索引值 12 System.err.println("Lookup does not work"); 13 else 14 System.err.println("Lookup works"); 15 } 16 }
l 在Elipse环境下编辑运行调试教材360页程序9-1,结合程序运行结果理解程序;
l 掌握ArrayList、LinkList两个类的用途及常用API。
1 package linkedList; 2 3 import java.util.*; 4 5 /** 6 * 这个程序演示了链表上的操作 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 //将单词从B合并为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(); // 跳过一个元素 44 if (bIter.hasNext()) 45 { 46 bIter.next(); // 跳过下一元素 47 bIter.remove(); // 删除该元素 48 } 49 } 50 51 System.out.println(b); 52 53 //批量操作:从A中删除B中的所有单词 54 55 a.removeAll(b); 56 57 System.out.println(a); 58 } 59 }
测试程序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"); // DUPLICATE h.add("Three"); Iterator it = h.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } |
1 import java.util.HashSet; 2 import java.util.Iterator; 3 public class SetDemo { 4 public static void main(String[] argv) { 5 HashSet h = new HashSet(); //也可以 Set h=new HashSet() 6 h.add("One"); 7 h.add("Two"); 8 h.add("One"); // DUPLICATE 重复; 复制; 复印; 打印; 9 h.add("Three"); 10 Iterator it = h.iterator(); 11 while (it.hasNext()) { 12 System.out.println(it.next()); 13 } 14 } 15 }
l 在Elipse环境下调试教材365页程序9-2,结合运行结果理解程序;了解HashSet类的用途及常用API。
1 package set; 2 3 import java.util.*; 4 5 /** 6 *这个程序使用一个集合来打印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<>(); //哈希集实现集 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 import java.util.*; 2 3 /** 4 * 该程序通过比较它们的描述对一组项目进行排序。 5 * @version 1.12 2015-06-21 6 * @author Cay Horstmann 7 */ 8 public class TreeSetTest 9 { 10 public static void main(String[] args) 11 { 12 SortedSet<Item> parts = new TreeSet<>(); 13 parts.add(new Item("Toaster", 1234)); 14 parts.add(new Item("Widget", 4562)); 15 parts.add(new Item("Modem", 9912)); 16 System.out.println(parts); 17 18 NavigableSet<Item> sortByDescription = new TreeSet<>( 19 Comparator.comparing(Item::getDescription)); 20 21 sortByDescription.addAll(parts); 22 System.out.println(sortByDescription); 23 } 24 }
1 package treeSet; 2 3 import java.util.*; 4 5 /** 6 * 具有描述和零件编号的项目 7 */ 8 public class Item implements Comparable<Item> 9 { 10 private String description; 11 private int partNumber; 12 13 /** 14 * 构造一个项目 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 * 获取此项的说明。 29 * 30 * @返回描述 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 }
测试程序4:
l 使用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); } } |
1 import java.util.*; 2 public class HashMapDemo { 3 public static void main(String[] argv) { 4 HashMap h = new HashMap(); 5 // 从公司名称到地址的哈希映射。 6 h.put("Adobe", "Mountain View, CA"); 7 h.put("IBM", "White Plains, NY"); 8 h.put("Sun", "Mountain View, CA"); 9 String queryString = "Adobe"; 10 String resultString = (String)h.get(queryString); 11 System.out.println("They are located in: " + resultString); 12 } 13 }
l 在Elipse环境下调试教材373页程序9-6,结合程序运行结果理解程序;
l 了解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 //打印所有条目 21 22 System.out.println(staff); 23 24 // 删除条目 25 26 staff.remove("567-24-2546"); 27 28 // 替换条目 29 30 staff.put("456-62-5527", new Employee("Francesca Miller")); 31 32 // 查找一个值 33 34 System.out.println(staff.get("157-62-7935")); 35 36 // 遍历所有条目 37 38 staff.forEach((k, v) -> 39 System.out.println("key=" + k + ", value=" + v)); 40 } 41 }
1 package map; 2 3 /** 4 * 用于测试目的的极简主义雇员类。 5 */ 6 public class Employee 7 { 8 private String name; 9 private double salary; 10 11 /** 12 * 建造一个工资0美元的员工。 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 }
实验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,
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 Test { 13 private static ArrayList<Student> studentlist; 14 public static void main(String[] args) { 15 16 studentlist = new ArrayList<>(); 17 Scanner scanner = new Scanner(System.in); 18 File file = new File("F:\\java\\身份证号.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("1.字典排序 "); 54 System.out.println("2.输出年龄最大和年龄最小的人 "); 55 System.out.println("3.寻找同乡 "); 56 System.out.println("4.寻找年龄相近的人 "); 57 System.out.println("5.退出 "); 58 String m = scanner.next(); 59 switch (m) { 60 case "1": 61 Collections.sort(studentlist); 62 System.out.println(studentlist.toString()); 63 break; 64 case "2": 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 "3": 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 "4": 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 "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 int j=0,min=53,value=0,ok=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 ok=i; 123 } 124 } 125 return ok; 126 } 127 }
1 public class Student implements Comparable<Student> { 2 3 private String name; 4 private String number ; 5 private String sex ; 6 private int age; 7 private String province; 8 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public String getnumber() { 16 return number; 17 } 18 public void setnumber(String number) { 19 this.number = number; 20 } 21 public String getsex() { 22 return sex ; 23 } 24 public void setsex(String sex ) { 25 this.sex =sex ; 26 } 27 public int getage() { 28 29 return age; 30 } 31 public void setage(int age) { 32 // int a = Integer.parseInt(age); 33 this.age= age; 34 } 35 36 public String getprovince() { 37 return province; 38 } 39 public void setprovince(String province) { 40 this.province=province ; 41 } 42 43 public int compareTo(Student o) { 44 return this.name.compareTo(o.getName()); 45 } 46 47 public String toString() { 48 return name+" "+sex+" "+age+" "+number+" "+province+" "; 49 } 50 }
1 package Second; 2 import java.io.BufferedReader; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.util.ArrayList; 9 import java.util.Arrays; 10 import java.util.Collections; 11 import java.util.Scanner; 12 13 14 public class Search{ 15 16 private static ArrayList<Person> Personlist1; 17 public static void main(String[] args) { 18 19 Personlist1 = new ArrayList<>(); 20 21 Scanner scanner = new Scanner(System.in); 22 File file = new File("E:\\面向对象程序设计Java\\实验\\实验六\\身份证号.txt"); 23 24 try { 25 FileInputStream F = new FileInputStream(file); 26 BufferedReader in = new BufferedReader(new InputStreamReader(F)); 27 String temp = null; 28 while ((temp = in.readLine()) != null) { 29 30 Scanner linescanner = new Scanner(temp); 31 32 linescanner.useDelimiter(" "); 33 String name = linescanner.next(); 34 String id = linescanner.next(); 35 String sex = linescanner.next(); 36 String age = linescanner.next(); 37 String place =linescanner.nextLine(); 38 Person Person = new Person(); 39 Person.setname(name); 40 Person.setid(id); 41 Person.setsex(sex); 42 int a = Integer.parseInt(age); 43 Person.setage(a); 44 Person.setbirthplace(place); 45 Personlist1.add(Person); 46 47 } 48 } catch (FileNotFoundException e) { 49 System.out.println("查找不到信息"); 50 e.printStackTrace(); 51 } catch (IOException e) { 52 System.out.println("信息读取有误"); 53 e.printStackTrace(); 54 } 55 boolean isTrue = true; 56 while (isTrue) { 57 System.out.println("******************************************"); 58 System.out.println("1:按姓名字典顺序输出信息;"); 59 System.out.println("2:查询最大年龄与最小年龄人员信息;"); 60 System.out.println("3:按省份找你的同乡;"); 61 System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;"); 62 System.out.println("5:退出"); 63 System.out.println("******************************************"); 64 int type = scanner.nextInt(); 65 switch (type) { 66 case 1: 67 Collections.sort(Personlist1); 68 System.out.println(Personlist1.toString()); 69 break; 70 case 2: 71 72 int max=0,min=100;int j,k1 = 0,k2=0; 73 for(int i=1;i<Personlist1.size();i++) 74 { 75 j=Personlist1.get(i).getage(); 76 if(j>max) 77 { 78 max=j; 79 k1=i; 80 } 81 if(j<min) 82 { 83 min=j; 84 k2=i; 85 } 86 87 } 88 System.out.println("年龄最大:"+Personlist1.get(k1)); 89 System.out.println("年龄最小:"+Personlist1.get(k2)); 90 break; 91 case 3: 92 System.out.println("place?"); 93 String find = scanner.next(); 94 String place=find.substring(0,3); 95 String place2=find.substring(0,3); 96 for (int i = 0; i <Personlist1.size(); i++) 97 { 98 if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 99 { 100 System.out.println("你的同乡:"+Personlist1.get(i)); 101 } 102 } 103 104 break; 105 case 4: 106 System.out.println("年龄:"); 107 int yourage = scanner.nextInt(); 108 int close=ageclose(yourage); 109 int d_value=yourage-Personlist1.get(close).getage(); 110 System.out.println(""+Personlist1.get(close)); 111 112 break; 113 case 5: 114 isTrue = false; 115 System.out.println("再见!"); 116 break; 117 default: 118 System.out.println("输入有误"); 119 } 120 } 121 } 122 public static int ageclose(int age) { 123 int m=0; 124 int max=53; 125 int d_value=0; 126 int k=0; 127 for (int i = 0; i < Personlist1.size(); i++) 128 { 129 d_value=Personlist1.get(i).getage()-age; 130 if(d_value<0) d_value=-d_value; 131 if (d_value<max) 132 { 133 max=d_value; 134 k=i; 135 } 136 137 } return k; 138 139 } 140 }
1 package Second; 2 3 //jiekouwenjiaan 4 5 6 public class Person implements Comparable<Person> { 7 private String name; 8 private String id; 9 private int age; 10 private String sex; 11 private String birthplace; 12 13 public String getname() { 14 return name; 15 } 16 public void setname(String name) { 17 this.name = name; 18 } 19 public String getid() { 20 return id; 21 } 22 public void setid(String id) { 23 this.id= id; 24 } 25 public int getage() { 26 27 return age; 28 } 29 public void setage(int age) { 30 //int a = Integer.parseInt(age); 31 this.age= age; 32 } 33 public String getsex() { 34 return sex; 35 } 36 public void setsex(String sex) { 37 this.sex= sex; 38 } 39 public String getbirthplace() { 40 return birthplace; 41 } 42 public void setbirthplace(String birthplace) { 43 this.birthplace= birthplace; 44 } 45 46 public int compareTo(Person o) { 47 return this.name.compareTo(o.getname()); 48 49 } 50 51 public String toString() { 52 return name+" "+sex+" "+age+" "+id+" ";
结合使用体验对所运行程序提出完善建议;
l 各自运行合作伙伴实验十编程练习2,
1 package a; 2 3 import java.io.FileNotFoundException; 4 import java.io.PrintWriter; 5 import java.util.Scanner; 6 7 //import org.w3c.dom.css.Counter; 8 9 10 public class Main{ 11 public static void main(String[] args) { 12 13 Scanner in = new Scanner(System.in); 14 counter Counter=new counter(); 15 PrintWriter output = null; 16 try { 17 output = new PrintWriter("result.txt"); 18 } catch (Exception e) { 19 //e.printStackTrace(); 20 } 21 int sum = 0; 22 23 for (int i = 1; i <=10; i++) { 24 int a = (int) Math.round(Math.random() * 100); 25 int b = (int) Math.round(Math.random() * 100); 26 int type = (int) Math.round(Math.random() * 4); 27 28 29 switch(type) 30 { 31 case 1: 32 33 System.out.println(i+": "+a+"/"+b+"="); 34 while(b== 0&& a%b!=0) 35 { 36 b = (int) Math.round(Math.random() * 100); 37 a = (int) Math.round(Math.random() * 100); 38 } 39 40 int c = in.nextInt(); 41 output.println(a+"/"+b+"="+c); 42 if (c == Counter.Chu(a, b)) 43 { 44 sum += 10; 45 System.out.println("恭喜答案正确!"); 46 } 47 else { 48 System.out.println("答案错误!"); 49 } 50 break; 51 52 case 2: 53 System.out.println(i+": "+a+"*"+b+"="); 54 int c1 = in.nextInt(); 55 output.println(a+"*"+b+"="+c1); 56 if (c1 == Counter.Cheng(a, b)) { 57 sum += 10; 58 System.out.println("恭喜答案正确!"); 59 } 60 else { 61 System.out.println("答案错误!"); 62 } 63 break; 64 case 3: 65 System.out.println(i+": "+a+"+"+b+"="); 66 int c2 = in.nextInt(); 67 output.println(a+"+"+b+"="+c2); 68 69 if (c2 == Counter.Jia(a, b)) { 70 sum += 10; 71 System.out.println("恭喜答案正确!"); 72 } 73 else { 74 System.out.println("答案错误!"); 75 } 76 break ; 77 78 79 case 4: 80 81 while (a < b) { 82 int x=0; 83 x=b; 84 b=a; 85 a=x; 86 } 87 System.out.println(i+": "+a+"-"+b+"="); 88 int c3 = in.nextInt(); 89 output.println(a+"-"+b+"="+c3); 90 if (c3 == Counter.Jian(a, b)) 91 { 92 sum += 10; 93 System.out.println("恭喜答案正确!"); 94 } 95 else { 96 System.out.println("答案错误!"); 97 } 98 break ; 99 100 } 101 102 } 103 System.out.println("成绩"+sum); 104 output.println("成绩:"+sum); 105 output.close(); 106 107 } 108 }
1 package a; 2 3 4 public class counter<T>{ 5 private T a; 6 private T b; 7 8 public counter(T a, T b) { 9 this.a = a; 10 this.b = b; 11 } 12 13 public counter() { 14 // TODO Auto-generated constructor stub 15 } 16 17 public int Jia(int a,int b) 18 { 19 return a+b; 20 } 21 public int Jian(int a,int b) 22 { 23 return a-b; 24 25 26 } 27 public int Cheng(int a,int b) 28 { 29 return a*b; 30 } 31 public int Chu(int a,int b) 32 { 33 if (b!= 0 && a%b==0) 34 return a / b; 35 else 36 return 0; 37 38 } 39 40 41 }
结合使用体验对所运行程序提出完善建议;
实验十
1.没有全面考虑小学生的实际状况:减法算法结果可能出现负数的情况,以及除法的运算结果直接取整输出,都不符合小学生的学习范围;
2.编程过程中,关于变量的使用混乱问题;
3.结果没有输出到文件中;
l 采用结对编程方式,与学习伙伴合作完成实验九编程练习1;
1 package Second; 2 import java.io.BufferedReader; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.util.ArrayList; 9 import java.util.Arrays; 10 import java.util.Collections; 11 import java.util.Scanner; 12 13 14 public class Search{ 15 16 private static ArrayList<Person> Personlist1; 17 public static void main(String[] args) { 18 19 Personlist1 = new ArrayList<>(); 20 21 Scanner scanner = new Scanner(System.in); 22 File file = new File("E:\\面向对象程序设计Java\\实验\\实验六\\身份证号.txt"); 23 24 try { 25 FileInputStream F = new FileInputStream(file); 26 BufferedReader in = new BufferedReader(new InputStreamReader(F)); 27 String temp = null; 28 while ((temp = in.readLine()) != null) { 29 30 Scanner linescanner = new Scanner(temp); 31 32 linescanner.useDelimiter(" "); 33 String name = linescanner.next(); 34 String id = linescanner.next(); 35 String sex = linescanner.next(); 36 String age = linescanner.next(); 37 String place =linescanner.nextLine(); 38 Person Person = new Person(); 39 Person.setname(name); 40 Person.setid(id); 41 Person.setsex(sex); 42 int a = Integer.parseInt(age); 43 Person.setage(a); 44 Person.setbirthplace(place); 45 Personlist1.add(Person); 46 47 } 48 } catch (FileNotFoundException e) { 49 System.out.println("查找不到信息"); 50 e.printStackTrace(); 51 } catch (IOException e) { 52 System.out.println("信息读取有误"); 53 e.printStackTrace(); 54 } 55 boolean isTrue = true; 56 while (isTrue) { 57 System.out.println("******************************************"); 58 System.out.println("1:按姓名字典顺序输出信息;"); 59 System.out.println("2:查询最大年龄与最小年龄人员信息;"); 60 System.out.println("3:按省份找你的同乡;"); 61 System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;"); 62 System.out.println("5:退出"); 63 System.out.println("******************************************"); 64 int type = scanner.nextInt(); 65 switch (type) { 66 case 1: 67 Collections.sort(Personlist1); 68 System.out.println(Personlist1.toString()); 69 break; 70 case 2: 71 72 int max=0,min=100;int j,k1 = 0,k2=0; 73 for(int i=1;i<Personlist1.size();i++) 74 { 75 j=Personlist1.get(i).getage(); 76 if(j>max) 77 { 78 max=j; 79 k1=i; 80 } 81 if(j<min) 82 { 83 min=j; 84 k2=i; 85 } 86 87 } 88 System.out.println("年龄最大:"+Personlist1.get(k1)); 89 System.out.println("年龄最小:"+Personlist1.get(k2)); 90 break; 91 case 3: 92 System.out.println("place?"); 93 String find = scanner.next(); 94 String place=find.substring(0,3); 95 String place2=find.substring(0,3); 96 for (int i = 0; i <Personlist1.size(); i++) 97 { 98 if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 99 { 100 System.out.println("你的同乡:"+Personlist1.get(i)); 101 } 102 } 103 104 break; 105 case 4: 106 System.out.println("年龄:"); 107 int yourage = scanner.nextInt(); 108 int close=ageclose(yourage); 109 int d_value=yourage-Personlist1.get(close).getage(); 110 System.out.println(""+Personlist1.get(close)); 111 112 break; 113 case 5: 114 isTrue = false; 115 System.out.println("再见!"); 116 break; 117 default: 118 System.out.println("输入有误"); 119 } 120 } 121 } 122 public static int ageclose(int age) { 123 int m=0; 124 int max=53; 125 int d_value=0; 126 int k=0; 127 for (int i = 0; i < Personlist1.size(); i++) 128 { 129 d_value=Personlist1.get(i).getage()-age; 130 if(d_value<0) d_value=-d_value; 131 if (d_value<max) 132 { 133 max=d_value; 134 k=i; 135 } 136 137 } return k; 138 139 } 140 }
1 package Second; 2 3 //jiekouwenjiaan 4 5 6 public class Person implements Comparable<Person> { 7 private String name; 8 private String id; 9 private int age; 10 private String sex; 11 private String birthplace; 12 13 public String getname() { 14 return name; 15 } 16 public void setname(String name) { 17 this.name = name; 18 } 19 public String getid() { 20 return id; 21 } 22 public void setid(String id) { 23 this.id= id; 24 } 25 public int getage() { 26 27 return age; 28 } 29 public void setage(int age) { 30 //int a = Integer.parseInt(age); 31 this.age= age; 32 } 33 public String getsex() { 34 return sex; 35 } 36 public void setsex(String sex) { 37 this.sex= sex; 38 } 39 public String getbirthplace() { 40 return birthplace; 41 } 42 public void setbirthplace(String birthplace) { 43 this.birthplace= birthplace; 44 } 45 46 public int compareTo(Person o) { 47 return this.name.compareTo(o.getname()); 48 49 } 50 51 public String toString() { 52 return name+" "+sex+" "+age+" "+id+" ";
采用结对编程方式,与学习伙伴合作完成实验十编程练习2。
1 package A; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.util.Scanner; 7 public class Main { 8 9 public static void main(String[] args) { 10 Scanner in = new Scanner(System.in); 11 counter min=new counter(); 12 PrintWriter out = null; 13 try { 14 out = new PrintWriter("result.txt"); 15 int sum = 0; 16 for (int i = 1; i <=10; i++) { 17 int a = (int) Math.round(Math.random() * 100); 18 int b = (int) Math.round(Math.random() * 100); 19 int menu = (int) Math.round(Math.random() * 3); 20 switch (menu) { 21 case 0: 22 System.out.println(i+":"+a + "+" + b + "="); 23 int c1 = in.nextInt(); 24 out.println(a + "+" + b + "=" + c1); 25 if (c1 == (a + b)) { 26 sum += 10; 27 System.out.println("恭喜答案正确"); 28 } else { 29 System.out.println("抱歉,答案错误"); 30 } 31 break; 32 case 1: 33 while (a < b) { 34 b = (int) Math.round(Math.random() * 100); 35 } 36 System.out.println(i+":"+a + "-" + b + "="); 37 int c2 = in.nextInt(); 38 out.println(a + "-" + b + "=" + c2); 39 if (c2 == (a - b)) { 40 sum += 10; 41 System.out.println("恭喜答案正确"); 42 } else { 43 System.out.println("抱歉,答案错误"); 44 } 45 46 break; 47 case 2: 48 System.out.println(i+":"+a + "*" + b + "="); 49 int c3 = in.nextInt(); 50 out.println(a + "*" + b + "=" + c3); 51 if (c3 == a * b) { 52 sum += 10; 53 System.out.println("恭喜答案正确"); 54 } else { 55 System.out.println("抱歉,答案错误"); 56 } 57 58 break; 59 case 3: 60 while(b == 0){ 61 b = (int) Math.round(Math.random() * 100); 62 } 63 while(a % b != 0){ 64 a = (int) Math.round(Math.random() * 100); 65 66 } 67 System.out.println(i+":"+a + "/" + b + "="); 68 int c4 = in.nextInt(); 69 if (c4 == a / b) { 70 sum += 10; 71 System.out.println("恭喜,答案正确"); 72 } else { 73 System.out.println("抱歉,答案错误"); 74 } 75 76 break; 77 } 78 } 79 System.out.println("你的得分为" + sum); 80 out.println("你的得分为" + sum); 81 out.close(); 82 } catch (FileNotFoundException e) { 83 e.printStackTrace(); 84 } 85 } 86 }
1 package A; 2 3 public class counter<T> { 4 private T a; 5 private T b; 6 public counter() { 7 a=null; 8 b=null; 9 } 10 public counter(T a,T b) { 11 this.a=a; 12 this.b=b; 13 } 14 public int count1(int a,int b) { 15 return a+b; 16 } 17 public int count2(int a,int b) { 18 return a-b; 19 } 20 public int count3(int a,int b) { 21 return a*b; 22 } 23 public int count4(int a,int b) { 24 return a/b; 25 } 26 }
实验总结:
这次实验完成后,感觉结对实验会更有收获,不管是从所得的分享,还是共同完成一个实验,都有一些的收获。
以上是关于201771010102 常惠琢 《2018面向对象程序设计(Java)》第11周学习总结的主要内容,如果未能解决你的问题,请参考以下文章
201771010102 常惠琢 《2018面向对象程序设计(Java)》第9周学习总结
201771010102 常惠琢 《2018面向对象程序设计(Java)》第11周学习总结
201771010102 常惠琢《2018面向对象程序设计(Java)》第10周学习总结
常惠琢 201771010102《面向对象程序设计(java)》第七周学习总结