Results 1 to 20 of 23
Thread: Help with finishing code
 12112012, 04:58 PM #1Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Help with finishing code
Hey everyone, I need some help finishing up this code...any advice
Java 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. *** } }
Last edited by Tolls; 12112012 at 05:04 PM. Reason: Fix code tag
 12112012, 05:05 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
Re: Help with finishing code
We're not going to write your code for you, you know.
What is your problem?
What have you done so far to solve your problem?
What errors/exceptions are you getting?
Where are you getting them?Please do not ask for code as refusal often offends.
** This space for rent **
 12112012, 05:22 PM #3Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
Thats why I asked for ADVICE...I never asked for you to write the entire code
 12112012, 05:28 PM #4Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
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? ***
I tried to add:
int ADD = scan.nextInt();
but it says that it can't find my variable scan....
but the problem is ADD is declared as a private static final int
 12112012, 05:32 PM #5Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
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.
//
public Rational add (Rational op2)
{
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]
 12112012, 05:41 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
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.Please do not ask for code as refusal often offends.
** This space for rent **
 12112012, 05:42 PM #7Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
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 '/'.Please do not ask for code as refusal often offends.
** This space for rent **
 12112012, 07:33 PM #8
Re: Help with finishing code
Forum Rules  particularly the third paragraph
Guide For New Members
BB Code List  Java Programming Forum
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 12132012, 05:03 PM #9Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
 12132012, 05:35 PM #10Moderator
 Join Date
 Apr 2009
 Posts
 12,709
 Rep Power
 22
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.Please do not ask for code as refusal often offends.
** This space for rent **
 01042013, 04:59 PM #11Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
Ok so here is what I did to the getFraction method...
Java 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(); }
 01042013, 05:33 PM #12Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
Ok so I changed things around a bit...heres what I got
Java 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. *** } }
 01082013, 05:23 PM #13Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
That doesn't look right....I'm really confused.... :/
 01082013, 05:44 PM #14Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
This is what i have done..
Java 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. *** } }
 01102013, 05:37 PM #15Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
Ok, I have managed to get this far in my program....
Java 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); } }
This program allows you to add, subtract, multiply, or divide two fractions.
Enter the first fraction.
Please enter the numerator:
5
Please enter the denominator:
8
Enter the second fraction.
Please enter the numerator:
7
Please enter the denominator:
9
1. Addition
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?Last edited by hockey101; 01102013 at 05:40 PM.
 01112013, 04:06 AM #16Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
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??
 01112013, 04:35 AM #17
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.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 01112013, 04:52 AM #18Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Re: Help with finishing code
Sorry...just trying to get some help
 01112013, 10:47 AM #19
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.
 01112013, 04:55 PM #20Member
 Join Date
 Dec 2012
 Posts
 44
 Rep Power
 0
Similar Threads

Need Help Finishing Method  Program Attached
By jimmyo88 in forum New To JavaReplies: 1Last Post: 01232012, 03:47 PM 
so i need help finishing my histogram plz help
By cachico12 in forum New To JavaReplies: 1Last Post: 05102011, 06:04 AM 
The last stumbling block to finishing a project: Checking if already used
By sarevok9 in forum New To JavaReplies: 11Last Post: 04262011, 07:55 AM 
finishing up a DB/Java exercise
By hayden06f4i in forum New To JavaReplies: 47Last Post: 12122010, 10:57 PM
Bookmarks