东文财、赵栋《面向对象程序设计(java)》第十一周学习总结
201771010106 东文财《面向对象程序设计(java)》第十一周
实验时间 2018-11-8
一:理论部分。
1.数据结构:
a.线性数据结构,如线性表、栈、队列、串、数组和文件。
b.非线性数据结构,如树和图。
1)所有数据元素在同一个线性表中必须是相同的数据类型。
线性表按其存储结构可分为顺序表和链表。
2)栈:也是一种特殊的线性表,是一种后进先出(LIFO)的结构。
栈是限定仅在表尾进行插入和删除运算的线性表,表尾称为栈顶,表头称为栈底。
3)队列:限定所有的插入只能在表的一端进行,而所有的删除都在表的另一端进行的线性表。是一种先进先出(FIFO)的结构。
表中允许插入的一端称为队尾,允许删除的一端称为队头。
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)练习,体验程序开发中的两人合作。
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:
package qq; 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 WW { 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) {//instanceof指出对象是否是cat类 ((Cat) cats.elementAt(i)).print(); } //((Cat) cats.elementAt(i)).print();//程序有未检查异常 else { ((Dog) cats.elementAt(i)).print();//判断后若不是则返回dog类 } } } }
实验结果:
示例程序2:
package qq; import java.util.*; public class Stzacks { 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)); //elementAt(2)该方法在没有list以前跟get方法的功能完全相同 while (!stk.empty()) System.out.println(stk.pop());//出栈操作 } }
实验结果:
示例程序3:
package qq; import java.util.*; class Counter { int i = 1;//权限 public String toString() { return Integer.toString(i); } } public class WW { 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));//random半开半闭区间0-19的数 //Integer是int的封装类,将生成的数据转化成整型,让r去调用它 if (ht.containsKey(r)) ((Counter) ht.get(r)).i++;//直接在类外部引用属性是因为i的访问权限是friendly //通过get方法通过键获得对应的value值 else ht.put(r, new Counter());//调用put方法使得哈希表创建一个新的键值对 } System.out.println(ht); } }
实验结果:
测试程序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"); } }
|
package qq; import java.util.*; public class WW { public static void main(String[] argv) { ArrayList al = new ArrayList();//ArrayList类生成一个数组由a1调用使用 // Add lots of elements to the ArrayList... al.add(new Integer(11)); //integer是与int不同的数据类型,往ArrayList中放东西时,像int这种内建类型是放不进去的。 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));//已有list,可直接用get方法 } } }
实验结果:
package qq; 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);//listIterator(迭代器)用来遍历集合 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"); } }
实验结果:
l 在Elipse环境下编辑运行调试教材360页程序9-1,结合程序运行结果理解程序;
l 掌握ArrayList、LinkList两个类的用途及常用API。
package qq; import java.util.*; /** * This program demonstrates operations on linked lists. * @version 1.11 2012-01-26 * @author Cay Horstmann */ public class WW { public static void main(String[] args) { List<String> a = new LinkedList<>();//创建一个泛型类型,使得a能引用它 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();//遍历a中的所有元素,且a中元素为string类 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程序,结合运行结果理解程序;
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()); } } } |
package qq; import java.util.*; public class WW { 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()); } } }
实验结果:
l 在Elipse环境下调试教材365页程序9-2,结合运行结果理解程序;了解HashSet类的用途及常用API。
package qq; 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 WW { 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."); } }
l 在Elipse环境下调试教材367页-368程序9-3、9-4,结合程序运行结果理解程序;了解TreeSet类的用途及常用API。
package qq; import java.util.*; /** * An item with a description and a part number. */ public class ee implements Comparable<ee> { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item's description * @param aPartNumber * the item's part number */ public ee(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; ee other = (ee) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(ee other) { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }
package qq; 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 WW { public static void main(String[] args) { SortedSet<ee> parts = new TreeSet<>(); parts.add(new ee("Toaster", 1234)); parts.add(new ee("Widget", 4562)); parts.add(new ee("Modem", 9912)); System.out.println(parts); NavigableSet<ee> sortByDescription = new TreeSet<>( Comparator.comparing(ee::getDescription)); sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
实验结果|:
测试程序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); } } |
package qq; import java.util.*; public class WW { 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";//访问指定的关键字Adobe String resultString = (String)h.get(queryString); System.out.println("They are located in: " + resultString); } }
实验结果:
l 在Elipse环境下调试教材373页程序9-6,结合程序运行结果理解程序;
l 了解HashMap、TreeMap两个类的用途及常用API。
package qq; /** * A minimalist employee class for testing purposes. */ public class WW { private String name; private double salary; /** * Constructs an employee with $0 salary. * @param n the employee name */ public WW(String name) { this.name = name; salary = 0; } public String toString() { return "[name=" + name + ", salary=" + salary + "]"; } }
package qq; 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 ee { public static void main(String[] args) { Map<String, WW> staff = new HashMap<>(); staff.put("144-25-5464", new WW("Amy Lee")); staff.put("567-24-2546", new WW("Harry Hacker")); staff.put("157-62-7935", new WW("Gary Cooper")); staff.put("456-62-5527", new WW("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 WW("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 各自运行合作伙伴实验十编程练习2,结合使用体验对所运行程序提出完善建议;
l 采用结对编程方式,与学习伙伴合作完成实验九编程练习1;
package qq; import java.beans.Statement; 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 WW { private static ArrayList<Statement> studentlist; public static void main(String[] args) { studentlist = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("F:\\java\\身份证号.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 age = linescanner.next(); String province =linescanner.nextLine(); Statement student = new Statement(student, province, args); ((Object) 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.寻找年龄相近的人 "); System.out.println("5.退出 "); String m = scanner.next(); switch (m) { 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("地址?"); 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; case "4": System.out.println("年龄:"); int yourage = scanner.nextInt(); int near=agenear(yourage); int value=yourage-studentlist.get(near).getage(); System.out.println(""+studentlist.get(near)); break; case "5 ": isTrue = false; System.out.println("退出程序!"); break; default: System.out.println("输入有误"); } } } public static int agenear(int age) { int j=0,min=53,value=0,ok=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; ok=i; } } return ok; } }
package qq; public class ee { 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) { // int a = Integer.parseInt(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 qq; 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 Search{ private static ArrayList<Person> Personlist1; public static void main(String[] args) { Personlist1 = new ArrayList<>(); Scanner scanner = new Scanner(System.in); File file = new File("E:\\面向对象程序设计Java\\实验\\实验六\\身份证号.txt"); 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("******************************************"); System.out.println("1:按姓名字典顺序输出信息;"); System.out.println("2:查询最大年龄与最小年龄人员信息;"); System.out.println("3:按省份找你的同乡;"); System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;"); System.out.println("5:退出"); System.out.println("******************************************"); 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; } Search }
//jiekouwenjiaan 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"; Person
实验结果:
l采用结对编程方式,与学习伙伴合作完成实验十编程练习2。
package PP; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); counter min=new counter(); PrintWriter out = null; try { out = new PrintWriter("result.txt"); 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 menu = (int) Math.round(Math.random() * 3); switch (menu) { case 0: System.out.println(i+":"+a + "+" + b + "="); int c1 = in.nextInt(); out.println(a + "+" + b + "=" + c1); if (c1 == (a + b)) { sum += 10; System.out.println("恭喜答案正确"); } else { System.out.println("抱歉,答案错误"); } break; case 1: while (a < b) { b = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "-" + b + "="); int c2 = in.nextInt(); out.println(a + "-" + b + "=" + c2); if (c2 == (a - b)) { sum += 10; System.out.println("恭喜答案正确"); } else { System.out.println("抱歉,答案错误"); } break; case 2: System.out.println(i+":"+a + "*" + b + "="); int c3 = in.nextInt(); out.println(a + "*" + b + "=" + c3); if (c3 == a * b) { sum += 10; System.out.println("恭喜答案正确"); } else { System.out.println("抱歉,答案错误"); } break; case 3: while(b == 0){ b = (int) Math.round(Math.random() * 100); } while(a % b != 0){ a = (int) Math.round(Math.random() * 100); } System.out.println(i+":"+a + "/" + b + "="); int c4 = in.nextInt(); if (c4 == a / b) { sum += 10; System.out.println("恭喜,答案正确"); } else { System.out.println("抱歉,答案错误"); } break; } } System.out.println("你的得分为" + sum); out.println("你的得分为" + sum); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
package PP; public class counter<T> { private T a; private T b; public counter() { a=null; b=null; } public counter(T a,T b) { this.a=a; this.b=b; } public int count1(int a,int b) { return a+b; } public int count2(int a,int b) { return a-b; } public int count3(int a,int b) { return a*b; } public int count4(int a,int b) { return a/b; } }
实验结果:
实验总结:
通过本周的实验我学到了新的知识点集合,知道了集合的作用和它的特点,同时我们也学习了Vectorl类和Stack类,也知道了Java集合框架中的基本接口的实现。总之来说这周收获也是不错的,在做本次实验时还进行了两人一组合作编程。通过合作发现自己的不足和别人的优点,然后加以改正和学习。