Page 1 of 4 123 ... LastLast
Results 1 to 20 of 61
Like Tree1Likes

Thread: Code Help: Serialization

  1. #1
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default Code Help: Serialization

    I honestly don't even know how to begin with this problem.

    Given the UML class diagram for class GpaRecord and a serialized file containing GpaRecord objects, produce the following:
    1. Write the Java class file for GpaRecord, including overriding the toString method. Output of the toString method should match the output provided with these instructions.
    2. Write code to read the provided serialized file using your class file. Use the toString of the GpaRecord class to output the instance variables. Use the FileInputStream and ObjectInputStream classes. HINT: This is most easily done by writing a class file with the processing code needed, then writing a test harness to instantiate the previous class and execute the methods in that class (refer to examples in the text). The processing code must include try, catch, and finally blocks as appropriate. Specific exceptions that must be caught include:
    a. ClassNotFoundException
    b. IOException
    c. EOFException
    3. For each time an exception is caught, include a message for the user describing the problem. You do not need to attempt to recover from the problem.
    4. Once the basic processing requirements have been successfully met, add code to the processing class to do the following (NOTE: You may not use an array for this processing; you may not read the file more than once):
    a. Count the number of records in the serialized file
    b. Calculate an overall average GPA for all records in the file
    c. Print the count of records and the average GPA in the format shown below
    5. BONUS QUESTION (5 points): Tell me what all the names in the serialized file have in common.

    Sample toString Output
    Jerry Springer
    Student ID: 12345678
    Class Grouping: 1 and GPA = 1.3578

    Sample Output (additional processing)
    The number of students is: 26
    The average GPA is 3.2145

    She sent a UML diagram which looks like:

    Ronn Kerner
    Student ID: 23652147
    Class Grouping: 3 and GPA=3.662000

    She also sent a StudentGpa.ser file
    Last edited by bwilson0117; 07-20-2011 at 05:59 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,920
    Rep Power
    19

    Default

    Start from the beginning...point 1.

  3. #3
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Is this a good start? The lines in blue are where I am still seeing errors, but it's just a start.

    Java Code:
    import java.io.ObjectInputStream;
    	import java.io.IOException;
    	import java.io.FileInputStream;
    	import java.io.EOFException;
    
    
    	[COLOR="blue"]import GpaRecordSerializable;[/COLOR]
    
    	public class GpaRecord
    	{
    	   private ObjectInputStream input;
    
    	   // enable user to select file to open
    	   public void openFile()
    	   {
    	      try // open file
    	      {
    	         input = new ObjectInputStream(
    	            new FileInputStream( "clients.ser" ) );
    	      } // end try
    	      catch ( IOException ioException )
    	      {
    	         System.err.println( "Error opening file." );
    	      } // end catch
    	   } // end method openFile
    
    	   // read record from file
    	   public void readRecords()
    	   {
    	      GpaRecordSerializable record;
    	      System.out.printf( "%-10s%-12s%-12s%10s\n", "Account",
    	         "First Name", "Last Name", "Balance" );
    
    	      try // input the values from the file
    	      { 
    	         while ( true )
    	         {
    	            record = ( GpaRecordSerializable ) input.readObject();
    
    	            // display record contents
    	            System.out.printf( "%-10d%-12s%-12s%10.2f\n",
    	               [COLOR="blue"]record.getGpa(), record.getFirstName(),
    	               record.getLastName(), record.getBalance() );[/COLOR]
    	         } // end while
    	      } // end try
    	      catch ( EOFException endOfFileException )
    	      {
    	         return; // end of file was reached
    	      } // end catch
    	      catch ( ClassNotFoundException classNotFoundException )
    	      {
    	         System.err.println( "Unable to create object." );
    	      } // end catch
    	      catch ( IOException ioException )
    	      {
    	         System.err.println( "Error during reading from file." );
    	      } // end catch
    	   } // end method readRecords
    
    	   // close file and terminate application
    	   public void closeFile()
    	   {
    	      try // close file and exit
    	      {
    	         if ( input != null )
    	            input.close();
    	         System.exit( 0 );
    	      } // end try
    	      catch ( IOException ioException )
    	      {
    	         System.err.println( "Error closing file." );
    	         System.exit( 1 );
    	      } // end catch
    	   } // end method closeFile
    	} // end class GpaRecordSerializable
    Last edited by bwilson0117; 07-20-2011 at 09:06 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    I am still seeing errors
    Please post the full text of the errors here.

  5. #5
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    import GpaRecordSerializable; Error (The import GpaRecordSerializable cannot be
    resolved)


    record.getGpa(), record.getFirstName(), Error (Multiple markers at this line
    - The method getFirstName() is undefined for the type
    GpaRecordSerializable
    - The method getGpa() is undefined for the type GpaRecordSerializable)


    record.getLastName(), record.getBalance() ); Error (Multiple markers at this line
    - The method getBalance() is undefined for the type
    GpaRecordSerializable
    - The method getLastName() is undefined for the type
    GpaRecordSerializable)



    I believe I am getting these errors because I simply need to write a GpaRecordSerializable class.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Your import statement is incorrect; all the other errors are a consequence of the import error. Is that GpaRecordSerializable a class? Does it exist in a package?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Yes, you must have a definition for a class if you are going to use it and its methods.

    Please edit your posted code and wrap it in code tags. Use the # icon above the input box.

  8. #8
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your import statement is incorrect; all the other errors are a consequence of the import error. Is that GpaRecordSerializable a class? Does it exist in a package?

    kind regards,

    Jos
    The GpaRecordSerializable is a class that I need to create, it does not exist in a package.

  9. #9
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please edit your posted code and wrap it in code tags. Use the # icon above the input box.
    I am a litte confused with what you are asking me to do here.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Go to the part of the thread with your code: post #3.
    At the bottom is "Edit Post" click on that. Click on the Go Advanced button.
    Select the source code part of your post
    Click on the # icon at the top of the input box.
    This will put a [ code] at the beginning and a [/ code] at the end. (without the space)
    Save the changes.

  11. #11
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Go to the part of the thread with your code: post #3.
    At the bottom is "Edit Post" click on that. Click on the Go Advanced button.
    Select the source code part of your post
    Click on the # icon at the top of the input box.
    This will put a [ code] at the beginning and a [/ code] at the end. (without the space)
    Save the changes.
    Ok got ya!!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Good, much easier to read.
    Now you need to get a definition for the missing class.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bwilson0117 View Post
    The GpaRecordSerializable is a class that I need to create, it does not exist in a package.
    If that class exists in the default package (i.e. no 'package' statement at the top) there is no need to import it in other classes in the default package.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    This is what I came up with but dunno if it's right

    Java Code:
    import java.io.Serializable;
    
     
    
    public class GpaRecordSerializable implements Serializable {
    
       private int account;
    
       private String firstName;
    
       private String lastName;
    
       private double balance;
    
       private double gpa;
    
        
    
       
    
       public GpaRecordSerializable()
    
       {
    
          this( 0, "", "", 0.0, 0.0);
    
       }
    
       
    
        
    
       public GpaRecordSerializable( int acct, String first,
    
          String last, double bal, double gpa )
    
       {
    
          setAccount( acct );
    
          setFirstName( first );
    
          setLastName( last );
    
          setBalance( bal );
    
          setgpa( gpa );
    
       }
    
     
    
        
    
       public void setAccount( int account )
    
       {
    
          this.account = account;
    
       }
    
     
    
          
    
       public int getAccount()
    
       {
    
          return account;
    
       }
    
        
    
         
    
       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 setBalance( double bal )
    
       {
    
          balance = bal;
    
       }
    
     
    
       
    
       public double getBalance()
    
       {
    
          return balance;
    
       }
    
       public void setgpa( double gpa )
    
       {
    
          gpa = gpa;
    
       }
    
       public double getgpa()
    
       {
    
          return gpa;
    
       }  
    
     
    
     
    
    }
    But I am still getting this error message in my GpaRecord class
    import GpaRecordSerializable; Error (The import GpaRecordSerializable cannot be
    resolved)

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Where are the two .java files? Are they in the same folder? Remove the import GpaRecordSerializable statement.
    Does the code for the GpaRecordSerializable class compile ok?

    Remov

  16. #16
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Where are the two .java files? Are they in the same folder? Remove the import GpaRecordSerializable statement.
    Does the code for the GpaRecordSerializable class compile ok?

    Remov
    Ok the code for both are compiling ok, but I am not getting any output at all it's just blank, so how am I suppose to get the output that she has stated we should be getting:

    Sample toString Output
    Jerry Springer
    Student ID: 12345678
    Class Grouping: 1 and GPA = 1.3578

    Sample Output (additional processing)
    The number of students is: 26
    The average GPA is 3.2145

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    am not getting any output at all
    Try debugging the code by adding printlns to show the execution flow and the values of variables as they are used and change. That printed output should help you see what the program is doing.

    Start in the main() method and then in each class's constructor

  18. #18
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    She also sent a StudentGPA.ser file which when I open in eclipse it looks like this

    __sr__StudentGPASerializableRJ_O___I_ classCodeD__gpaL_ firstNamet__Ljava/lang/String;L__lastNameq_~__L_ studentIDq_~__xp____@_
    =p◊
    t__Tomt__Jordant__44521365sq_~______@
    (ı\)t__Rickt__Nevint__12355487sq_~______@ _∏Q_t__Charliet__Pipert__35715964sq_~______@ _◊
    =p◊t__Carolet__Bradshawt__22587964sq_~______@_ t__Samt__Wellst__44563218sq_~______@
    333333t__Marcust__Williamst__32547951sq_~______@_ _∏Qt__Leonardt__Wolfet__11254987sq_~______@__zG _t__Nickq_~__t__00548712sq_~______?˙fffffft__Bill t__Cortellt__88745236sq_~______@
    G_zHt__Ronnt__Kernert__23652147sq_~______@
    \(ı\t__Petet__Mitchellt__33654871sq_~______@____ ___t_ Charlottet_ Blackwoodt__66542187sq_~______@_◊
    =p◊q_~__t_ Heatherlyt__99845213sq_~______@_◊
    =p◊q_~__t__Kazanskit__12345678sq_~______@_______t __Miket__Metcalft__88547126



    So am I somehow suppose to add the names and things from this file? How would I do that?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    That appears to be a serialized file that your program is supposed to read.
    Look in your posted code for the file that you are to read. It ends with .ser.

    Better ask what the .ser file is to be used for.

  20. #20
    bwilson0117's Avatar
    bwilson0117 is offline Java Beginner
    Join Date
    Jul 2011
    Location
    San Antonio, TX
    Posts
    33
    Rep Power
    0

    Default

    All of the directions that were given to us is in post #1. How do I get my file to read the .ser file?

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Serialization
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 10:47 PM
  2. serialization
    By elsa14290 in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2010, 04:03 PM
  3. Serialization
    By thayalan in forum Advanced Java
    Replies: 4
    Last Post: 08-03-2009, 10:22 PM
  4. Need help using serialization
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 08:23 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •