Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: Help with finishing code

  1. #1
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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; 12-11-2012 at 04:04 PM. Reason: Fix code tag

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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 **

  3. #3
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Help with finishing code

    Thats why I asked for ADVICE...I never asked for you to write the entire code
    akshaygoel96 likes this.

  4. #4
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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

  5. #5
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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]

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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 **

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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 **

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Help with finishing code

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Help with finishing code

    Quote Originally Posted by Tolls View Post
    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.

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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 **

  11. #11
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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();
    
       }
    Does that look right? It's not giving me an error message, but i don't know if its logically incorrect

  12. #12
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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.				 ***
        }
    
    }

  13. #13
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Help with finishing code

    That doesn't look right....I'm really confused.... :/

  14. #14
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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.				 ***
        }
    
    }

  15. #15
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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);
    
        }
    
    }
    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.
    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; 01-10-2013 at 04:40 PM.

  16. #16
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default 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??

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Help with finishing code

    Sorry...just trying to get some help

  19. #19
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

    Default 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.

  20. #20
    hockey101 is offline Member
    Join Date
    Nov 2012
    Posts
    44
    Rep Power
    0

    Default Re: Help with finishing code

    So then how would I get it to stop doing that?

Page 1 of 2 12 LastLast

Similar Threads

  1. Need Help Finishing Method - Program Attached
    By jimmyo88 in forum New To Java
    Replies: 1
    Last Post: 01-23-2012, 02:47 PM
  2. so i need help finishing my histogram plz help
    By cachico12 in forum New To Java
    Replies: 1
    Last Post: 05-10-2011, 06:04 AM
  3. Replies: 11
    Last Post: 04-26-2011, 07:55 AM
  4. finishing up a DB/Java exercise
    By hayden06f4i in forum New To Java
    Replies: 47
    Last Post: 12-12-2010, 09:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •