第十周作业
本人学号:201771010138
姓名:邹丰蔚
《面向对象程序设计(java)》第十周学习总结
1、实验目的与要求
(1) 理解泛型概念;
(2) 掌握泛型类的定义与使用;
(3) 掌握泛型方法的声明与使用;
(4) 掌握泛型接口的定义与实现;
(5)了解泛型程序设计,理解其用途。
2、实验内容和步骤
实验1: 导入第8章示例程序,测试程序并进行代码注释。
测试程序1:
l 编辑、调试、运行教材311、312页 代码,结合程序运行结果理解程序;
l 在泛型类定义及使用代码处添加注释;
l 掌握泛型类的定义及使用。
Ÿ 编辑、编译、调试运行教材PersonTest程序(教材163页-165页);
Ÿ 掌握超类的定义及其使用要求;
Ÿ 掌握利用超类扩展子类的要求;
Ÿ 在程序中相关代码处添加新知识的注释。
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);
}
}
Pair: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; }
}
测试程序2:
l 编辑、调试运行教材315页 PairTest2,结合程序运行结果理解程序;
l 在泛型程序设计代码处添加相关注释;
l 掌握泛型方法、泛型变量限定的定义及用途。
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);
}
}
Pair:package pair2;
/**
* @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; }
}
测试程序3:
l 用调试运行教材335页 PairTest3,结合程序运行结果理解程序;
l 了解通配符类型的定义及用途。
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);
}
}
Pair: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; }
}
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;
}
}
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;
}
}
实验2:编程练习:
编程练习1:实验九编程题总结
实验九编程练习1总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)。
实验九编程练习2总结(从程序总体结构说明、模块说明,目前程序设计存在的困难与问题三个方面阐述)。
1:
总体结构说明:
主类:Main 子类:Student
模块说明:
Main:查找文件,对文件进行读取。
Student:对文件进行具体处理。
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+"\t"+sex+"\t"+year+"\t"+number+"\t"+province+"\n"; }
public int getyear() { // TODO Auto-generated method stub return 0; }
public void setname(String name2) { // TODO Auto-generated method stub
} }
|
存在问题:
- 对异常的出现处理之后还是不能使得编程问题得到解决,由于对问题的分析不够所导致。
- Main类和Student类的变量名有时不能统一。
2:
总体结构说明:
主类:test
模块说明:
Test类:
数据域:sum,总分数
Core负责随机生成题目,并对其打上分数。
Main(String[]args)是程序的入口,负责程序的开始。
|
存在问题:
除不尽的计算无法实行,无论什么答案都是错误。
编程练习2:采用泛型程序设计技术改进实验九编程练习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;
}
}
}
|
总结:通过这次实验,我了解了泛型类,泛型方法、通配符、泛型变量上界、泛型变量下界。我对之前的实验进行了总结,对之前的知识进行进一步的回顾,感觉依旧存在些小问题,希望在接下来的实验中要更好的解决。