zoukankan      html  css  js  c++  java
  • 201771010134杨其菊《面向对象程序设计java》第十周学习总结

             第8章泛型程序设计学习总结


     第一部分:理论知识

     主要内容:   什么是泛型程序设计
                       泛型类的声明及实例化的方法
                    泛型方法的定义
                         泛型接口的定义
                        泛型类型的继承规则
                        通配符类型及使用方法

     

    1:泛型类的定义
      

       (1) 一个泛型类(generic class)就是具有一个或多个类型变量的类,即创建用类型作为参数的类。如一个泛型类定义格式如下:class Generics<K,V>其中的K和V是类中的可变类型参数。如:

    public class Pair{
    private T first;
    private T second;
    public Pair() {first = null; second = null;}
    public Pair(T first, T second) {
    this.first = first; this.second = second;
    }
    public T getFirst() {return first;}
    public T getSecond() {return second;}
    public void setFirst(T newValue)
    {first = newValue;}
    public void setSecond(T newValue)
    {second = newValue;}
    }

    (2)Pair类引入了一个类型变量T,用尖括号(<>)括起来,并放在类名的后面。泛型类可以有多个类型变量。例如:public class Pair<t, u=""> { … }

      (3)   类定义中的类型变量用于指定方法的返回类型以及域、局部变量的类型。

    2.泛型方法的声明
    (1)泛型方法
                 – 除了泛型类外,还可以只单独定义一个方法作为泛型方法,用于指定方法参数或者返回值为泛型类型,留待方法调用时确定。
                – 泛型方法可以声明在泛型类中,也可以声明在普通类中。

    public class ArrayTool
    {
    public static void insert(
    E[] e, int i)
    {
    //请自己添加代码
    }
    public static E valueAt(
    E[] e , int i)
    {
    //请自己添加代码
    }
    }

    3.泛型接口的定义
    (1)定义
    public interface IPool
    {
    T get();
    int add(T t);
    }

    (2)实现

    public class GenericPool implements IPool
    {

    }

    public class GenericPool implements IPool

    {

    }

    4.泛型变量的限定
    (1) 定义泛型变量的上界
    public class NumberGeneric< T extends Number>
    (2) 泛型变量上界的说明
     上述声明规定了NumberGeneric类所能处理的泛型变量类型需和Number有继承关系;
     extends关键字所声明的上界既可以是一个类,也可以是一个接口;
    (3)< T extends  BoundingType> 表示T应该是绑定类型的子类型。  一个类型变量或通配符可以有多个限定,限定类型用“&”分割。例如:< T extends Comparable & Serializable >
    (4) 定义泛型变量的下界
    List cards = new ArrayList();
    (5) 泛型变量下界的说明
    – 通过使用super关键字可以固定泛型参数的类型为某种类型或者其超类
    – 当程序希望为一个方法的参数限定类型时,通常可以使用下限通配符

    public static void sort(T[] a,Comparator c)
    { …
    }

    5.通配符类型

    通配符
    – “?”符号表明参数的类型可以是任何一种类型,它和参数T的含义是有区别的。T表示一种未知类型,而“?”表示任何一种类型。这种通配符一般有以下三种用法:
        – 单独的?,用于表示任何类型。
       – ? extends type,表示带有上界。
       – ? super type,表示带有下界。


    第二部分:实验部分

    实验十  泛型程序设计技术

    实验时间 2018-11-1

    1、实验目的与要求

    (1) 理解泛型概念;

    (2) 掌握泛型类的定义与使用;

    (3) 掌握泛型方法的声明与使用;

    (4) 掌握泛型接口的定义与实现;

    (5)了解泛型程序设计,理解其用途。

    2、实验内容和步骤

    实验1: 导入第8章示例程序,测试程序并进行代码注释。

    测试程序1:

    l 编辑、调试、运行教材311、312页 代码,结合程序运行结果理解程序;

    l 在泛型类定义及使用代码处添加注释;

    l 掌握泛型类的定义及使用。 

     1 package pair1;
     2 
     3 /**
     4  * @version 1.01 2012-01-26
     5  * @author Cay Horstmann
     6  */
     7 public class PairTest1
     8 {
     9    public static void main(String[] args)
    10    {
    11       String[] words = { "Mary", "had", "a", "little", "lamb" };
    12      //ArrayAlg调用静态方法minmax;
    13       Pair<String> mm = ArrayAlg.minmax(words);
    14       
    15       System.out.println("min = " + mm.getFirst());
    16       System.out.println("max = " + mm.getSecond());
    17    }
    18 }
    19 
    20 class ArrayAlg
    21 {
    22    /**
    23     * Gets the minimum and maximum of an array of strings.
    24     * @param a an array of strings
    25     * @return a pair with the min and max value, or null if a is null or empty
    26     */
    27    public static Pair<String> minmax(String[] a)//定义静态泛型方法,将类型变量实例化为String;
    28    {
    29       if (a == null || a.length == 0) return null;
    30       String min = a[0];
    31       String max = a[0];
    32       for (int i = 1; i < a.length; i++)
    33       {
    34          if (min.compareTo(a[i]) > 0) min = a[i];
    35          if (max.compareTo(a[i]) < 0) max = a[i];
    36       }
    37       return new Pair<>(min, max);//返回一个实例化后的类对象;
    38    }
    39 }
     1 package pair1;
     2 
     3 /**
     4  * @version 1.00 2004-05-10
     5  * @author Cay Horstmann
     6  */
     7 public class Pair<T> //定义泛型类,类型变量为T;
     8 {
     9    private T first;
    10    private T second;
    11 
    12    public Pair() { first = null; second = null; }
    13   // 构造泛型方法,构造方法的入口参数(first, second)类型为泛型T;
    14    public Pair(T first, T second) { this.first = first;  this.second = second; }
    15    //定义泛型方法,类型变量T指定了访问方法的返回值和入口参数的类型;
    16    public T getFirst() { return first; }
    17    public T getSecond() { return second; }
    18   //泛型方法,构造方法的入口参数类型为泛型T;
    19    public void setFirst(T newValue) { first = newValue; }
    20    public void setSecond(T newValue) { second = newValue; }
    21 }

     

    测试程序2:

    l 编辑、调试运行教材315页 PairTest2,结合程序运行结果理解程序;

    l 在泛型程序设计代码处添加相关注释;

    l 掌握泛型方法、泛型变量限定的定义及用途。

     1 package pair2;
     2 
     3 /**
     4  * @version 1.00 2004-05-10
     5  * @author Cay Horstmann
     6  */
     7 public class Pair<T> //定义泛型类,类型变量为T;
     8 {
     9    private T first;
    10    private T second;
    11 
    12    public Pair() { first = null; second = null; }
    13   // 构造泛型方法,构造方法的入口参数(first, second)类型为泛型T;
    14    public Pair(T first, T second) { this.first = first;  this.second = second; }
    15    //定义泛型方法,类型变量T指定了访问方法的返回值和入口参数的类型;
    16    public T getFirst() { return first; }
    17    public T getSecond() { return second; }
    18   //泛型方法,构造方法的入口参数类型为泛型T;
    19    public void setFirst(T newValue) { first = newValue; }
    20    public void setSecond(T newValue) { second = newValue; }
    21 }
     1 package pair2;
     2 //import PairTest1.Pair;
     3 import java.time.*;
     4 
     5 /**
     6  * @version 1.02 2015-06-21
     7  * @author Cay Horstmann
     8  */
     9 public class PairTest2
    10 {
    11    public static void main(String[] args)
    12    {
    13       LocalDate[] birthdays = 
    14          { 
    15             LocalDate.of(1906, 12, 9), // G. Hopper
    16             LocalDate.of(1815, 12, 10), // A. Lovelace
    17             LocalDate.of(1903, 12, 3), // J. von Neumann
    18             LocalDate.of(1910, 6, 22), // K. Zuse
    19          };
    20       //ArrayAlg调用静态方法minmax,
    21       Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);
    22       System.out.println("min = " + mm.getFirst());
    23       System.out.println("max = " + mm.getSecond());
    24    }
    25 }
    26 
    27 class ArrayAlg
    28 {
    29    /**
    30       Gets the minimum and maximum of an array of objects of type T.
    31       @param a an array of objects of type T
    32       @return a pair with the min and max value, or null if a is 
    33       null or empty
    34    */
    35     //构造泛型方法;限定类型变量,类型变量T是Comparable类的子类,
    36    public static <T extends Comparable> Pair<T> minmax(T[] a) 
    37    {
    38       if (a == null || a.length == 0) return null;
    39       T min = a[0];
    40       T max = a[0];
    41       for (int i = 1; i < a.length; i++)
    42       {
    43          if (min.compareTo(a[i]) > 0) min = a[i];
    44          if (max.compareTo(a[i]) < 0) max = a[i];
    45       }
    46       return new Pair<>(min, max);//返回一个实例化泛型Pair类对象;
    47    }
    48 }

     

    测试程序3:

    l 用调试运行教材335页 PairTest3,结合程序运行结果理解程序;

    l 了解通配符类型的定义及用途。

     1 package pair3;
     2 
     3 import java.time.*;
     4 
     5 public class Employee
     6 {  
     7    private String name;
     8    private double salary;
     9    private LocalDate hireDay;
    10 
    11    public Employee(String name, double salary, int year, int month, int day)
    12    {
    13       this.name = name;
    14       this.salary = salary;
    15       hireDay = LocalDate.of(year, month, day);
    16    }
    17 
    18    public String getName()
    19    {
    20       return name;
    21    }
    22 
    23    public double getSalary()
    24    {  
    25       return salary;
    26    }
    27 
    28    public LocalDate getHireDay()
    29    {  
    30       return hireDay;
    31    }
    32 
    33    public void raiseSalary(double byPercent)
    34    {  
    35       double raise = salary * byPercent / 100;
    36       salary += raise;
    37    }
    38 }
     1 package pair3;
     2 
     3 public class Manager extends Employee// Manager继承父类 Employee;
     4 {  
     5    private double bonus;
     6 
     7    /**
     8       @param name the employee's name
     9       @param salary the salary
    10       @param year the hire year
    11       @param month the hire month
    12       @param day the hire day
    13    */
    14    public Manager(String name, double salary, int year, int month, int day)
    15    {  
    16       super(name, salary, year, month, day);
    17       bonus = 0;
    18    }
    19 
    20    public double getSalary()
    21    { 
    22       double baseSalary = super.getSalary();
    23       return baseSalary + bonus;
    24    }
    25 
    26    public void setBonus(double b)
    27    {  
    28       bonus = b;
    29    }
    30 
    31    public double getBonus()
    32    {  
    33       return bonus;
    34    }
    35 }
     1 package pair3;
     2 
     3 /**
     4  * @version 1.00 2004-05-10
     5  * @author Cay Horstmann
     6  */
     7 public class Pair<T> //定义泛型类,类型变量为T;
     8 {
     9    private T first;
    10    private T second;
    11 
    12    public Pair() { first = null; second = null; }
    13    // 构造泛型方法,构造方法的入口参数(first, second)类型为泛型T;
    14    public Pair(T first, T second) { this.first = first;  this.second = second; }
    15    //定义泛型方法,类型变量T指定了访问方法的返回值和入口参数的类型;
    16    public T getFirst() { return first; }
    17    public T getSecond() { return second; }
    18    //泛型方法,构造方法的入口参数类型为泛型T;
    19    public void setFirst(T newValue) { first = newValue; }
    20    public void setSecond(T newValue) { second = newValue; }
    21 }
     1 package pair3;
     2 
     3 /**
     4  * @version 1.01 2012-01-26
     5  * @author Cay Horstmann
     6  */
     7 public class PairTest3
     8 {
     9    public static void main(String[] args)
    10    {
    11       Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);
    12       Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);
    13      //创建Pair泛型类对象(泛型变量T默认为int型,并将其传递给类型变量T为Manager的类变量buddies;
    14       Pair<Manager> buddies = new Pair<>(ceo, cfo); 
    15       printBuddies(buddies);
    16 
    17       ceo.setBonus(1000000);
    18       cfo.setBonus(500000);
    19       Manager[] managers = { ceo, cfo };
    20 
    21       Pair<Employee> result = new Pair<>();//创建一个Pair类对象,并将其传递给类型变量T为Employee的类变量 result;
    22       minmaxBonus(managers, result);
    23       System.out.println("first: " + result.getFirst().getName() 
    24          + ", second: " + result.getSecond().getName());
    25       maxminBonus(managers, result);
    26       System.out.println("first: " + result.getFirst().getName() 
    27          + ", second: " + result.getSecond().getName());
    28    }
    29    
    30     //?:下限通配符;限定类型变量的下界为Employee类;
    31    public static void printBuddies(Pair<? extends Employee> p) {
    32       Employee first = p.getFirst();
    33       Employee second = p.getSecond();
    34       System.out.println(first.getName() + " and " + second.getName() + " are buddies.");
    35    }
    36        //?:下限通配符;限定类型变量的下界为Manager类;
    37    public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)
    38    {
    39       if (a.length == 0) return;
    40       Manager min = a[0];
    41       Manager max = a[0];
    42       for (int i = 1; i < a.length; i++)
    43       {
    44          if (min.getBonus() > a[i].getBonus()) min = a[i];
    45          if (max.getBonus() < a[i].getBonus()) max = a[i];
    46       }
    47       result.setFirst(min);
    48       result.setSecond(max);
    49    }
    50     //?:下限通配符;限定泛型类Pair类型变量的下界为Manager类;
    51    public static void maxminBonus(Manager[] a, Pair<? super Manager> result)
    52    {
    53       minmaxBonus(a, result);
    54       PairAlg.swapHelper(result); //swapHelper捕获通配符类型 ;
    55    }
    56    // 不能写: public static <T super manager> ...
    57 }
    58 
    59 class PairAlg
    60 {
    61    public static boolean hasNulls(Pair<?> p)//限定泛型类Pair类型变量的下界为p;
    62    {
    63       return p.getFirst() == null || p.getSecond() == null;
    64    }
    65 
    66    public static void swap(Pair<?> p) { swapHelper(p); }
    67 
    68    public static <T> void swapHelper(Pair<T> p)
    69    {
    70       T t = p.getFirst();
    71       p.setFirst(p.getSecond());
    72       p.setSecond(t);
    73    }
    74 }

     

    实验2:编程练习:

    编程练习1:实验九编程题总结

    l 实验九编程练习1总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)

      1 package shiyan9;
      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         
     20         
     21       Personlist1 = new ArrayList<>();
     22  
     23        Scanner scanner = new Scanner(System.in);
     24        File file = new File("E:\面向对象程序设计Java\实验\实验六\身份证号.txt");
     25 
     26         try {
     27              FileInputStream F = new FileInputStream(file);
     28              BufferedReader in = new BufferedReader(new InputStreamReader(F));
     29              String temp = null;
     30              while ((temp = in.readLine()) != null) {
     31                 
     32                 Scanner linescanner = new Scanner(temp);
     33                 
     34                 linescanner.useDelimiter(" ");    
     35                 String name = linescanner.next();
     36                 String id = linescanner.next();
     37                 String sex = linescanner.next();
     38                 String age = linescanner.next();
     39                 String place =linescanner.nextLine();
     40                 Person Person = new Person();
     41                 Person.setname(name);
     42                 Person.setid(id);
     43                 Person.setsex(sex);
     44                 int a = Integer.parseInt(age);
     45                 Person.setage(a);
     46                 Person.setbirthplace(place);
     47                 Personlist1.add(Person);
     48 
     49             }
     50         } catch (FileNotFoundException e) {
     51             System.out.println("查找不到信息");
     52             e.printStackTrace();
     53         } catch (IOException e) {
     54             System.out.println("信息读取有误");
     55             e.printStackTrace();
     56         }
     57         boolean isTrue = true;
     58         while (isTrue) {
     59             System.out.println("******************************************");
     60             System.out.println("1:按姓名字典顺序输出信息;");
     61             System.out.println("2:查询最大年龄与最小年龄人员信息;");
     62             System.out.println("3:按省份找你的同乡;");
     63             System.out.println("4:输入你的年龄,查询年龄与你最近人的信息;");
     64             System.out.println("5:退出");
     65             System.out.println("******************************************");
     66             int type = scanner.nextInt();
     67             switch (type) {
     68             case 1:
     69                 Collections.sort(Personlist1);
     70                 System.out.println(Personlist1.toString());
     71                 break;
     72             case 2:
     73                 
     74                 int max=0,min=100;int j,k1 = 0,k2=0;
     75                 for(int i=1;i<Personlist1.size();i++)
     76                 {
     77                     j=Personlist1.get(i).getage();
     78                    if(j>max)
     79                    {
     80                        max=j; 
     81                        k1=i;
     82                    }
     83                    if(j<min)
     84                    {
     85                        min=j; 
     86                        k2=i;
     87                    }
     88 
     89                 }  
     90                 System.out.println("年龄最大:"+Personlist1.get(k1));
     91                 System.out.println("年龄最小:"+Personlist1.get(k2));
     92                 break;
     93             case 3:
     94                 System.out.println("place?");
     95                 String find = scanner.next();        
     96                 String place=find.substring(0,3);
     97                 String place2=find.substring(0,3);
     98                 for (int i = 0; i <Personlist1.size(); i++) 
     99                 {
    100                     if(Personlist1.get(i).getbirthplace().substring(1,4).equals(place)) 
    101                     {
    102                         System.out.println("你的同乡:"+Personlist1.get(i));
    103                     }
    104                 } 
    105 
    106                 break;
    107             case 4:
    108                 System.out.println("年龄:");
    109                 int yourage = scanner.nextInt();
    110                 int close=ageclose(yourage);
    111                 int d_value=yourage-Personlist1.get(close).getage();
    112                 System.out.println(""+Personlist1.get(close));
    113           
    114                 break;
    115             case 5:
    116            isTrue = false;
    117            System.out.println("再见!");
    118                 break;
    119             default:
    120                 System.out.println("输入有误");
    121             }
    122         }
    123     }
    124     public static int ageclose(int age) {
    125            int m=0;
    126         int    max=53;
    127         int d_value=0;
    128         int k=0;
    129         for (int i = 0; i < Personlist1.size(); i++)
    130         {
    131             d_value=Personlist1.get(i).getage()-age;
    132             if(d_value<0) d_value=-d_value; 
    133             if (d_value<max) 
    134             {
    135                max=d_value;
    136                k=i;
    137             }
    138 
    139          }    return k;
    140         
    141      }
    142 
    143 
    144 
    145 }
    Search
     1 package shiyan9;
     2 
     3 
     4 public class Person implements Comparable<Person> {
     5     private String name;
     6     private String id;
     7     private int age;
     8     private String sex;
     9     private String birthplace;
    10 
    11 public String getname() {
    12 return name;
    13 }
    14 public void setname(String name) {
    15 this.name = name;
    16 }
    17 public String getid() {
    18 return id;
    19 }
    20 public void setid(String id) {
    21 this.id= id;
    22 }
    23 public int getage() {
    24 
    25 return age;
    26 }
    27 public void setage(int age) {
    28 // int a = Integer.parseInt(age);
    29 this.age= age;
    30 }
    31 public String getsex() {
    32 return sex;
    33 }
    34 public void setsex(String sex) {
    35 this.sex= sex;
    36 }
    37 public String getbirthplace() {
    38 return birthplace;
    39 }
    40 public void setbirthplace(String birthplace) {
    41 this.birthplace= birthplace;
    42 }
    43 
    44 public int compareTo(Person o) {
    45 return this.name.compareTo(o.getname());
    46 
    47 }
    48 
    49 public String toString() {
    50 return  name+"	"+sex+"	"+age+"	"+id+"	";
    51 
    52 }
    53 
    54 
    55 
    56 }
    Person
     

    (1)编程练习1总结:

    程序总体结构:主类(Search)和接口(Person);

     模块说明:(1)主类Search:读取文件模块;

                                                   Try/catch模块:监视可能出现的异常;捕捉处理异常;

                                                   Switch/case模块匹配选择5种具体操作;

                    (2)接口(Person)

                           属性声明模块;

                       构造方法模块:getname,setname, getid,setid,getage,setage,getsexsetsexgetbirthplace,setbirthplacecompareTo等;

    目前存在的困难与问题:    文件读取路径的设置影响到能否找到文件;

                                   不论是语法,还是主要的技术(如接口)方面不是娴熟,有些显而易见的东西不能立马想到;

                                    构想正确的设计思路;

    实验九编程练习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 < 11; 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                System.out.println(i+": "+a+"/"+b+"=");
    33                while(b==0){  
    34                    b = (int) Math.round(Math.random() * 100); 
    35                    }
    36                double c = in.nextDouble();
    37                output.println(a+"/"+b+"="+c);
    38                if (c == counter.Chu(a, b)) 
    39                {
    40                    sum += 10;
    41                    System.out.println("恭喜答案正确!");
    42                }
    43                else {
    44                    System.out.println("答案错误!");
    45                } break;
    46             
    47            case 2:
    48                System.out.println(i+": "+a+"*"+b+"=");
    49                int c1 = in.nextInt();
    50                output.println(a+"*"+b+"="+c1);
    51                if (c1 == counter.Cheng(a, b)) {
    52                    sum += 10;
    53                    System.out.println("恭喜答案正确!");
    54                }
    55                else {
    56                    System.out.println("答案错误!");
    57                }break;
    58            case 3:
    59                System.out.println(i+": "+a+"+"+b+"=");
    60                int c2 = in.nextInt();
    61                output.println(a+"+"+b+"="+c2);
    62                if (c2 ==  counter.Jia(a, b)) {
    63                    sum += 10;
    64                    System.out.println("恭喜答案正确!");
    65                }
    66                else {
    67                    System.out.println("答案错误!");
    68                }break ;
    69            case 4:
    70                System.out.println(i+": "+a+"-"+b+"=");
    71                int c3 = in.nextInt();
    72                output.println(a+"-"+b+"="+c3);
    73                if (c3 ==  counter.Jian(a, b)) {
    74                    sum += 10;
    75                    System.out.println("恭喜答案正确!");
    76                }
    77                else {
    78                    System.out.println("答案错误!");
    79                }break ;
    80 
    81              } 
    82     
    83           }
    84         System.out.println("成绩"+sum);
    85         output.println("成绩:"+sum);
    86         output.close();
    87          
    88     }
    89 }    
    Main
     1 package a;
     2 
     3 
     4 public class counter {
     5        private int a;
     6        private int b;
     7 
     8                public int  Jia(int a,int b)
     9                {
    10                    return a+b;
    11                }
    12                public int   Jian(int a,int b)
    13                {
    14                    if((a-b)<0)
    15                        return 0;
    16                    else
    17                    return a-b;
    18                }
    19                public int   Cheng(int a,int b)
    20                {
    21                    return a*b;
    22                }
    23                public int   Chu(int a,int b)
    24                {
    25                    if(b!=0)
    26                    return a/b;    
    27                    else
    28                return 0;
    29                }
    30 
    31                
    32        }
    counter

    编程练习2总结:

    程序结构:主类Main和子类counter两大部分;

    模块说明:Main类:

                (1) 建文件字符流,文件输出模块,将out结果输出到result.txt中;

                        (2)  try/catch模块捕获处理异常;

                        (3)  Switch//case模块:随机生成a,b两个操作数,匹配选择加减乘除四种具体操作;

                         (4)  输出总成绩;

                子类counter类:

                            (1) 属性声明;

                           (2) 构造方法;

     程序设计存在的困难与问题:
                               1.没有全面考虑小学生的实际状况:减法算法结果可能出现负数的情况,以及除法的运算结果直接取整输出,都不符合小学生的学习范围;
                               2.编程过程中,我在变量的 使用范围中出现了问题;
                               3.结果输出到文件中;

                          4.设计思路比较难;                                              

    编程练习2:采用泛型程序设计技术改进实验九编程练习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.拿到一个问题后的解决思路是非常重要的!!!

  • 相关阅读:
    罗尔定理、微分中值定理、广义微分中值定理
    高等数学和数学分析教材推荐及其学习方法浅谈
    音视频下载插件 安装及使用
    win10台式机rtl8188eu(FW 150 UM V2.0)无线网卡无法连接wifi(无法连接到这个网络)
    django模板中的extends和include使用方法
    wordpress中文目录出现“有点尴尬诶!该页无法显示"
    wordpress迁移后登陆时出现Forbidden You don’t have permission to access /wp-login.php on this server
    centos设置开机自启动脚本
    hexo的jacman主题设置语言为英文后偶尔出现中文
    安卓QQ聊天记录导出、备份完全攻略
  • 原文地址:https://www.cnblogs.com/yqj-yf-111/p/9890955.html
Copyright © 2011-2022 走看看