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

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

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

    一.l何谓泛型程序设计:

    1. JDK5.0中增加的泛型类型,是Java语言中类型 安全的一次重要改进。
    2. 泛型:也称参数化类型(parameterizedtype), 就是在定义类、接口和方法时,通过类型参数指 示将要处理的对象类型。
    3. 泛型程序设计(Genericprogramming):编写 代码可以被很多不同类型的对象所重用。

    二.泛型接口的定义

      1.定义

    public interface IPool <T>

     {

          T get();

    int add(T t);

     }

    2.实现:

    public class GenericPool<T> implements IPool<T>

    {

     }

    }

    public class GenericPoolimplements IPool<Account>

    {

    }

    二.泛型变量的限定:

    l1.定义泛型变量的上界: public class NumberGeneric< T extends Number>

    1. 泛型变量上界的说明

    (1)上述声明规定了NumberGeneric类所能处理的 泛型变量类型需和Number有继承关系; u

    (2)extends关键字所声明的上界既可以是一个类, 也可以是一个接口;

    l3.<TextendsBoundingType>表示T应该是绑定 类型的子类型。

    l4.一个类型变量或通配符可以有多个限定,限定类 型用“&”分割。

    三.泛型类的约束与局限性

    1. 不能用基本类型实例化类型参数 l
    2. 运行时类型查询只适用于原始类型 l
    3. 不能抛出也不能捕获泛型类实例 l
    4. 参数化类型的数组不合法 l
    5. 不能实例化类型变量 l
    6. 泛型类的静态上下文中类型变量无效 l
    7. 注意擦除后的冲突

    四.泛型类型的继承规则

    1. Java中的数组是协变的(covariant)。
    2. Java中泛型类不具协变性。如果能够将 List<Integer>赋给List<Number>。那么下面的代码 就允许将非Integer的内容放入List<Integer>:

    List<Integer> li = new ArrayList<Integer>();

     List<Number> ln = li; // illegal

    ln.add(new Float(3.1415));

    1. 泛型类可扩展或实现其它的泛型类。

    五.通配符类型及使用方法

    1. 通配符:

      1)“?”符号表明参数的类型可以是任何一种类 型,它和参数T的含义是有区别的。T表示一种 未知类型,而“?”表示任何一种类型。这种 通配符一般有以下三种用法:

      –单独的,用于表示任何类型。 –?extends type,表示带有上界。 –super type,表示带有下界。

    第二部分:实验部分

    1、实验目的与要求

    (1) 理解泛型概念;

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

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

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

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

    2、实验内容和步骤

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

    测试程序1:

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

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

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

    Pair<T>

    package pair1;

    /**

     * @version 1.00 2004-05-10

     * @author Cay Horstmann

     */

    public class Pair<T>

    {

       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; }

    }

    PairTest1

    package pair1;

    /**

     * @version 1.01 2012-01-26

     * @author Cay Horstmann

     */

    public class PairTest1

    {

       public static void main(String[] args)

       {

          String[] words = { "Mary", "had", "a", "little", "lamb" };

          Pair<String> mm = ArrayAlg.minmax(words);

          System.out.println("min = " + mm.getFirst());

          System.out.println("max = " + mm.getSecond());

       }

    }

    class ArrayAlg

    {

       /**

        * Gets the minimum and maximum of an array of strings.

        * @param a an array of strings

        * @return a pair with the min and max value, or null if a is null or empty

        */

       public static Pair<String> minmax(String[] a)

       {

          if (a == null || a.length == 0) return null;

          String min = a[0];

          String max = a[0];

          for (int i = 1; i < a.length; i++)

          {

             if (min.compareTo(a[i]) > 0) min = a[i];

             if (max.compareTo(a[i]) < 0) max = a[i];

          }

          return new Pair<>(min, max);

       }

    }

    测试结果:

     

    测试程序2:

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

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

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

    测试程序:

    Pair<T>

    package pair1;

    /**

     * @version 1.00 2004-05-10

     * @author Cay Horstmann

     */

    public class Pair<T>

    {

       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; }

    }

    PairTest2

    package pair2;

    import java.time.*;

    /**

     * @version 1.02 2015-06-21

     * @author Cay Horstmann

     */

    public class PairTest2

    {

       public static void main(String[] args)

       {

          LocalDate[] birthdays =

             {

                LocalDate.of(1906, 12, 9), // G. Hopper

                LocalDate.of(1815, 12, 10), // A. Lovelace

                LocalDate.of(1903, 12, 3), // J. von Neumann

                LocalDate.of(1910, 6, 22), // K. Zuse

             };

          Pair<LocalDate> mm = ArrayAlg.minmax(birthdays);

          System.out.println("min = " + mm.getFirst());

          System.out.println("max = " + mm.getSecond());

       }

    }

    class ArrayAlg

    {

       /**

          Gets the minimum and maximum of an array of objects of type T.

          @param a an array of objects of type T

          @return a pair with the min and max value, or null if a is

          null or empty

       */

       public static <T extends Comparable> Pair<T> minmax(T[] a)

       {

          if (a == null || a.length == 0) return null;

          T min = a[0];

          T max = a[0];

          for (int i = 1; i < a.length; i++)

          {

             if (min.compareTo(a[i]) > 0) min = a[i];

             if (max.compareTo(a[i]) < 0) max = a[i];

          }

          return new Pair<>(min, max);

       }

    }

    测试结果:

     

    测试程序3:

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

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

    实验程序;

    Employee

    package pair3;

    import java.time.*;

    public class Employee

    {  

       private String name;

       private double salary;

       private LocalDate hireDay;

       public Employee(String name, double salary, int year, int month, int day)

       {

          this.name = name;

          this.salary = salary;

          hireDay = LocalDate.of(year, month, day);

       }

       public String getName()

       {

          return name;

       }

       public double getSalary()

       {  

          return salary;

       }

       public LocalDate getHireDay()

       {  

          return hireDay;

       }

       public void raiseSalary(double byPercent)

       {  

          double raise = salary * byPercent / 100;

          salary += raise;

       }

    }

    Manager

    package pair3;

    public class Manager extends Employee

    {  

       private double bonus;

       /**

          @param name the employee's name

          @param salary the salary

          @param year the hire year

          @param month the hire month

          @param day the hire day

       */

       public Manager(String name, double salary, int year, int month, int day)

       {  

          super(name, salary, year, month, day);

          bonus = 0;

       }

       public double getSalary()

       {

          double baseSalary = super.getSalary();

          return baseSalary + bonus;

       }

       public void setBonus(double b)

       {  

          bonus = b;

       }

       public double getBonus()

       {  

          return bonus;

       }

    }

    Pair<T>

    package pair3;

    /**

     * @version 1.00 2004-05-10

     * @author Cay Horstmann

     */

    public class Pair<T>

    {

       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; }

    }

    PairTest3

    package pair3;

    /**

     * @version 1.01 2012-01-26

     * @author Cay Horstmann

     */

    public class PairTest3

    {

       public static void main(String[] args)

       {

          Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);

          Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);

          Pair<Manager> buddies = new Pair<>(ceo, cfo);      

          printBuddies(buddies);

          ceo.setBonus(1000000);

          cfo.setBonus(500000);

          Manager[] managers = { ceo, cfo };

          Pair<Employee> result = new Pair<>();

          minmaxBonus(managers, result);

          System.out.println("first: " + result.getFirst().getName()

             + ", second: " + result.getSecond().getName());

          maxminBonus(managers, result);

          System.out.println("first: " + result.getFirst().getName()

             + ", second: " + result.getSecond().getName());

       }

       public static void printBuddies(Pair<? extends Employee> p)

       {

          Employee first = p.getFirst();

          Employee second = p.getSecond();

          System.out.println(first.getName() + " and " + second.getName() + " are buddies.");

       }

       public static void minmaxBonus(Manager[] a, Pair<? super Manager> result)

       {

          if (a.length == 0) return;

          Manager min = a[0];

          Manager max = a[0];

          for (int i = 1; i < a.length; i++)

          {

             if (min.getBonus() > a[i].getBonus()) min = a[i];

             if (max.getBonus() < a[i].getBonus()) max = a[i];

          }

          result.setFirst(min);

          result.setSecond(max);

       }

       public static void maxminBonus(Manager[] a, Pair<? super Manager> result)

       {

          minmaxBonus(a, result);

          PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type

       }

       // Can't write public static <T super manager> ...

    }

    class PairAlg

    {

       public static boolean hasNulls(Pair<?> p)

       {

          return p.getFirst() == null || p.getSecond() == null;

       }

       public static void swap(Pair<?> p) { swapHelper(p); }

       public static <T> void swapHelper(Pair<T> p)

       {

          T t = p.getFirst();

          p.setFirst(p.getSecond());

          p.setSecond(t);

       }

    }

    测试结果:

     

    实验2:编程练习:

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

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

       总体结构说明:主类:Main      子类:People

       模块说明:主类Main:读取文件txt,将文件导入程序,并对文件选择进行五种不同的操作

                 子类People:声明属性,对数据进行处理

    Main

    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("输入有误");

                }

            }

        }

     

    }

    People:

    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总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)。

       总体结构说明:主类:实验3

       模块说明:随机选取数据,生成十个题目进行四则运算,并与输入的结果进行比较判断对错,并计算得分

    主类实验3:

    package 实验3-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;

    }

    }

    }  

       目前的困难与问题:没有将最终结果输入到txt文档中,而且再进行除法运算时随机生成的数据最低位为小数点后一位

    编程练习2:采用泛型程序设计技术改进实验九编程练习2,使之可处理实数四则运算,其他要求不变。

    实验程序:

    package test;

    import java.util.Random;

    import java.io.FileOutputStream;

    import java.io.PrintStream;

    import java.io.FileNotFoundException;

    import java.util.Scanner;

    public class 实验3{

    int sum;

    public static void main(String[] args) throws FileNotFoundException {

    实验3 t=new 实验3();
    PrintStream out = System.out;
    PrintStream ps = new PrintStream("C:\Users\张季跃\Desktop\第十周实验报告\text.txt");
    System.setOut(ps);
    System.out.println("kaishiks");

    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() throws FileNotFoundException {

    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;

    }

    PrintStream out = System.out;
    break;

    }

    }

    }

    实验结果:

    第三部分:实验总结:

         这一周我主要学习了泛型程序设计,泛型接口的定义,泛型变量的限定和通配符类型及使用方法,但在本周的实验过程中我发现我对泛型的掌握还是不熟练,即使是修改上一次的程序也力不从心。其次,在对上一次实验报告的总结中我也发现了自己在上一次程序中的许多错误,并尝试对其进行改正。

  • 相关阅读:
    ado GetRows
    mysql数据库学习——2,数据库的选定,创建,删除和变更
    mysql数据库学习——4,完整性约束
    mssql数据集操作方法
    mysql数据库学习——1,获取原数据
    mysql书籍
    php学习——smarty
    mysql数据库学习——5,数据类型,字符集和校对
    phpcms——评论内容字符控制
    phpcms权限问题,父栏目权限应用到子栏目不管用
  • 原文地址:https://www.cnblogs.com/Alex-Morcer/p/9903663.html
Copyright © 2011-2022 走看看