Need to create an array out of this code

Must input fractions into an array and perform math

Page 1 of 1

9 Replies - 1638 Views - Last Post: 27 April 2008 - 01:50 PM Rate Topic: -----

#1 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Need to create an array out of this code

Posted 26 April 2008 - 02:06 PM

I am having problems understanding how to write the code for the array needed for my program. Here is what I am suppose to do:

This time we will create an array of 5 fractions that the user can operate on. When prompted for entering a fraction, be sure to ask which index. When performing math on a fraction, be sure to ask for all source indexes and a destination index (yes, we will write our answer back to the fraction array)

1) set a fraction (1-5)
2) add two fractions C = A + B
3) subtract
4) multiply
5) divide
6) compare A == B
7) print out all fractions and their array indices
8) quit


Here is my existing code that I have created and need to use for the new program. I am so lost on arrays, please help.

//Assignment 6
//Written By: Matt Clements



import javax.swing.JOptionPane;
class Fraction
	{
	int iNumerator;
	int iDenominator;

	public Fraction()
   		{
		  iNumerator = iDenominator = 0;
   		}
   	
   	public int getNumerator()
   		{
		  return iNumerator;
   		}

	public void setNumerator(int iNum)
   		{
		  iNumerator=iNum;
   		}

   	public int getDenominator()
   		{
		  return iDenominator;
   		}

   	public void setDenominator(int iDen)
   		{
		  iDenominator=iDen;
   		}
	
	public void input()
   		{
   		String sNumerator;
   		String sDenominator;
		  sNumerator = JOptionPane.showInputDialog(null, "Enter a positive integer for numerator.");
		  iNumerator = Integer.parseInt(sNumerator);
		  do 
			  {
		 	sDenominator = JOptionPane.showInputDialog(null, "Enter a positive integer for denominator.");
		 	iDenominator = Integer.parseInt(sDenominator);
		 	if (iDenominator <= 0)
			System.out.println("Can not compute, enter a new value.");
			  } 
		  while (iDenominator <= 0);
   		}

   	public void output()
   		{
	   	System.out.print(this);
   		}
	
	public String toString()
   		{
		  String sReadout = iNumerator + "/" + iDenominator;
		  return sReadout;
		  }
   
   	
   	public boolean equals(Object Thor)
		  {  
		if ( this == Thor ) return true;  
		if ( !(Thor instanceof Fraction) ) return false;  
  
		Fraction other = (Fraction)Thor;  
		return  ( this.getNumerator() / this.getDenominator() == other.getNumerator() / other.getDenominator());  
 		}  
   	
   	
   	public Fraction add(Fraction b)
   		{
		  int iCom = lcd(iDenominator, b.iDenominator);
		  Fraction fComA = new Fraction();
		  Fraction fComB = new Fraction();
		  fComA = convert(iCom);
		  fComB = b.convert(iCom);
		  Fraction fSum = new Fraction();
		  fSum.iNumerator = fComA.iNumerator + fComB.iNumerator;
		  fSum.iDenominator = iCom;
		  fSum = fSum.reduce();
		  return fSum;
   		}

   	public Fraction subtract(Fraction b)
   		{
		  int iCom = lcd(iDenominator, b.iDenominator);
		  Fraction fComA = new Fraction();
		  Fraction fComB = new Fraction();
		  fComA = convert(iCom);
		  fComB = b.convert(iCom);
		  Fraction fDiff = new Fraction();
		  fDiff.iNumerator = fComA.iNumerator - fComB.iNumerator;
		  fDiff.iDenominator = iCom;
		  fDiff = fDiff.reduce();
		  return fDiff;
   		}

   	public Fraction multiply(Fraction b)
   		{
		  Fraction fMult = new Fraction();
		  fMult.iNumerator = iNumerator * b.iNumerator;
		  fMult.iDenominator = iDenominator * b.iDenominator;
		  fMult = fMult.reduce();
		  return fMult;
   		}

   	public Fraction divide(Fraction b)
   		{
		  Fraction fDiv = new Fraction();
		  fDiv.iNumerator = iNumerator * b.iDenominator;
		  fDiv.iDenominator = iDenominator * b.iNumerator;
		  fDiv = fDiv.reduce();
		  return fDiv;
   		}



   	private int lcd(int iDenom1, int iDenom2)
   		{
		  int iFinalDenom = iDenom1;
		  while ((iDenom1 % iDenom2) != 0)
		iDenom1 += iFinalDenom;
		  return iDenom1;
   		}

   
   	private int gcd(int iDenom1, int iDenom2)
   		{
		  int iFinalDenom = iDenom2;
		  while (iDenom2 != 0) 
			  {
			iFinalDenom = iDenom2;
			iDenom2 = iDenom1 % iDenom2;
			iDenom1 = iFinalDenom;
			  }
		  return iDenom1;
   		}

   	private Fraction convert(int iCom)
   		{
		  Fraction fFinal = new Fraction();
		  int iFinalDenom = iCom / iDenominator;
		  fFinal.iNumerator = iNumerator * iFinalDenom;
		  fFinal.iDenominator = iCom;
		  return fFinal;
   		}

   	private Fraction reduce()
   		{
		  Fraction fFinal = new Fraction();
		  int iCom = 0;
		  int iNum = Math.abs(iNumerator);
		  int iDen = Math.abs(iDenominator);
		  if (iNum > iDen)
		iCom = gcd(iNum, iDen);
		  else if (iNum < iDen)
		iCom = gcd(iDen, iNum);
		  else
		iCom = iNum;
		  fFinal.iNumerator = iNum / iCom;
		  fFinal.iDenominator = iDenominator / iCom;
		  return fFinal;
   		}
   	
	}
	
	
