Results 21 to 25 of 25
Thread: Binary I/O
 04262014, 03:25 AM #21Senior Member
 Join Date
 Feb 2014
 Posts
 219
 Rep Power
 4
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.
Java Code:Fraction f = new Fraction( /* arguments */ );
Java Code:Fraction fraction = new Fraction(numerator[i], denominator[i]);
 04292014, 08:32 PM #22Senior Member
 Join Date
 Jan 2014
 Posts
 106
 Rep Power
 0
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); } }
 04292014, 08:35 PM #23Senior Member
 Join Date
 Jan 2014
 Posts
 106
 Rep Power
 0
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(); }
Last edited by javaStooge; 04292014 at 08:39 PM.
 04292014, 09:07 PM #24Senior Member
 Join Date
 Feb 2014
 Posts
 447
 Rep Power
 4
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
 04292014, 09:22 PM #25Senior Member
 Join Date
 Jan 2014
 Posts
 106
 Rep Power
 0
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!
Similar Threads

Program Using Arrays To Conver Binary to Decimal and Decimal to Binary.
By CreatingDrake in forum New To JavaReplies: 1Last Post: 11192013, 11:25 PM 
Decimal to Binary conversion & Character to Binary conversion
By seena in forum New To JavaReplies: 2Last Post: 02222013, 07:10 PM 
Convert integer to binary then each binary character to boolean
By davidfla90 in forum New To JavaReplies: 1Last Post: 11012011, 06:35 PM 
anyone know's how to program a conversion of binarydecimal , decimalbinary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08252011, 07:32 PM
Bookmarks