Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree6Likes

Thread: Binary I/O

  1. #21
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Binary I/O

    I was focusing on the wording of the assignment, where it explicitly asked to add set and get methods to the Fraction class. If the methods are not used, then why bother...

    If you're going ahead with providing a toString() method, remember to also add the @Override annotation. An Internet search using the search term "override tostring java" will give you further information on this.

    Do you think this is an unorthodox way of passing values into Fraction? The assignment said we could choose any values and Jim presented a straightforward way of accomplishing that.
    There's absolutely nothing wrong with Jim's suggestion. Note though that he wrote,
    Java Code:
    Fraction f = new Fraction( /* arguments */ );
    Note also that the Fraction class is specified in the assignment to contain an int numerator and an int denominator. Therefore this indicates that Fraction only needs 1 value for each numerator and denominator, and not an array of values for each of them. Taking all of the above into consideration, it will make more sense to me to have
    Java Code:
    Fraction fraction = new Fraction(numerator[i], denominator[i]);

  2. #22
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    I'm sorry, but I'm still not getting it. The way that I read the code in its current state -- I've created two arrays with specific integers. The try/catch block will create the file SerialF.dat so long as it does not exits, otherwise a catch error will throw. Now the for loop I have created will instantiate a faction object consisting of two parameters, a numerator and denominator, which are then set() in the Fraction class. At this point, I am just kind of winging it based off of previous knowledge and what I have found elsewhere.

    Java Code:
    public static void main(String[] args) throws ClassNotFoundException {
    		int[] numeratorArray = { 2, 3, 4 };
    		int[] denominatorArray = { 8, 6, 4 };
                    int numerator, denominator;
    
    		//Setting up the output stream  and creating new output file
    		try {
    			ObjectOutputStream output = new ObjectOutputStream(
    					new FileOutputStream("SerialF.dat"));
    		//Loop to instantiate three objects of the Fraction class
    			for (int i = 0; i < denominatorArray.length; i++) {
    				Fraction fraction = new Fraction(numeratorArray[i], denominatorArray[i]);
    				fraction.setNumerator(numerator);
    				fraction.setDenominator(denominator);
    				output.writeObject(fraction);
    			}
    			output.close();
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}

    Java Code:
    public class Fraction implements java.io.Serializable{
    	private int  numerator, denominator;
    	private static char slash = '/';
    
    	public Fraction(int numeratorArray, int denominatorArray) {
    		this.numerator = numeratorArray;
    		this.denominator = denominatorArray;
    	}
    	
    	int getNumerator(){
    		return  numerator;
    	}
    	int getDenominator(){
    		return denominator;
    	}
    	void setNumerator(int numerator){
    		this.numerator = numerator;
    	}
    	void setDenominator(int denominator){
    		this.denominator = denominator;
    	}
    	
    	public String toString(){
    		return ("Fraction:" + this.numerator + slash + this.denominator);	
    	}
    }

  3. #23
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    I am starting to wonder if I shouldn't change things around a bit...something like what follows:

    Java Code:
    public static void main(String[] args) throws ClassNotFoundException {
    		int[] numeratorArray = { 2, 3, 4 };
    		int[] denominatorArray = { 8, 6, 4 };
    		int numerator, denominator;
    		
    		Fraction [] fraction = new Fraction[3];
    		
    		//Setting up the output stream  and creating new output file
    		try {
    			ObjectOutputStream output = new ObjectOutputStream(
    					new FileOutputStream("SerialF.dat"));
    		//Loop to instantiate three objects of the Fraction class
    			for (int i = 0; i < denominatorArray.length; i++) {
    				fraction = new Fraction(numeratorArray[i], denominatorArray[i]);
    				fraction.setNumerator(numerator);
    				fraction.setDenominator(denominator);
    				output.writeObject(fraction);
    			}
    	output.close();
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		} catch (IOException e1) {
    			e1.printStackTrace();
    		}
    try {
    			ObjectInputStream input = new ObjectInputStream(
    					new FileInputStream("SerialF.dat"));
    			for (int i = 0; i < denominatorArray.length; i++) {
    				Fraction fraction = (Fraction) (input.readObject());
    				System.out.println(fraction.getNumerator());
    				System.out.println(fraction.getDenominator());
    			}
    			input.close();
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    Except with the changes, I get an error: "Type mismatch: Cannot convert from Fraction to Fraction[]"
    Last edited by javaStooge; 04-29-2014 at 09:39 PM.

  4. #24
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Binary I/O

    Hi,

    first of all, you create fraction as an array. So you have to set the single elements of course.

    And I do not understand your code when you create a new fraction:
    You already set numerator / denominator when you call the constructor.
    Why do you call the setters again (and pass 2 variables that are not initialized in your code)?
    Just keep the Constructor and remove the call of the two setters.

    Konrad

  5. #25
    javaStooge is offline Senior Member
    Join Date
    Jan 2014
    Posts
    104
    Rep Power
    0

    Default Re: Binary I/O

    I'm not sure what you mean by setting the single elements...I'm still a noob, so if you could please be a little more specific.

    I get what you are saying about the new fraction, I was just trying to do that, and initialize the variables using the setters...since the professor asked that we use "proper get and set methods in Fraction class". Should I not pass them through the constructor and just use the setters?

    (I get an idea and just kinda run with, that's why that second code with the new fraction probably looks so confusing) :)

    Thanks!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2013, 11:25 PM
  2. Replies: 2
    Last Post: 02-22-2013, 07:10 PM
  3. Replies: 1
    Last Post: 11-01-2011, 06:35 PM
  4. Replies: 5
    Last Post: 08-25-2011, 08:32 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
  •