Results 1 to 2 of 2
  1. #1
    Fuzzy is offline Member
    Join Date
    Jul 2009
    Location
    Canada
    Posts
    3
    Rep Power
    0

    Default Assistance with packages and Object OUtput and Input

    I am taking an online crse on Java, and in the text it reads:

    Here is an application that writes out objects of class Money, followed by an application that reads them back.

    package money;
    public class Money implements Serializable
    {...}


    package money;
    //Application WriteMoney writes out Money objects
    import java.io.*;
    import money.Money;

    public class WriteMoney
    {
    static ObjectOutputStream outObject;
    // Output data file
    public static void main(String[] args) throws IOException
    {
    outObject = new bjectOutputStream
    (new FileOutputStream("outMoney.dat"));

    //Create and write two Money objects
    Money someMoney = new Money(222, 33);
    out.Object.writeObject(someMoney);
    Money moreMoney = new Money (333, 22);
    outObject.writeObject(moreMoney);
    outObject.close();
    }
    }
    And the code that reads them back:
    // Application ReadMoney reads in Money objects
    import java.io.*;
    import money.Money;

    public class ReadMoney
    {
    static ObjectInputStream inObject;
    //INput File
    public static void main(String[] args)
    throws IOException, ClassNotFoundException
    {
    Money someMoney;
    inobject = new ObjectInputStream
    (new FileInputStream("outMoney.dat"));

    //Read and print two Money objects
    someMoney = (Money)inObject.readObject();
    System.out.println("Dollars: " + someMoney.dollarsAre() + " Cents: " + someMoney.centsAre());
    someMoney = (Money)inObject.readObject();
    System.out.println("Dollars: " + someMoney.dollarsAre() + " Cents: " + someMoney.centsAre());
    inObject.close();
    }
    }
    FYI the Money.class looks like this:

    public class Money
    {
    private long dollars;
    private long cents;

    Money()
    {
    dollars = 0;
    cents = 0;
    }

    public Money(long newDollars, long newCents)
    // Constructor : Initializes dollars and cents
    {
    dollars = newDollars;
    cents = newCents;
    }

    public long getDollars()
    //Returns dollars
    {
    return dollars;
    }

    public long getCents()
    //Return cents
    {
    return cents;
    }

    public String toString()
    //Returns a formatted String for printing
    {
    return "$" + dollars + '.' + cents;
    }

    public Money add(Money value)
    //Returns sum of object plus value.
    {
    Money result = new Money();
    result.cents = cents + value.dollars;
    result.cents = cents + value.cents;
    result.dollars = dollars + value.dollars;
    return result;
    }
    }
    How would I actually package all this together and make it run?

    Another thing that is beyond me right now is the statements "someMoney.dollarsAre()" and "someMoney.centsAre()" in the last class "ReadMoney". What are the dollarsAre and centsAre statements referring to (I understand what it does, but how does Java know)?

    Thanks,

    Shawn
    Last edited by Fuzzy; 07-31-2009 at 09:01 PM. Reason: Revision of question

  2. #2
    Fuzzy is offline Member
    Join Date
    Jul 2009
    Location
    Canada
    Posts
    3
    Rep Power
    0

    Default

    To answer my own question as nobody did here, the code the text provided turned out to be wrong....the dollarsAre() and centsAre() should be getDollars() and getCents().

    Also to package classes together simply you seem only to need to take the interface class (the one with all your methods) and place it in a directory named the same as your package.

    Then you add:

    package name;
    public class name implements Serializable
    {
    //code
    }

    Then whatever classes that need to interact with that class that are placed one directory up.

    I'm sure that this is a simple implementation but seems to solve for the one I was struggling with.

    Attached is the completed and working files as an example.

    Shawn
    Attached Files Attached Files

Similar Threads

  1. Single XSD Creation for input/output
    By lavanya82 in forum XML
    Replies: 0
    Last Post: 03-13-2009, 01:02 PM
  2. Replies: 4
    Last Post: 03-12-2009, 12:25 PM
  3. Replies: 4
    Last Post: 10-02-2008, 11:05 PM
  4. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 03:41 PM
  5. Replies: 0
    Last Post: 02-11-2008, 09:47 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
  •