# Thread: Creating an Array of Objects Method

1. Member
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; 07-16-2011 at 06:58 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.

3. 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,

Jos

4. Member
Join Date
Jan 2011
Posts
24
Rep Power
0
Originally Posted by Norm
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.
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.

5. Member
Join Date
Jan 2011
Posts
24
Rep Power
0
Originally Posted by JosAH
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,

Jos
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.

6. What I wanna do is to take the instances that I made f1 f2 f3 f4 etc. and put them into an array.
That sounds like a very normal thing to do. What is your problem? Do you know the size of the 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 index
Last edited by Norm; 07-16-2011 at 08:03 PM.

7. Member
Join Date
Jan 2011
Posts
24
Rep Power
0
Originally Posted by Norm
That sounds like a very normal thing to do. What is your problem? Do you know the size of the 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 index
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.

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.

9. Originally Posted by DBaskov
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.
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) };
}```
This little method creates and fills an array of Fraction objects and returns it (all in one single statement).

kind regards,

ps. I still think that your Fraction class code sucks; no offense.

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 */}`
Your add, mult, etc methods should all return new Fractions since the class should be immutable and can't be changed once it is constructed. So the add method, mult, etc methods should simply compute the numerator and denom then pass them to a new object which is then returned
Java Code:
```public Fraction add(Fraction other){
int numerator = ...;
int denominator = ...;
return new Fraction(numerator, denominator);
}```

11. Member
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?!

12. I want to treat those instances like I did not know them
You have to "know" a variable to be able to use it. This makes no sense to me.
Can you explain what you mean?
What are the program requirements for this task?

13. Member
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?

14. Did you read (and understand) my reply #9?

kind regards,

Jos

15. 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.
Could you store them in a list that other methods can see and use?

in the main method.
You should shorten the main() method to only create an instance of the class and put the logic in the constructor or other methods of the class vs in the main() method.

#### Posting Permissions

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