201771010120 苏浪浪 面向对象程序设计(Java)第10周

Posted 小浪o小浪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了201771010120 苏浪浪 面向对象程序设计(Java)第10周相关的知识,希望对你有一定的参考价值。

 

1、实验目的与要求

(1) 理解泛型概念;

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

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

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

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

2、实验内容和步骤

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

测试程序1:

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

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

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

package pair1;

/**
 * @version 1.00 2004-05-10
 * @author Cay Horstmann
 */
public class Pair<T> //引入类型变量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; }
}
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,结合程序运行结果理解程序;

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

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

 

 

测试程序3:

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

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

 

 

实验2:编程练习:

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

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

1程序总体分为两个类、三个模块具体如下:

package aaa;

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 b{
    private static ArrayList<Student> studentlist;
    public static void main(String[] args) {
        studentlist = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        File file = new File("身份证号.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();
                Student student = new Student();
                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 aaa;

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

目前存在的问题:文件读入的地址可能出现错误,接口的定义也可能出现问题;算法的实现也可能会出问题。

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

1程序总体分为两个类、三个模块具体如下:

 package duanyan;
  import java.io.FileNotFoundException;
  import java.io.PrintWriter;
  import java.util.Scanner;
  public class z {
      public static void main(String[] args) {
  
         Scanner in = new Scanner(System.in);
         Student student=new Student();
         PrintWriter out = null;
         try {
            out = new PrintWriter("text.txt");//保存文件模块
        } catch (FileNotFoundException e) {
             e.printStackTrace();
         }
         int sum = 0;
 
         
         //随机生成a、b的值以及随机的运算模块
         for (int i = 1; i <=10; i++) {
             int a = (int) Math.round(Math.random() * 100);
             int b = (int) Math.round(Math.random() * 100);
            int c= (int) Math.round(Math.random() * 3);
 
             
            switch(c)
            {
            case 0:
                System.out.println(i+": "+a+"/"+b+"=");
                
                while(b==0)
                { 
                    b = (int) Math.round(Math.random() * 100);
             }
                
            int C = in.nextInt();
            out.println(a+"/"+b+"="+C);
            if (C == student.d(a, b)) {
                 sum += 10;
                 System.out.println("恭喜答案正确");
             }
             else {
                System.out.println("抱歉,答案错误");
           }
             
             break;
            
           case 1:
               System.out.println(i+": "+a+"*"+b+"=");
                int D = in.nextInt();
                out.println(a+"*"+b+"="+D);
                if (D == student.m(a, b)) {
                   sum += 10;
                    System.out.println("恭喜答案正确");
               }
               else {
                    System.out.println("抱歉,答案错误");
                }
               break;
            case 2:
                System.out.println(i+": "+a+"+"+b+"=");
                while(a<b)
                { 
                    a = (int) Math.round(Math.random() * 100);
             }
                int E = in.nextInt();
               out.println(a+"+"+b+"="+E);
               if (E == student.add(a, b)) {
                   sum += 10;
                    System.out.println("恭喜答案正确");
                }
                else {                    System.out.println("抱歉,答案错误");
                }
                
                break ;
           case 3:
               System.out.println(i+": "+a+"-"+b+"=");
               int F = in.nextInt();
                out.println(a+"-"+b+"="+F);
                if (F == student.r(a, b)) {              
                    sum += 10;
                    System.out.println("恭喜答案正确");
                }
                else {
                    System.out.println("抱歉,答案错误");
                }
                break ;
               } 
           }
        System.out.println("成绩"+sum);
         out.println("成绩:"+sum);
          out.close();       
   }  
   }
package duanyan;
public class Student {
       private int a;
       private int b;
        public int  add(int a,int b)
        {
            return a+b;
        }
        public int   reduce(int a,int b)
        {
            return a-b;
        }
        public int   multiplication(int a,int b)
        {
            return a*b;
        }
        public int   division(int a,int b)
        {
            if(b!=0)
            return a/b;
            else return 0;
        }

    }

存在问题的:算法的实现可能出现错误,子类的定义可能出现错误,除法运算中被除数b不能等于0可能会考虑不到,减法运算中a>b可能会注意不到,

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

 package 算;
  import java.io.FileNotFoundException;
  import java.io.PrintWriter;
  import java.util.Scanner;
  public class z {
      public static void main(String[] args) {
  
         Scanner in = new Scanner(System.in);
         Student student=new Student();
         PrintWriter out = null;
         try {
            out = new PrintWriter("text.txt");
        } catch (FileNotFoundException e) {
             e.printStackTrace();
         }
         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 c= (int) Math.round(Math.random() * 3);
 
             
            switch(c)
            {
            case 0:
                System.out.println(i+": "+a+"/"+b+"=");
                
                while(b==0)
                { 
                    b = (int) Math.round(Math.random() * 100);
             }
                
            int C = in.nextInt();
            out.println(a+"/"+b+"="+C);
            if (C == student.d(a, b)) {
                 sum += 10;
                 System.out.println("恭喜答案正确");
             }
             else {
                System.out.println("抱歉,答案错误");
           }
             
             break;
            
           case 1:
               System.out.println(i+": "+a+"*"+b+"=");
                int D = in.nextInt();
                out.println(a+"*"+b+"="+D);
                if (D == student.m(a, b)) {
                   sum += 10;
                    System.out.println("恭喜答案正确");
               }
               else {
                    System.out.println("抱歉,答案错误");
                }
               break;
            case 2:
                System.out.println(i+": "+a+"+"+b+"=");
                while(a<b)
                { 
                    a = (int) Math.round(Math.random() * 100);
             }
                int E = in.nextInt();
               out.println(a+"+"+b+"="+E);
               if (E == student.add(a, b)) {
                   sum += 10;
                    System.out.println("恭喜答案正确");
                }
                else {                    System.out.println("抱歉,答案错误");
                }
                
                break ;
           case 3:
               System.out.println(i+": "+a+"-"+b+"=");
               int F = in.nextInt();
                out.println(a+"-"+b+"="+F);
                if (F == student.r(a, b)) {              
                    sum += 10;
                    System.out.println("恭喜答案正确");
                }
                else {
                    System.out.println("抱歉,答案错误");
                }
                break ;
               } 
           }
        System.out.println("成绩"+sum);
         out.println("成绩:"+sum);
          out.close();       
   }  
   }
package 算;
public class Student<T> {
       private T a;
       private T b;
       public Student() {
           a = null;
           b = null;
       }
      
       public Student(T a, T b) {
           this.a = a;
           this.b = b;
       }

    public int  add(int a,int b)
    {
        return a+b;
    }
    public int   r(int a,int b)
    {
    
        return a-b;
    }
    public int   m(int a,int b)
    {
        return a*b;
    }
    public int   d(int a,int b)
    {
       
        return a/b;
      
    }

}

 实验总结:本章学习了泛型程序设计,泛型程序设计可以让程序更可读、更安全。它舍弃了不安全的强制转换类型;通过老师与主教学长的帮助,知道了什么是泛型程序设计,也知道了通配符是什么;在本次的实验中野实践了泛型程序设计。

以上是关于201771010120 苏浪浪 面向对象程序设计(Java)第10周的主要内容,如果未能解决你的问题,请参考以下文章

苏浪浪 201771010120《面向对象程序设计(java)》第六章学习总结

苏浪浪 201771010120《面向对象程序设计(java)》第八周学习总结

苏浪浪 201771010120 《面向对象程序设计(java)》第9周学习总结

201771010120 苏浪浪 面向对象程序设计(Java)第10周

201771010120 苏浪浪

17 苏浪浪 201771010120