Results 1 to 15 of 15
 07162011, 06:54 PM #1Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
Creating an Array of Objects Method
Hello, I'm creating a program called Fractions where you create a fraction and get to do operations like subtracting, multiplying, adding them etc. However, I need to make a method that takes the Fraction objects that are already instantiated and put them into an array and then returns the array. Since I'm not very experienced with arrays I'm kind of stuck and need help please. In case anyone is wondering what my code for the Fraction program is so far(please ignore the red font/comment code):
PHP Code:package fractions; import java.lang.Math; /** * * @author Dennis */ public class Fraction { private int numerator; private int denominator; public final static int ARRAY_SIZE = 5; // private int gdc; public Fraction (int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; // gdc = 0; setFraction(numerator , denominator); // if(numerator % 2 == 0 && denominator % 2 == 0) { // greatestcommonDiviser(numerator , denominator); // } // else if(numerator % 3 == 0 && denominator % 3 == 0) { // greatestcommonDiviser(numerator, denominator); // } } public Fraction() { numerator = 0; denominator = 0; // gdc = 0; } public void setFraction(int numerator, int denominator) { //boolean switch = false; this.numerator = numerator; this.denominator = denominator; if(denominator == 0) { System.out.println("The fraction is undefined"); System.exit(0); } else if(denominator < 0) { this.denominator = Math.abs(denominator); this.numerator = numerator * 1; } else if(numerator == denominator) { this.numerator = numerator / denominator; this.denominator = denominator / numerator; } simplify(numerator, denominator); } private int greatestcommonDiviser(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; while(numerator != denominator) { if(numerator > denominator) { numerator = numerator  denominator; } else { denominator = denominator  numerator; } } return numerator; } public Fraction simplify(int numerator, int denominator) { Fraction other = new Fraction(); this.numerator = numerator; System.out.println(this.numerator + " Num"); this.denominator = denominator; System.out.println(this.denominator + " Denum"); // other.numerator = this.numerator; // other.denominator = this.denominator; if(denominator < 0) { this.denominator = Math.abs(denominator); this.numerator = numerator * 1; } while(this.numerator % 2 == 0 && this.denominator % 2 == 0) { System.out.println("tracer" + this.numerator); this.numerator = this.numerator / 2; // System.out.println("other" + other.numerator); this.denominator = this.denominator / 2; } while(this.numerator % 3 == 0 && this.denominator % 3 == 0) { // System.out.println("tracer"); this.numerator = this.numerator / 3; this.denominator = this.denominator / 3; } while(this.numerator % 5 == 0 && this.denominator % 5 == 0) { // System.out.println("tracer"); this.numerator = this.numerator / 5; this.denominator = this.denominator / 5; } other.numerator = this.numerator; other.denominator = this.denominator; return other; } public void reciprocal() { int rdenominator; int rnumerator; rdenominator = this.numerator; rnumerator = this.denominator; this.numerator = rnumerator; this.denominator = rdenominator; } public Fraction getFraction(int numerator, int denominator) { Fraction answer = new Fraction(); this.numerator = numerator; this.denominator = denominator; answer.numerator = this.numerator; answer.denominator = this.denominator; return answer; } public int getNumerator(int numerator) { return this.numerator; } public int getDenominator(int denominator) { return this.denominator; } public Fraction multiply(Fraction other) { Fraction answer = new Fraction(); answer.numerator = this.numerator * other.numerator; answer.denominator = this.denominator * other.denominator; numerator = answer.numerator; denominator = answer.denominator; simplify(numerator, denominator); answer.numerator = numerator; answer.denominator = denominator; return answer; } public Fraction divide(Fraction other) { Fraction answer = new Fraction(); answer.numerator = this.numerator * other.denominator; answer.denominator = this.denominator * other.numerator; numerator = answer.numerator; denominator = answer.denominator; simplify(numerator, denominator); answer.numerator = numerator; answer.denominator = denominator; return answer; } public Fraction add(Fraction other) { Fraction answer = new Fraction(); int anothernum; if(this.denominator != other.denominator) { answer.denominator = this.denominator * other.denominator; anothernum = this.numerator * other.denominator; //9 other.numerator = other.numerator * this.denominator; //5 answer.numerator = anothernum + other.numerator; } else { answer.numerator = this.numerator  other.numerator; answer.denominator = this.denominator; } numerator = answer.numerator; denominator = answer.denominator; simplify(numerator, denominator); answer.numerator = numerator; answer.denominator = denominator; return answer; } public Fraction subtract(Fraction other) { Fraction answer = new Fraction(); int anothernum; if(this.denominator != other.denominator) { answer.denominator = this.denominator * other.denominator; anothernum = this.numerator * other.denominator; other.numerator = other.numerator * this.denominator; answer.numerator = anothernum  other.numerator; } numerator = answer.numerator; denominator = answer.denominator; simplify(numerator, denominator); answer.numerator = numerator; answer.denominator = denominator; return answer; } public boolean equals(Fraction other) { return (this.numerator == other.numerator && this.denominator == other.denominator); } public static Fraction[] readFractionArray(Fraction[] array) { Fraction[] fractionArray = new Fraction[ARRAY_SIZE]; for(int i=0; i<5; i++) { fractionArray[i] = new Fraction(); } } public static void main(String[] args) { Fraction[] fractionArray = new Fraction[ARRAY_SIZE]; Fraction f1 = new Fraction(2, 5); Fraction f2 = new Fraction(2, 4); Fraction f3 = new Fraction(10, 20); Fraction f4 = new Fraction(1 , 3); Fraction f5 = new Fraction(); f5 = f2.add(f4); System.out.println("Added Fraction numerator is: " + f5.numerator); System.out.println("Added Fraction denominator is: " + f5.denominator); System.out.println("The numerator is: " + f3.numerator); System.out.println("The denumerator is: " + f3.denominator); // System.out.println(f4.numerator + " :simplified numerator "); // System.out.println(f4.denominator + " :simplified denominator"); // f3.greatestcommonDiviser(10, 20); // System.out.println(f3.gdc); // System.out.println(f2.numerator); f1 = f1.multiply(f2); System.out.println(f1.numerator); System.out.println(f1.denominator); f2.reciprocal(); System.out.println(f2.numerator); System.out.println(f2.denominator); } }
Last edited by DBaskov; 07162011 at 06:58 PM.
 07162011, 07:21 PM #2
Can you explain what your problem is?
Is the problem in the code you posted or are you waiting to write that code later?
I don't see any comments in your code. It is hard to know if the code does what you want it to do. A comment could be: This method does this and that and returns the other.
Some one could then look at the comment and then at the code and see that the code did NOT do what the comments said it was supposed to do.
 07162011, 07:41 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,231
 Blog Entries
 7
 Rep Power
 24
I'm sorry for saying so but that code sucks; make a Fraction object immutable for starters and next properly implement a gcd(m, n) method that can be used for the construction of a Fraction. If you do it properly you need half a page maximum.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07162011, 07:42 PM #4Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
I'm trying to come up with the code. I made a method in attempt to create what I wanted but it's unfinished because I'm stuck. What I wanna do is to take the instances that I made f1 f2 f3 f4 etc. and put them into an array. The reason I show the code is in case you want to see how my class look like; constructor, instances, etc. Most of the comments I have are not really comments they are methods/statements that I decided to toggle off, I just want you to save the confusion and ignore them.
 07162011, 07:47 PM #5Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
Well it does almost everything I want it to do. I made the method gdc just in case, I wasn't really attempting to use it when I made method called simplify. There might be other code here and there that is unnecessary but my prime focus is to get that creating array out of instances method going.
 07162011, 08:00 PM #6What I wanna do is to take the instances that I made f1 f2 f3 f4 etc. and put them into an array.
Create the array and add the instances.
<Data type>[] arrayName = new <Data type>[<Size of array>]; // create an array of with <Size of array> slots.
arrayName[index] = new <Data type>(); // put an instance of the class into the array at indexLast edited by Norm; 07162011 at 08:03 PM.
 07162011, 08:11 PM #7Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
I guess my problem is doing it in a method. I can't figure out the algorithm of bringing in an instance of an object and make sure it assigns a different one for each index in a method. It's easy if I do it in the main method, seems more complicated if you write it in a separate method.
Btw I have to go to work in couple minutes so I may have to read and reply any more posts later.
 07162011, 08:16 PM #8
Define exactly what the method is supposed to do.
What is passed to the method. What class variables, if any, it will use and what it will return.
 07172011, 08:36 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,231
 Blog Entries
 7
 Rep Power
 24
If your question is still about creating and filling an array and doing it in a method, have a look at at this:
Java Code:public Fraction[] createArray() { return new Fraction[] { new Fraction(1, 3), new Fraction(2, 4), new Fraction(1, 5), new Fraction(2, 6), new Fraction(4, 7) }; }
kind regards,
ps. I still think that your Fraction class code sucks; no offense.The only person who got everything done by Friday was Robinson Crusoe.
 07172011, 09:47 AM #10
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 10
As Jos said, your code can be made much better. One bug I notice right away is that your getFraction method modifies the object it is called on, creates an extra unnecessary option and then returns it.
You should consider(as Jos said), making this class immutable and have the constructor do simplifying of fractions for you. Then implement a simple gcd(m, n) which uses euclids algorithm. Also, your current gcd also modifies the fraction object which it shouldn't.
Euclids algorithm can be stated like this:
1) find the remainder of m and n(m%n) and set it to r
2) if r is 0 return n
3) otherwise set m to n and n to r and go to step 1
Also, your equals implementation is wrong. You should use the override annotation when overriding methods.
Equals should always look like this
Java Code:@Override public boolean equals(Object o){/* body */}
Java Code:public Fraction add(Fraction other){ int numerator = ...; int denominator = ...; return new Fraction(numerator, denominator); }
 07182011, 04:23 AM #11Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
