Files & Streams


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Files & Streams

  1. #1
    Join Date
    May 2006
    Posts
    3

    Question 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;
    }
    }

  2. #2
    Join Date
    May 2006
    Posts
    2
    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());
    }

  3. #3
    Join Date
    May 2006
    Posts
    2
    sorry sud have been "System.out.println(position.toString());

  4. #4
    Join Date
    May 2006
    Posts
    3

    Files and streams

    Thank you Sanchit.... i wil try that... thanks again

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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

  1. Parsing/Converting large files
    By acaravia in forum .NET
    Replies: 1
    Last Post: 03-20-2006, 12:42 PM
  2. XML files vs Database
    By Mansoor in forum Enterprise
    Replies: 2
    Last Post: 09-10-2002, 10:38 PM
  3. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 04:35 PM
  4. Replies: 0
    Last Post: 03-13-2001, 01:54 PM
  5. Replace In Files utility released
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center