package Test;
public class Student2 {
private String name;
private int age;
private double score;
private int year;
private int month;
private int day;
public Student2(String name,int age,int year,int month,int day,double score){
this.name = name;
this.age = age;
this.year = year;
this.month = month;
this.day = day;
this.score = score;
}
public Student2(){};
public void set(String name,int age,int year,int month,int day,double score)
{
this.name = name;
this.age = age;
this.year = year;
this.month = month;
this.day = day;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public void show(){
System.out.printf("name: %15s age: %3d year: %5d month: %3d day: %3d markOfJava: %6.2f",
this.name,this.age, this.year,this.month,this.day,this.score);
}
}
ErrorInputException类
package Test;
public class ErrorInputException extends Exception{
String message;
public ErrorInputException(String str)
{
message=str+"字符串不能转换成数值,出现异常";
}
public String ToString()
{
return message;
}
}
主程序Lab3_3类
package Mypackage;
import Test.ErrorInputException;
import Test.Student2;
import java.util.Scanner;
import static java.lang.System.exit;
public class Lab3_3 {
interface Average {
public double ave(Test.Student2[] students);
}
public static class Type1 implements Average {
@Override
public double ave(Test.Student2[] students) {
double sum = 0.0;
for(int i=0;i< students.length;i++) {
sum += students[i].getScore();
}return sum / students.length;
}
}
public static class Type2 implements Average {
@Override
public double ave(Test.Student2[] students) {
double max = students[0].getScore();
double min = students[0].getScore();
double sum = 0.0;
for (Test.Student2 student : students) {
if (student.getScore() >= max) {
max = student.getScore();
} else {
if (student.getScore() <= min) {
min = student.getScore();
}
}
}
for (Test.Student2 student : students) {
if (student.getScore() != max && student.getScore() != min) {
sum += student.getScore();
}
}
return sum / (students.length-2);
}
}
public static void main(String[] args) throws ErrorInputException {
System.out.println("请输入学生人数:");
Scanner input = new Scanner(System.in);
int numOfStudent = input.nextInt();//学生人数
input.nextLine();
Test.Student2[] students = new Test.Student2[numOfStudent];
String name;
String age;
String year;
String month;
String day;
String score;
for (int i = 0; i < numOfStudent; i++) {
students[i]=new Test.Student2();
System.out.println("请依次输入name,age,year,month,day,score");
name = input.next();
try {
age = input.next();
year = input.next();
month = input.next();
day = input.next();
score = input.next();
for (char str : age.toCharArray()) {
if(str<'0'||str>'9')
throw new ErrorInputException(age);
}
for (char str : year.toCharArray()) {
if(str<'0'||str>'9')
throw new ErrorInputException(year);
}
for (char str : month.toCharArray()) {
if(str<'0'||str>'9')
throw new ErrorInputException(month);
}
for (char str : day.toCharArray()) {
if(str<'0'||str>'9')
throw new ErrorInputException(day);
}
for (char str : score.toCharArray()) {
if(str<'0'||str>'9')
throw new ErrorInputException(score);
}
students[i].set(name,Integer.valueOf(age),Integer.valueOf(year),Integer.valueOf(month),Integer.valueOf(day),Double.valueOf(score));
} catch (ErrorInputException e) {
System.out.println(e.ToString());
exit(0);
}finally {
System.out.println("");
}
}//对数组进行输入,输入了学生信息
Lab3_3.Type1 a=new Lab3_3.Type1();
Lab3_3.Type2 a2=new Lab3_3.Type2();
System.out.println("第一种平均成绩为"+a.ave(students));
System.out.println("第二种平均成绩为"+a2.ave(students));
}
}
实验总结分析
在调试的时候出现了很多问题,
1.第一个问题是显示students[i]为空,检查得知和实验一的问题一样,我在Student2[] students=new Student2[numofStudent]后,没有对每一个students进行动态内存分配,于是加了个for循环,students[i]=new Student2();问题得以解决
2.本题需要判断输入的字符串能不能转换为数值,而此前我用Student2这个类的时候,像year month成员变量都不是String字符串类型,而这里必须要为字符串类型,所以上网搜了下,可以在初始化initialize的时候将String转换为int类型和double类型,分别用Integer.valueof()和Double.valueof()即可正确的初始化赋值。
3.
最开始我把对students[i]的初始化赋值写在了异常处理的前面,导致如果有异常的话,还没来得及处理,就执行赋值这一步,会出现错误,经调整后,把这个初始化赋值代码调整到异常处理后,问题得以解决。
4.在写此代码的时候用到了类的封装性访问权限的知识,最开始在Student2类中无参初始化的访问权限为缺省,而我这里Studen2类和Lab3_3类不是在同一个包中,缺省权限的访问控制只能由类内或包内其他类访问,所以我将Student2无参构造的权限改为了public,就得以在Lab3_3中得以调用
5.用到了内部类的知识。在Lab3_3类中,Type1和Type2都是内部类,内部类要靠外部类的实例化来访问,所以当我想调用Type1中的方法时,要先Lab3_3.Type1 a=new Lab3_3.Type1();来构造一个实例化,然后用a.ave()调用Type1中的方法。