@ Norm I think I was supposed to make a method that takes an empty array and fills it with already created instances, kind of like what Jos showed except I want to treat those instances like I did not know them, where as Jos made an array that mimicked created instance and created objects in the array that have the same values, unless you can't do it any other way.
@Josah and sunde887
I know my coding is mediocre which is why I post in the noob section. I learn as go, to be honest it wasn't until recently that I started creating real projects by myself. A lot of things I do, is by trial and error which is why turn some codes into comments. I learn as I go.
Also my IDE, Netbeans does not all me to override the equals method. And I thought my operation methods did return new objects, are you talking about me returning a new object with numerator and denominator parameters { return new Fraction(numerator, denominator) } instead of attaching a variable to an object { Fraction.numerator, Fraction.denominator return Fraction }
Why is it important to implement that way again?!
 07182011, 04:26 AM #12I want to treat those instances like I did not know them
Can you explain what you mean?
What are the program requirements for this task?
 07182011, 04:46 AM #13Member
 Join Date
 Jan 2011
 Posts
 24
 Rep Power
 0
Well the method supposed to be called readFractionArray and I believe what it does is takes an empty array, and then inputs the created instances of the class into it and then returns it for the getAverage method were it takes the array, takes out all of the fraction values, adds them up and then computes the average, that method I already completed. The problem I see with making that method is that as far as I know you cannot access the instances through it because they were created exclusively in the main method. So I guess there is no choice but to make new fraction objects in that method that have the same value as the instances of the main method?
 07182011, 07:14 AM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,231
 Blog Entries
 7
 Rep Power
 24
Did you read (and understand) my reply #9?
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 07182011, 01:33 PM #15The problem I see with making that method is that as far as I know you cannot access the instances through it because they were created exclusively in the main method.
in the main method.
Similar Threads

Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08092011, 12:40 PM 
Creating array in method parameter
By Dipke in forum New To JavaReplies: 2Last Post: 02252011, 10:18 AM 
Creating Array of Objects
By chathurajeewaka in forum New To JavaReplies: 4Last Post: 12032009, 04:23 PM 
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11162009, 02:25 AM 
Creating an array of nonprimitive objects
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:46 PM
Bookmarks