对象序列化:每一个对象都关联一个序列号。
其算法:
- 对你遇到的每一个对象引用都关联一个序列号
- 对于每个对象,当第一次被遇到时,保存其对象数据到流中。
- 如果某个对象之前已经被保存过,那么只写出“与之前保存过的序列号为x的 对象相同”。在读回时相反。
- 对于六中的对象,在第一次遇到其序列号时,构建它,并使用流中数据来初始化它,然后记录这个顺序号和新对象之间的关联。
- 当遇到“与之前保存过的序列号为x的对象相同”标记时,获取与这个顺序号相关联的对象引用。
实现了从控制台写入对象
package version1;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class ScoreSort
{
Scanner scanner;
Course[] course;
int number;
File filename;
private double avg_gpa = 0;
public ScoreSort()
{
System.out.print("输入课程的个数:/n");
scanner=new Scanner(System.in);
number=scanner.nextInt();
try
{
if(number<=0) throw new NegativeArraySizeException();
input(number);
writeToFile(course);
}
catch(NegativeArraySizeException e)
{
JOptionPane.showMessageDialog(null,"人数小于1!");
}
}
public void input(int n)
{
course=new Course[n];
//System.out.println("课程号 课程名 学分 成绩 ");
for(int i=0;i<course.length;i++)
{ //输入学生的信息
int j = i+1;
System.out.println("输入第"+ j +"门课程");
System.out.println("课程号");
long id=scanner.nextLong();
System.out.println("课程名");
String name=scanner.next();
System.out.println("学分");
int credit=scanner.nextInt();
System.out.println("成绩");
int score=scanner.nextInt();
System.out.println("此课程的绩点为:");
course[i]=new Course(id,name,credit,score);
System.out.println(course[i].getGPA());
System.out.println("___________________");
}
}
public double calAvgGPA(Course course[])
{
double total_credit = 0;
double weigh_score = 0;
for(int i=0;i<course.length;i++)
{
total_credit += course[i].getCourse_credit();
weigh_score += (course[i].getCourse_gpa() * course[i].getCourse_credit());
}
avg_gpa = weigh_score / total_credit;
return avg_gpa;
}
public void writeToFile(Course[] s)
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream( "student.dat "));
oos.writeObject(course);//这里存的是数组对象,你也可以用循环把每一个Course对象写进去。
oos.close();
}
catch(IOException ex)
{
System.out.println("写入文件失败!");
}
}
public double getAvg_gpa() {
return avg_gpa;
}
}
ScoreSort scoresort=new ScoreSort();
scoresort.calAvgGPA(scoresort.course);
System.out.println("平均GPA为:" + scoresort.getAvg_gpa());
package version1;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class ScoreSort
{
Scanner scanner;
Course[] course;
int number;
File filename;
private double avg_gpa = 0;
public ScoreSort()
{
System.out.print("输入课程的个数:/n");
scanner=new Scanner(System.in);
number=scanner.nextInt();
try
{
if(number<=0) throw new NegativeArraySizeException();
input(number);
writeToFile(course);
}
catch(NegativeArraySizeException e)
{
JOptionPane.showMessageDialog(null,"人数小于1!");
}
}
public void input(int n)
{
course=new Course[n];
//System.out.println("课程号 课程名 学分 成绩 ");
for(int i=0;i<course.length;i++)
{ //输入学生的信息
int j = i+1;
System.out.println("输入第"+ j +"门课程");
System.out.println("课程号");
long id=scanner.nextLong();
System.out.println("课程名");
String name=scanner.next();
System.out.println("学分");
int credit=scanner.nextInt();
System.out.println("成绩");
int score=scanner.nextInt();
System.out.println("此课程的绩点为:");
course[i]=new Course(id,name,credit,score);
System.out.println(course[i].getGPA());
System.out.println("___________________");
}
}
public double calAvgGPA(Course course[])
{
double total_credit = 0;
double weigh_score = 0;
for(int i=0;i<course.length;i++)
{
total_credit += course[i].getCourse_credit();
weigh_score += (course[i].getCourse_gpa() * course[i].getCourse_credit());
}
avg_gpa = weigh_score / total_credit;
return avg_gpa;
}
public void writeToFile(Course[] s)
{
try
{
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream( "student.dat "));
oos.writeObject(course);//这里存的是数组对象,你也可以用循环把每一个Course对象写进去。
oos.close();
}
catch(IOException ex)
{
System.out.println("写入文件失败!");
}
}
public double getAvg_gpa() {
return avg_gpa;
}
}
ScoreSort scoresort=new ScoreSort();
scoresort.calAvgGPA(scoresort.course);
System.out.println("平均GPA为:" + scoresort.getAvg_gpa());