# Help with finishing code

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-11-2012, 04:58 PM
hockey101
Help with finishing code
Hey everyone, I need some help finishing up this code...any advice
Code:

``` package fractions; import java.util.Scanner; public class Main {   private static final int ADD = 1;   private static final int SUB = 2;   private static final int MULT = 3;   private static final int DIV = 4;   //-----------------------------------------------------------------   //  Gets a fraction from the user and returns it as a   //  Rational object   //-----------------------------------------------------------------   public static Rational getFraction()   {         // *** You have to write the entire method! ***         // ***    More than one line is needed!    ***   }   //-----------------------------------------------------------------   //  Gets the user's choice of operation (add, subtract, multiply,   //  or divide) and returns it   //-----------------------------------------------------------------   public static int getOperation()   {       Scanner scan = new Scanner (System.in);       System.out.println(ADD + ". Addition");       System.out.println(SUB + ". Subtraction");       System.out.println(MULT + ". Multiplication");       System.out.println(DIV + ". Division");       System.out.print("Select an operation by entering the"                       + " corresponding number: ");       int op = scan.nextInt();       // *** Here, you need to check for a valid response.       // *** If not valid, ask again until it is, then return it.   }       public static void main(String[] args)     {         System.out.println("This program allows you to add, subtract,"                         + " multiply, or divide two fractions.");         System.out.println("Enter the first fraction.");         // *** How are you going to read this from the user? ***         System.out.println("Enter the second fraction.");         // *** How are you going to read this from the user? ***         int op = getOperation();         // *** carry out the operation that was selected, ***         // *** and display the result.                                ***     } }```
• 12-11-2012, 05:05 PM
Tolls
Re: Help with finishing code
We're not going to write your code for you, you know.

What have you done so far to solve your problem?
What errors/exceptions are you getting?
Where are you getting them?
• 12-11-2012, 05:22 PM
hockey101
Re: Help with finishing code
• 12-11-2012, 05:28 PM
hockey101
Re: Help with finishing code
I did try to start at the bottom after it says:

System.out.println("Enter the first fraction.");
// *** How are you going to read this from the user? ***

but it says that it can't find my variable scan....

but the problem is ADD is declared as a private static final int
• 12-11-2012, 05:32 PM
hockey101
Re: Help with finishing code
This code does have 2 files. I completed this one:
[code/]

package fractions;

/**
*
* @author 2398
*/
public class Rational
{
private int numerator, denominator;

//--------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator sighned.
//--------------------------------------------------------------
public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;

reduce();

}

//------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest comon divisor.
//------------------------------------------------------------
private void reduce()
{
if (numerator != 0)
{
int common = gcd (Math.abs(numerator), denominator);

numerator = numerator/common;
denominator = denominator/common;
}

}

//-----------------------------------------------------------
// Computes and returns the greates common divisor of the two
// positive parameters. Uses Euclids's algorithm.
//-----------------------------------------------------------
private int gcd(int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;

}

//-----------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------
public int getNumerator ()
{
return numerator;
}

//-------------------------------------------------
// Returns the denominator of this rational number.
//-------------------------------------------------
public int getDenominator ()
{
return denominator;
}

//------------------------------------------------
// Returns the reciprocal of this rational number.
//------------------------------------------------
public Rational reciprocal ()
{
return new Rational (denominator, numerator);
}

//------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominator.
//------------------------------------------------------------
{
int commonDenominator = denominator * op2.getDenominator ();
int numerator1 = numerator * op2.getNumerator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return new Rational (sum, commonDenominator);
}

//--------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//--------------------------------------------------------------
public Rational subtact (Rational op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;

return new Rational (difference, commonDenominator);
}

//-------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-------------------------------------------------------
public Rational multiply (Rational op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();

return new Rational (numer, denom);
}

//--------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//--------------------------------------------------------------
public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}

//--------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//--------------------------------------------------------------
public boolean equals (Rational op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}

//------------------------------------------
// Returns this rational number as a string.
//------------------------------------------
public String toString ()
{
String result;

if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}

}
[/code]
• 12-11-2012, 05:41 PM
Tolls
Re: Help with finishing code
That's the sort of question a forum like this can handle.
Or is at least likely to get an answer.

Currently your Scanner 'scan' only exists in the getOperation() method.
You could either take the declaration out of there and put it as a class attribute (a static member), or create another, local, one in the main() method.