public class assign6
	{
	public static void main(String[] agrs)
		{
		Fraction fFrac1 = new Fraction();
		  Fraction fFrac2 = new Fraction();
		  Fraction fFrac3 = new Fraction();
		  Fraction fFrac4 = new Fraction();
		  Fraction fFrac5 = new Fraction();
		  Fraction fAnswer = new Fraction();	
		  
			  {
			  fFrac1.input();
	 		fFrac2.input();
	 		fFrac3.input();
	 		fFrac4.input();
	 		fFrac5.input();
		  
			  System.out.println("Fraction 1 = " + fFrac1);
			  System.out.println("Fraction 2 = " + fFrac2);
			  System.out.println("Fraction 3 = " + fFrac3);
			  System.out.println("Fraction 4 = " + fFrac4);
			  System.out.println("Fraction 5 = " + fFrac5);
 
		 
 				{
				  fAnswer = fFrac1.add(fFrac2);
				  fFrac1.output();
				  System.out.print(" + ");
				  fFrac2.output();
 		 		System.out.print(" = ");
				  fAnswer.output();
				  System.out.println();
		  
	 	 		fAnswer = fFrac1.subtract(fFrac2);
   	   			fFrac1.output();
				  System.out.print(" - ");
				  fFrac2.output();
				  System.out.print(" = ");
				  fAnswer.output();
				  System.out.println();
		  
				  fAnswer = fFrac1.multiply(fFrac2);
				  fFrac1.output();
				  System.out.print(" * ");
				  fFrac2.output();
				  System.out.print(" = ");
				  fAnswer.output();
				  System.out.println();
		  
				  fAnswer = fFrac1.divide(fFrac2);
				  fFrac1.output();
				  System.out.print(" / ");
				  fFrac2.output();
				  System.out.print(" = ");
				  fAnswer.output();
				  System.out.println();
				  
				  System.out.println("Fraction 1 = Fraction 2 is " + fFrac1.equals(fFrac2));
					  
 				}
			  }
		  System.exit(0);
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Need to create an array out of this code

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Need to create an array out of this code

Posted 26 April 2008 - 04:39 PM

Well when you say you need an array of fractions, you can code it just like you say it...

// Create an array called myFractions made up of 5 new Fraction objects.
Fraction myFractions[] = new Fraction[5];



Now when you do some math on them, you can store them right in the array.

// Setup our two fractions
Fraction fraction1 = new Fraction();
Fraction fraction2 = new Fraction();

// Set up the fractions
fraction1.input();
fraction2.input();

// Lets multiply the fractions and store the result in our first array slot
myFractions[0] = fraction1.multiply(fraction2);

// Lets pull it out and print it to screen
System.out.println(myFractions[0].toString());



Hopefully you get the idea here. We created an array that holds Fraction objects. We declared it to by of size 5 and we can put Fraction objects in each spot of the array.

In our example we setup to fractions, multiply them together and the result of the multiplication is another Fraction object which we stored in the first slot of the array.

If we wanted to we could even loop through the array and print each fraction object.

Hope this helps! Enjoy!

"At DIC we be Fraction handling code ninjas... of course Skyhawk is only 1/2 the man I am. Lets just keep that one a secret." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Re: Need to create an array out of this code

Posted 26 April 2008 - 05:29 PM

If I am going to create 5 new fractions, wouldn't I set it as
Fraction myFractions[] = new Fraction[4];
because Java starts its arrays with 0?
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 284
  • View blog
  • Posts: 1,502
  • Joined: 07-April 08

Re: Need to create an array out of this code

Posted 26 April 2008 - 06:40 PM

View Postmclem864, on 26 Apr, 2008 - 05:29 PM, said:

If I am going to create 5 new fractions, wouldn't I set it as
Fraction myFractions[] = new Fraction[4];
because Java starts its arrays with 0?



actually no, the Fraction myFractions[] = new Fractions[5] is specifying the size of the array I.E 0-4

if you said new Fraction[4] it would use indexes 0 - 3 :)
Was This Post Helpful? 0
  • +
  • -

#5 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Re: Need to create an array out of this code

Posted 27 April 2008 - 08:07 AM

One other question. I also need to make sure that the user input fractions are created in the array. So at the end of the program all of the user created fractions and the new fractions created by the math are all in the array. I understand how to set the math fraction into the array now....thanks to you, but not the input fractions.
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Need to create an array out of this code

Posted 27 April 2008 - 08:16 AM

Once you create the array, you have an array full of empty Fraction elements so you should be able to do this...

Fraction myFractions[] = new Fraction[5];

// Call input for the first Fraction.
myFractions[0].input();



After the user is prompted for the info, you will have set the first of the Fractions and then you can pull it out and do other functionality.

:)
Was This Post Helpful? 0
  • +
  • -

