J Cole
J Cole

Reputation: 15

Java Storing objects

How do I add fractions into the arraylist and then retrieve and store them in a local variable?

Question: Create a second class that can store a number of Fraction class objects using an ArrayList of Fractions. This class should have a method called demo that does the following in order:

I'm confused and primary need help on the first two problems. I want to try it out on my own but I just need a little push on what or how to do it.

So far I have-

Second Class:

 import java.util.ArrayList;
    public class MyFractions 
    {
    private ArrayList<Fraction> fractions;



     public MyFractions()
   {
   ArrayList<Fraction> fractions = new ArrayList<Fraction>();

   }
    public void demo()
    {
      fractions.add();
    }
  }

Fraction Class:

 public class Fraction
       {
           private long numerator;
          private long denominator;

public Fraction(long num, long den)   
{
   numerator=num;
   denominator=den;
}

public Fraction(long num) 
{
   numerator=num;
   denominator=1;
}


public long denominator() 
{
    return denominator;  
}

public void dividedBy(Fraction otherFraction)
{
    numerator=numerator*otherFraction.denominator;
    denominator=denominator*otherFraction.numerator;
}


public boolean  equals(long n) 
{
    return  numerator==n;
}

public boolean  equals(Fraction otherFraction) 
{
    return numerator==otherFraction.numerator && denominator==otherFraction.denominator;
}

public void negative() 
{
    numerator= -numerator;
}


public long    numerator() 
{
    return numerator;
}


public void inverse() 
{
    long temp=numerator;
    numerator=denominator;
    denominator=temp;
}

public boolean  isProper()
{
    return numerator<denominator;  
}

public void times(Fraction other) 
{
    numerator=numerator*other.numerator;
    denominator=denominator*other.denominator;
}


public double   toDouble() 
{
    return 1.0*numerator/denominator;
}

public String toString() 
{
    return numerator + "/" + denominator;
}


public boolean  isWholeNumber()
{
    return denominator==1;
}

 }

Upvotes: 0

Views: 247

Answers (2)

RAZ_Muh_Taz
RAZ_Muh_Taz

Reputation: 4099

If you want to be able to add a Fraction object to your arraylist of fractions you need to give your add method a parameter that will be the Fraction object you are trying to add.

public void demo(Fraction obj)
{
  fractions.add(obj);
}

Now you can add a Fraction object to your arraylist of Fractions by doing the following

MyFractions mFractions = new MyFractions();
mFractions.demo(new Fraction(1, 2)); // adds the fraction 1/2 to the arraylist

Here is a method you can use to retrieve fractions from your collection of fractions (This method is for the MyFractions class)

public Fraction getFraction(int index)
{
    return fractions.get(index);
}

Upvotes: 1

jdjohannsen
jdjohannsen

Reputation: 1

To add a fraction, try the following: fractions.add(new Fraction(1, 4));

To retrieve a faction: Fraction fraction1 = fractions.get(0), to retrieve the first fraction

Upvotes: 0

Related Questions