Looking at the code, though, it seems to me this should be done in the getFraction method.
• 12-11-2012, 05:42 PM
Tolls
Re: Help with finishing code
Please ensure you do the code tags correctly.
Currently you seem to be doing [code/] as the opening tag...it should not have the '/'.
• 12-11-2012, 07:33 PM
DarrylBurke
Re: Help with finishing code
• 12-13-2012, 05:03 PM
hockey101
Re: Help with finishing code
Quote:

Originally Posted by Tolls
That's the sort of question a forum like this can handle.
Or is at least likely to get an answer.

Currently your Scanner 'scan' only exists in the getOperation() method.
You could either take the declaration out of there and put it as a class attribute (a static member), or create another, local, one in the main() method.

Looking at the code, though, it seems to me this should be done in the getFraction method.

so your saying to put the scanner object in the getFraction method?

My teacher wrote this part of the code....so thats why I am confused on what to do.
• 12-13-2012, 05:35 PM
Tolls
Re: Help with finishing code
Just create a new Scanner in the getFraction method then.

The one in the getOperation method is not useable outside of that method...it is what's called a 'local' variable, local to that method.
So you need to create your own one in the getFraction method.
• 01-04-2013, 04:59 PM
hockey101
Re: Help with finishing code
Ok so here is what I did to the getFraction method...
Code:

```public static Rational getFraction()   {       Scanner scan = new Scanner (System.in);       System.out.println("Please enter the first fraction: ");       Rational frac1 = getFraction();       System.out.println("Please enter the second fraction: ");       Rational frac2 = getFraction();   }```
Does that look right? It's not giving me an error message, but i don't know if its logically incorrect
• 01-04-2013, 05:33 PM
hockey101
Re: Help with finishing code
Ok so I changed things around a bit...heres what I got
Code:

``` public class Main {   private static final int ADD = 1;   private static final int SUB = 2;   private static final int MULT = 3;   private static final int DIV = 4;   public static int frac1, frac2;   //-----------------------------------------------------------------   //  Gets a fraction from the user and returns it as a   //  Rational object   //-----------------------------------------------------------------   public static Rational getFraction()   {       Scanner scan = new Scanner (System.in);       frac1 = scan.nextInt();       frac2 = scan.nextInt();             return new Rational (frac1, frac2);         }   //---------------------------------------------------------------   //  Gets the user's choice of operation (add, subtract, multiply,   //  or divide) and returns it   //---------------------------------------------------------------   public static int getOperation()   {       Scanner scan = new Scanner (System.in);       System.out.println(ADD + ". Addition");       System.out.println(SUB + ". Subtraction");       System.out.println(MULT + ". Multiplication");       System.out.println(DIV + ". Division");       System.out.print("Select an operation by entering the"                       + " corresponding number: ");       int op = scan.nextInt();       // *** Here, you need to check for a valid response.       // *** If not valid, ask again until it is, then return it.   }       public static void main(String[] args)     {         Scanner scan = new Scanner (System.in);         System.out.println("This program allows you to add, subtract,"                         + " multiply, or divide two fractions.");         System.out.println("Enter the first fraction.");         frac1 = scan.nextInt();         System.out.println("Enter the second fraction.");         frac2 = scan.nextInt();               int op = getOperation();         // *** carry out the operation that was selected, ***         // *** and display the result.                                ***     } }```
• 01-08-2013, 05:23 PM
hockey101
Re: Help with finishing code
That doesn't look right....I'm really confused.... :/
• 01-08-2013, 05:44 PM
hockey101
Re: Help with finishing code
This is what i have done..
Code:

``` public class Main {   private static final int ADD = 1;   private static final int SUB = 2;   private static final int MULT = 3;   private static final int DIV = 4;   public static int frac1, frac2;   //-----------------------------------------------------------------   //  Gets a fraction from the user and returns it as a   //  Rational object   //-----------------------------------------------------------------   public static Rational getFraction()   {       Scanner scan = new Scanner (System.in);       frac1 = scan.nextInt();       frac2 = scan.nextInt();       return new Rational (frac1, frac2);   }   //---------------------------------------------------------------   //  Gets the user's choice of operation (add, subtract, multiply,   //  or divide) and returns it   //---------------------------------------------------------------   public static int getOperation()   {       Scanner scan = new Scanner (System.in);       System.out.println(ADD + ". Addition");       System.out.println(SUB + ". Subtraction");       System.out.println(MULT + ". Multiplication");       System.out.println(DIV + ". Division");       System.out.print("Select an operation by entering the"                       + " corresponding number: ");       int op = scan.nextInt();       // *** Here, you need to check for a valid response.       // *** If not valid, ask again until it is, then return it.   }       public static void main(String[] args)     {                 Scanner scan = new Scanner (System.in);         System.out.println("This program allows you to add, subtract,"                         + " multiply, or divide two fractions.");         System.out.println("Enter the first fraction.");         frac1 = new Rational(scan.nextInt(), scan.nextInt());         System.out.println("Enter the second fraction.");         frac2 = new Rational(scan.nextInt(), scan.nextInt());         frac3 = new Rational();               int op = getOperation();         // *** carry out the operation that was selected, ***         // *** and display the result.                                ***     } }```
• 01-10-2013, 05:37 PM
hockey101
Re: Help with finishing code
Ok, I have managed to get this far in my program....
Code:

```import java.util.Scanner; public class Main {   private static final int ADD = 1;   private static final int SUB = 2;   private static final int MULT = 3;   private static final int DIV = 4;   public static int frac1, frac2;   //-----------------------------------------------------------------   //  Gets a fraction from the user and returns it as a   //  Rational object   //-----------------------------------------------------------------   public static Rational getFraction()   {       int numer, denom;       Scanner scan = new Scanner (System.in);       System.out.println("Please enter the numerator: ");       numer = scan.nextInt();       System.out.println("Please enter the denominator: ");       denom = scan.nextInt();             return new Rational (numer, denom);   }   //---------------------------------------------------------------   //  Gets the user's choice of operation (add, subtract, multiply,   //  or divide) and returns it   //---------------------------------------------------------------   public static int getOperation()   {       Scanner scan = new Scanner (System.in);       System.out.println(ADD + ". Addition");       System.out.println(SUB + ". Subtraction");       System.out.println(MULT + ". Multiplication");       System.out.println(DIV + ". Division");       System.out.print("Select an operation by entering the"                       + " corresponding number: ");       int op = scan.nextInt();       while (op < ADD || op > DIV)       {             System.out.println("Sorry, that is not a valid response.  Please Try again.");             System.out.println(ADD + ". Addition");             System.out.println(SUB + ". Subtraction");             System.out.println(MULT + ". Multiplication");             System.out.println(DIV + ". Division");             System.out.print("Select an operation by entering the"                       + " corresponding number: ");       }           return op;         }       public static void main(String[] args)     {                 Scanner scan = new Scanner (System.in);         System.out.println("This program allows you to add, subtract,"                         + " multiply, or divide two fractions.");         System.out.println("Enter the first fraction.");         Rational fraction1 = getFraction();         System.out.println("Enter the second fraction.");         Rational fraction2 = getFraction();                       int op = getOperation();         Rational result;                 if (op == ADD)             result = fraction1.add(fraction2);         else if(op == SUB)             result = fraction1.subtact(fraction2);         else if (op == MULT)             result = fraction1.multiply(fraction2);         else if (op == DIV)             result = fraction1.divide(fraction2);     } }```
But here is what I get for the output...

This program allows you to add, subtract, multiply, or divide two fractions.
Enter the first fraction.
5
8
Enter the second fraction.
7
9
2. Subtraction
3. Multiplication
4. Division
Select an operation by entering the corresponding number: 1
BUILD SUCCESSFUL (total time: 12 seconds)

But when I try to choose an operation, it just ends the program. Why is it doing that?
• 01-11-2013, 04:06 AM
hockey101
Re: Help with finishing code
I can't seem to figure out the problem. I do notice that it says that my Result variable hasn't been used. But when I take it off, anything that has result in it causes an error. So then I put it back and then the error messages go away but then it still says the result variable is still not used. How does that work??
• 01-11-2013, 04:35 AM
DarrylBurke
Re: Help with finishing code
Don't misuse the PM system to ask moderators for help. That's a sure way to discourage anyone from trying to help you.

db
• 01-11-2013, 04:52 AM
hockey101
Re: Help with finishing code
Sorry...just trying to get some help
• 01-11-2013, 10:47 AM
SurfMan
Re: Help with finishing code
It is probably your editor telling you that the variable isn't used. You have a Rational object called result. It is assigned on lines 77, 79, 81 or 83, but nothing happens after that. The program just ends. That's why your editor says that the variable is assigned but not used.
• 01-11-2013, 04:55 PM
hockey101
Re: Help with finishing code
So then how would I get it to stop doing that?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last