#7 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Re: Need to create an array out of this code

Posted 27 April 2008 - 08:25 AM

I get it now, thank you for the help!
:^:
Was This Post Helpful? 0
  • +
  • -

#8 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Re: Need to create an array out of this code

Posted 27 April 2008 - 08:51 AM

After writing the new code it will compile but will not run, it gives the following error code:

Exception in thread "main" java.lang.NullPointerException
at assign6.main(assign6.java:185)

//Assignment 6
//Written By: Matt Clements



import javax.swing.JOptionPane;
class Fraction
	{
	int iNumerator;
	int iDenominator;

	public Fraction()
   		{
		  iNumerator = iDenominator = 0;
   		}
   	
   	public int getNumerator()
   		{
		  return iNumerator;
   		}

	public void setNumerator(int iNum)
   		{
		  iNumerator=iNum;
   		}

   	public int getDenominator()
   		{
		  return iDenominator;
   		}

   	public void setDenominator(int iDen)
   		{
		  iDenominator=iDen;
   		}
	
	public void input()
   		{
   		String sNumerator;
   		String sDenominator;
		  sNumerator = JOptionPane.showInputDialog(null, "Enter a positive integer for numerator.");
		  iNumerator = Integer.parseInt(sNumerator);
		  do 
			  {
		 	sDenominator = JOptionPane.showInputDialog(null, "Enter a positive integer for denominator.");
		 	iDenominator = Integer.parseInt(sDenominator);
		 	if (iDenominator <= 0)
			System.out.println("Can not compute, enter a new value.");
			  } 
		  while (iDenominator <= 0);
   		}

   	public void output()
   		{
	   	System.out.print(this);
   		}
	
	public String toString()
   		{
		  String sReadout = iNumerator + "/" + iDenominator;
		  return sReadout;
		  }
   
   	
   	public boolean equals(Object Thor)
		  {  
		if ( this == Thor ) return true;  
		if ( !(Thor instanceof Fraction) ) return false;  
  
		Fraction other = (Fraction)Thor;  
		return  ( this.getNumerator() / this.getDenominator() == other.getNumerator() / other.getDenominator());  
 		}  
   	
   	
   	public Fraction add(Fraction b)
   		{
		  int iCom = lcd(iDenominator, b.iDenominator);
		  Fraction fComA = new Fraction();
		  Fraction fComB = new Fraction();
		  fComA = convert(iCom);
		  fComB = b.convert(iCom);
		  Fraction fSum = new Fraction();
		  fSum.iNumerator = fComA.iNumerator + fComB.iNumerator;
		  fSum.iDenominator = iCom;
		  fSum = fSum.reduce();
		  return fSum;
   		}

   	public Fraction subtract(Fraction b)
   		{
		  int iCom = lcd(iDenominator, b.iDenominator);
		  Fraction fComA = new Fraction();
		  Fraction fComB = new Fraction();
		  fComA = convert(iCom);
		  fComB = b.convert(iCom);
		  Fraction fDiff = new Fraction();
		  fDiff.iNumerator = fComA.iNumerator - fComB.iNumerator;
		  fDiff.iDenominator = iCom;
		  fDiff = fDiff.reduce();
		  return fDiff;
   		}

   	public Fraction multiply(Fraction b)
   		{
		  Fraction fMult = new Fraction();
		  fMult.iNumerator = iNumerator * b.iNumerator;
		  fMult.iDenominator = iDenominator * b.iDenominator;
		  fMult = fMult.reduce();
		  return fMult;
   		}

   	public Fraction divide(Fraction b)
   		{
		  Fraction fDiv = new Fraction();
		  fDiv.iNumerator = iNumerator * b.iDenominator;
		  fDiv.iDenominator = iDenominator * b.iNumerator;
		  fDiv = fDiv.reduce();
		  return fDiv;
   		}



   	private int lcd(int iDenom1, int iDenom2)
   		{
		  int iFinalDenom = iDenom1;
		  while ((iDenom1 % iDenom2) != 0)
		iDenom1 += iFinalDenom;
		  return iDenom1;
   		}

   
   	private int gcd(int iDenom1, int iDenom2)
   		{
		  int iFinalDenom = iDenom2;
		  while (iDenom2 != 0) 
			  {
			iFinalDenom = iDenom2;
			iDenom2 = iDenom1 % iDenom2;
			iDenom1 = iFinalDenom;
			  }
		  return iDenom1;
   		}

   	private Fraction convert(int iCom)
   		{
		  Fraction fFinal = new Fraction();
		  int iFinalDenom = iCom / iDenominator;
		  fFinal.iNumerator = iNumerator * iFinalDenom;
		  fFinal.iDenominator = iCom;
		  return fFinal;
   		}

   	private Fraction reduce()
   		{
		  Fraction fFinal = new Fraction();
		  int iCom = 0;
		  int iNum = Math.abs(iNumerator);
		  int iDen = Math.abs(iDenominator);
		  if (iNum > iDen)
		iCom = gcd(iNum, iDen);
		  else if (iNum < iDen)
		iCom = gcd(iDen, iNum);
		  else
		iCom = iNum;
		  fFinal.iNumerator = iNum / iCom;
		  fFinal.iDenominator = iDenominator / iCom;
		  return fFinal;
   		}
   	
	}
	
	
