0

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:

  • Create four fraction objects (1/4, 2/4, 3/4, 4/1) and store them in the collection.
  • Retrieves the first fraction from the list and store in a local variable called fraction1. Retrieves the second fraction from list and store in a local variable called fraction2.
  • Also have to call the equal method of fraction1 passing fraction2 as an argument.
  • Multiplies fraction1 times fraction2

  • Inverts fraction1

  • Uses toString to print out fraction1.

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;
}

 }
1
  • In addition to the answers below it may be helpful to use the Java API as a reference when trying to leverage something like an ArrayList. In general it's a great reference for anyone programming in Java and has the answers to your question and more: docs.oracle.com/javase/7/docs/api Commented Oct 21, 2016 at 18:06

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.