java程序设计,求代码 1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造
java程序设计,求代码1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造方法完成数据的初始化。2.在主类中创建3个学生对象,并调用学生类的有参的构...
java程序设计,求代码
1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造方法完成数据的初始化。
2.在主类中创建3个学生对象,并调用学生类的有参的构造方法初始化学生信息(学生信息直接在程序中输入即可),将学生信息使用对象流输入保存到文件(data.txt),再使用对象输入流读取这些对象,求他们的平均成绩并输出。 展开
1.定义学生类,学生类有学号,姓名,语文成绩,数学成绩的属性和有参的构造方法完成数据的初始化。
2.在主类中创建3个学生对象,并调用学生类的有参的构造方法初始化学生信息(学生信息直接在程序中输入即可),将学生信息使用对象流输入保存到文件(data.txt),再使用对象输入流读取这些对象,求他们的平均成绩并输出。 展开
展开全部
哥们你稍微等等,我第二道快出来了,有点难,卡在了多个对象保存。10来分钟能弄出来。
追答
package ex1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Student3 implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String num;
private String name;
private int chinese;
private int math;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public String toString() {
return "Student3 [num=" + num + ", name=" + name + ", chinese=" + chinese + ", math=" + math + "]";
}
public Student3(String num, String name, int chinese, int math) {
super();
this.num = num;
this.name = name;
this.chinese = chinese;
this.math = math;
}
public Student3() {
super();
}
public static void writeObjectToFile(List obj)
{
File file =new File("date.txt");
FileOutputStream out;
try {
out = new FileOutputStream(file);
ObjectOutputStream objOut=new ObjectOutputStream(out);
objOut.writeObject(obj);
objOut.flush();
objOut.close();
System.out.println("write object success!");
} catch (IOException e) {
System.out.println("write object failed");
e.printStackTrace();
}
}
public static Object readObjectFromFile()
{
Object temp=null;
File file =new File("date.txt");
FileInputStream in;
try {
in = new FileInputStream(file);
ObjectInputStream objIn=new ObjectInputStream(in);
temp=objIn.readObject();
objIn.close();
System.out.println(
"read object success!");
} catch (IOException e) {
System.out.println("read object failed");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return temp;
}
public static void main(String[] args) {
List list = new ArrayList();
Student3 student3 = new Student3();
Student3 student1 = new Student3();
Student3 student2 = new Student3();
student3.setChinese(60);
student3.setMath(100);
student3.setName("张三");
student3.setNum("201102");
student2.setChinese(70);
student2.setMath(90);
student2.setName("李四");
student2.setNum("201103");
student1.setChinese(80);
student1.setMath(90);
student1.setName("王五");
student1.setNum("201105");
list.add(student3);
list.add(student2);
list.add(student1);
writeObjectToFile(list);
Student3 student33 = new Student3();
Student3 student11 = new Student3();
Student3 student22 = new Student3();
List list2 = new ArrayList();
list2=(List)readObjectFromFile();
student11=list2.get(0);
student22=list2.get(1); student33=list2.get(2);
System.out.println(student11.getName()+"的平均成绩为"+(student11.getChinese()+student11.getMath())/2);
System.out.println(student22.getName()+"的平均成绩为"+(student22.getChinese()+student22.getMath())/2);
System.out.println(student33.getName()+"的平均成绩为"+(student33.getChinese()+student33.getMath())/2);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询