public class assign6
	{
	public static void main(String[] agrs)
		{
			
		  Fraction myFractions[] = new Fraction[9];  

		  
		  boolean roger = true;
		  
		  while (roger)
			  {
			  myFractions[0].input();
			  myFractions[1].input();
			  myFractions[2].input();
			  myFractions[3].input();
			  myFractions[4].input();  
		  
			  System.out.println("Fraction 1 = " + myFractions[0]);
			  System.out.println("Fraction 2 = " + myFractions[1]);
			  System.out.println("Fraction 3 = " + myFractions[2]);
			  System.out.println("Fraction 4 = " + myFractions[3]);
			  System.out.println("Fraction 5 = " + myFractions[4]);
 
		 	if(myFractions[0].getNumerator() == 8 || myFractions[0].getDenominator() == 8 || myFractions[1].getNumerator() == 8 || myFractions[1].getDenominator() == 8)
				{
				System.out.println("Thanks for entering the magical number!");
				System.exit(0);
				}
 
 			else
 				{
				  myFractions[5] = myFractions[0].add(myFractions[1]);
				  myFractions[0].output();
				  System.out.print(" + ");
				  myFractions[1].output();
 		 		System.out.print(" = ");
				  myFractions[5].output();
				  System.out.println();
		  
	 	 		myFractions[6] = myFractions[0].subtract(myFractions[1]);
   	   			myFractions[0].output();
				  System.out.print(" - ");
				  myFractions[1].output();
				  System.out.print(" = ");
				  myFractions[6].output();
				  System.out.println();
		  
				  myFractions[7] = myFractions[0].multiply(myFractions[1]);
				  myFractions[0].output();
				  System.out.print(" * ");
				  myFractions[1].output();
				  System.out.print(" = ");
				  myFractions[7].output();
				  System.out.println();
		  
				  myFractions[8] = myFractions[0].divide(myFractions[1]);
				  myFractions[0].output();
				  System.out.print(" / ");
				  myFractions[1].output();
				  System.out.print(" = ");
				  myFractions[8].output();
				  System.out.println();
				  
				  System.out.println("Fraction 1 = Fraction 2 is " + myFractions[0].equals(myFractions[1]));
					  
 				}
			  }
		  System.exit(0);
		}
	}


Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Need to create an array out of this code

Posted 27 April 2008 - 09:26 AM

My mistake, you are going to have to load the array after you declare it. Then you can use each one.

Fraction myFractions[] = new Fraction[9];

// Loop through the array adding new fraction objects to it.
for (int i = 0; i < 9; i++) {
     myFractions[i] = new Fraction();
}

// Then you can use them like so...
myFractions[0].input();



Sorry about that. :)
Was This Post Helpful? 0
  • +
  • -

#10 mclem864  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-April 08

Re: Need to create an array out of this code

Posted 27 April 2008 - 01:50 PM

It worked after that, thank you for help!
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1