zoukankan      html  css  js  c++  java
  • 张季跃 201771010139《面向对象程序设计(java)》第十一周学习总结

    张季跃 201771010139《面向对象程序设计(java)》第十一周学习总结

    第一部分:理论知识学习部分

    一.数据结构:

    1. 一般将数据结构分为两大类:

    (1)线性数据结构:线性表、栈、队列、串、数组和文件。

    (2)非线性数据结构:树和图。

    1. 线性表:l

    (1)线性表的逻辑结构是n个数据元素的有限序列: (a1,a 2,a3,…an) n为线性表的长度(n≥0),n=0的表称为空表。 l

    (2)数据元素呈线性关系:必存在唯一的称为“第一 个”的数据元素;必存在唯一的称为“后一个 ”的数据元素;除第一个元素外,每个元素都有 且只有一个前驱元素;除后一个元素外,每个 元素都有且只有一个后继元素。 l

    (3)所有数据元素在同一个线性表中必须是相同的数 据类型。

    (4)线性表按其存储结构可分为顺序表和链表

    (5)用顺序存储结构存储的线性表称为 顺序表

    (6)顺序表将线性表中的数据元素依次存放在某个存储区域中。 一维数组 就是用顺序方式存储的线性表。

    (7)用链式存储结构存储的线性表称为链表

    1. 栈:

    l(1)栈(Stack)也是一种特殊的 线性表,是一种后进先出 (LIFO)的结构。 l

    2)栈是限定仅在表尾进行插入 和删除运算的线性表,表尾 称为栈顶(top),表头称为 栈底(bottom)。 l

    (3)栈的物理存储可以用顺序存 储结构,也可以用链式存储 结构

    1. 队列:

    (1)队列(Queue)是限定所有的插入只能在表的一端进行 ,而所有的删除都在表的另一端进行的线性表。 l

    (2)表中允许插入的一端称为队尾(Rear),允许删除的一端称为队头(Front)。 l

    (3)队列的操作是按先进先出(FIFO)的原则进行的。 l

    (4)队列的物理存储可以用顺序存储结构,也可以用链 式存储结构

    1. 散列表:

    (1)散列表又称为哈希表。散列表算法的基本思想是:以结点 的关键字为自变量,通过一定的函数关系(散列函数)计 算出对应的函数值,以这个值作为该结点存储在散列表中的地址。 l

    (2)当散列表中的元素存放太满,就必须进行再散列,将产生 一个新的散列表,所有元素存放到新的散列表中,原先的 散列表将被删除。在Java语言中,通过负载因子(load factor)来决定何时对散列表进行再散列。例如:如果负载 因子是0.75,当散列表中已经有75%的位置已经放满,那么 将进行再散列。 l

    (3)负载因子越高(越接近1.0),内存的使用效率越高,元素的 寻找时间越长。负载因子越低(越接近0.0),元素的寻找时 间越短,内存浪费越多。

    (4)HashSet类的缺省负载因子是0.75。

    二.JAVA的集合框架:

    1. 集合框架:

    (1)JAVA的集合框架实现对各种数据结构的封装,以 降低对数据管理与处理的难度。 l

    (2)所谓框架就是一个类库的集合,框架中包含很多 超类,编程者创建这些超类的子类可较方便的设 计设计程序所需的类。例如:Swing类包 l

    (3)集合(Collection或称为容器)是一种包含多个元素 并提供对所包含元素操作方法的类,其包含的元 素可以由同一类型的对象组成,也可以由不同类 型的对象组成。 l

    (4)集合框架:JAVA集合类库的统一架构。

    1. 集合类的作用:

    (1)l集合类的作用: –Java的集合类提供了一些基本数据结构的支持。 –例如Vector、Hashtable、Stack等。

    (2)集合类的使用: –Java的集合类包含在java.util包中。 –import java.util.*;

    1. 集合类的特点

    (1)–只容纳对象。 注意:数组可以容纳基本数据类型数据和对象。 –如果集合类中想使用基本数据类型,又想利用 集合类的灵活性,可以把基本数据类型数据封 装成该数据类型的包装器对象,然后放入集合 中处理

    (2)–集合类容纳的对象都是Object类的实例,一旦 把一个对象置入集合类中,它的类信息将丢失 ,这样设计的目的是为了集合类的通用性。 –因为Object类是所有类的祖先,所以可以在这 些集合中存放任何类的对象而不受限制,但切 记在使用集合成员之前必须对它重新造型

    1. 新旧集合类:

    (1)l在JDK1.0和JDK1.1中提供了Vector(矢量), Hashtable(哈希表),Stack(堆栈), Properties(属性集)等集合类,尽管这些类非 常有用,但却彼此独立,缺少一个统一集中的机 制。 l

    (2)JDK1.2中,JAVA设计了一个统一的类集,并 对上述类进行了改写,使其统一纳入JAVA的集 合框架

    三.JDK1.1版本中的集合类:

    1. Vector:

    (1)lVector类类似长度可变的数组。 l

    (2)Vector中只能存放对象。 l

    (3)Vector的元素通过下标进行访问。 l

    (4)Vector类关键属性: –capacity表示集合多能容纳的元素个数。 –capacityIncrement表示每次增加多少容量。 –size表示集合当前元素个数

    1. Stack:

    (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. Hashtable:

    (1)lHashtable通过键来查找元素。

    (2)Hashtable用散列码(hashcode)来确定键。所 有对象都有一个散列码,可以通过Object类的 hashCode()方法获得

    四.JDK1.2以后版本中的集合类:

    1. Collection :

    集合层次中的根接口,JDK未提供这个 接口的直接实现类。

    1. List:

    是一个有序的集合,可以包含重复的元素。提 供了按索引访问的方式。

    1. Set :

    不能包含重复的元素。对象可能不是按存放的 次序存放,也就是说不能像数组一样按索引的方式进 行访问,SortedSet是一个按照升序排列元素的Set。

    1. Map:

    包含了key-value对。Map不能包含重复的key 。 lSortedMap是一个按照升序排列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:

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

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

    测试程序1:

    import java.util.Vector;

    class Cat {

    private int catNumber;

    Cat(int i) {

    catNumber = i;

    }

    void print() {

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

    }

    }

    class Dog {

    private int dogNumber;

    Dog(int i) {

    dogNumber = i;

    }

    void print() {

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

    }

    }

    public class CatsAndDogs {

    public static void main(String[] args) {

    Vector cats = new Vector();

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

    cats.addElement(new Cat(i));

    cats.addElement(new Dog(7));

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

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

    }

    }

    测试结果:

     

    修改测试程序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:

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

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

    掌握ArrayListLinkList两个类的用途及常用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:

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

    Elipse环境下调试教材365页程序9-2,结合运行结果理解程序;了解HashSet类的用途及常用API。

    Elipse环境下调试教材367页-368程序9-3、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-3、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:

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

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

    了解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:结对编程练习:

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

     

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

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

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

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

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

     

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

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

    合作伙伴:邹丰蔚

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

    合作伙伴的程序:

    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+" "+sex+" "+year+" "+number+" "+province+" ";

             }

    public int getyear()

    {

    // TODO Auto-generated method stub

    return 0;

    }

    public void setname(String name2)

    {

    // TODO Auto-generated method stub

    }

    }

    测试结果:

     

    完善建议

    程序运行无明显错误,但程序行与行之间空格太大,显得程序太长,建议再重新将程序排列一下。

    各自运行合作伙伴实验十编程练习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;

    }

    }

    }

    测试结果:

     

    完善建议

    行数太多,但程序总数少,建议简化程序。

    采用结对编程方式,与学习伙伴合作完成实验九编程练习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(" ");

                        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+ " " +place;

            }

        }

        public int compareTo(People o)

        {

            return this.name.compareTo(o.getName());

        }

        public String toString()

        {

    return  name+" "+sex+" "+age+" "+number+" "+place+" ";

        }

    }

    实验结果:

     

     

     

    采用结对编程方式,与学习伙伴合作完成实验十编程练习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中的数据结构我有了初步的了解,但对于集合我感觉还是有不会的东西需要请教同学,而且,这一周通过结对编程练习,我对于前几周编写的程序有了更深入的了解,巩固了以前学习的知识。

  • 相关阅读:
    假期周进度报告02
    假期周进度报告01
    浪潮之巅阅读笔记6
    浪潮之巅阅读笔记5
    浪潮之巅阅读笔记4
    科技创新平台年报系统利益相关者分析
    浪潮之巅阅读笔记3
    浪潮之巅阅读笔记2
    浪潮之巅阅读笔记1
    Linux Redis 重启数据丢失解决方案,Linux重启后Redis数据丢失解决方
  • 原文地址:https://www.cnblogs.com/Alex-Morcer/p/9942566.html
Copyright © 2011-2022 走看看