-
Files & Streams
Can anyone please help me to write this program, am new to programming & am having a hard time with this... anyone?
Here is what i am trying to do:
Using the student record below, i am trying to write a program that allows me to input student records and creates an output file using object serialization. Then i have to write another program that reads the file created by the first program and displays, to the screen or in a GUI,each student’s ID, name and letter grade. Use the following scale to determine the letter
grade:
A 90-100%
B 80-89%
C 70-79%
D 60-69%
F Below 60%
//
package studentRecord;
import java.io.Serializable;
public class StudentRecord implements Serializable{
private int ID;
private String firstName;
private String lastName;
private double score;
public StudentRecord()
{
this( 0, "", "", 0.0 );
}
public StudentRecord(
int id, String first, String last, double s )
{
setID( id );
setFirstName( first );
setLastName( last );
setScore( s );
}
public void setID( int id )
{
ID = id;
}
public int getID()
{
return ID;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setScore( double s )
{
score = s;
}
public double getScore()
{
return score;
}
}
-
Inorder to write to the file you need to implement the following code:
try
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("name of your file));
out.write("write your entire class here");
}
catch(Exception e)
{
System.out.println(" An Error Occurred" + e)
}
finallly
{
if(out!=null)
{
try
{
out.close();
}
catch(Exception ee)
{
System.out.println(ee.toString());
}
}
}
Inorder to read back the contents of the file you need to implement the following code
Set ur Object to null here i,e if ur Object name is Subject;
Subject position = null;
if u r reading more than one object u sud use arrays or linked lists or other data storage medium.
try
{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("nameofyour file goes here"));
while(true)//while it is not the end of the file;
{
position = in.readObject(Subject);
System.out.println(position);
}
}
catch(Exception eee)
{
System.out.println(e.toString());
}
-
sorry sud have been "System.out.println(position.toString());
-
Files and streams
Thank you Sanchit.... i wil try that... thanks again
-
Your "setScore" will want to accumulate over time the scores given to the students in the class. Perhaps you'll want your setScore method as being
score += s;
Have you thought about your "GradeReport" class/program? If so, what does it look like, so far?
Similar Threads
-
By acaravia in forum .NET
Replies: 1
Last Post: 03-20-2006, 01:42 PM
-
By Mansoor in forum Enterprise
Replies: 2
Last Post: 09-10-2002, 10:38 PM
-
Replies: 8
Last Post: 08-23-2002, 04:35 PM
-
By Pat Meaney in forum Java
Replies: 0
Last Post: 03-13-2001, 02:54 PM
-
By René Whitworth in forum vb.announcements
Replies: 1
Last Post: 04-29-2000, 02:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|