Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: User Defined Decimal Precision

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post User Defined Decimal Precision

    Return a String representation of a Rational number in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.)

    I am trying to take a user defined floating-point formatted number from a rational number that is also user defined and put through arithmetic methods. Currently my issue is where to input the prompt for the user to input the value for the floating-point variable and allowing it to be put through the correct method which I defined as toFloat to format the rational number.

     
    public String toFloat() {
            float num;
            int n;
            Scanner input = new Scanner(System.in);
            System.out.print("Enter precision: ");
            n = input.nextInt();
            float numerator = this.getNumerator();
            float denominator = this.getDenominator();
            num = numerator / denominator;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(n);
            return df.format(num);
        }

    Currently I have it inside the toFloat method but the output example provided to me shows the scanner for precision being called when inputting the values for the rational number.

    public Rational inputR() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter numerator: ");
            numerator = input.nextInt();
            System.out.print("Enter denominator: ");
            denominator = input.nextInt();
            return new Rational(numerator, denominator);
            }

    The constructor Rational is defined as numerator and denominator but not precision as if I do this it prompts me to input precision twice which I do not want. My question is how could I implement the scanner for toFloat outside of the method to be called before and still applied to toFloat.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Defined Decimal Precision

    the prompt for the user
    Can you post here a sample console for what you want the user's interaction with your program to look like?

    implement the scanner for toFloat outside of the method to be called before and still applied to toFloat
    What "method" is that?
    If you want to use the same Scanner instance declared in the inputR method in the toFloat method, its reference can be passed as an argument to the toFloat method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Decimal Precision

    I would like to use the same scanner from inputR to get the variable n that would be used in toFloat rather than having a separate scanner in that method. As for the output example I am not sure how to post it here as the manage attachments is not opening. However it "should" look similar to :

    Enter numerator: 2
    Enter denominator: 4
    Enter numerator: 3
    Enter denominator: 9
    Enter precision: 2
    1. ADD
    2. SUBTRACT
    3. MULTIPLY
    4. DIVIDE
    5. EXIT
    Enter your choice:

    I am getting it to display as:

    Enter numerator: 2
    Enter denominator: 4
    Enter numerator: 3
    Enter denominator: 9
    1. ADD
    2. SUBTRACT
    3. MULTIPLY
    4. DIVIDE
    5. EXIT
    Enter your choice: 3
    Enter precision: 2

    And then my output from the arithmetic methods.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Defined Decimal Precision

    Sorry, I do not see what the difference is between: it "should" look similar to : and I am getting
    Can you add some comments in the output that says what is wrong? For example: <<< This should be 4 not 9

    use the same scanner from inputR to get the variable n that would be used in toFloat rather than having a separate scanner in that method.
    If you want to use the same Scanner instance declared in the inputR method in the toFloat method, its reference can be passed as an argument to the toFloat method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Decimal Precision

    Quote Originally Posted by Norm View Post
    Sorry, I do not see what the difference is between: it "should" look similar to : and I am getting
    Can you add some comments in the output that says what is wrong? For example: <<< This should be 4 not 9

    Enter numerator: 2
    Enter denominator: 4
    Enter numerator: 3
    Enter denominator: 9
    Enter precision: 2 <<<<<< I need precision here
    1. ADD
    2. SUBTRACT
    3. MULTIPLY
    4. DIVIDE
    5. EXIT
    Enter your choice:

    But I am getting the precision prompt after I select the arithmetic method to be called.

    Enter numerator: 2
    Enter denominator: 4
    Enter numerator: 3
    Enter denominator: 9
    1. ADD
    2. SUBTRACT
    3. MULTIPLY
    4. DIVIDE
    5. EXIT
    Enter your choice: 3
    Enter precision: 2 <<<<<<< I want this to be called before I choose add, subtract, multiply, divide, or exit.


    If you want to use the same Scanner instance declared in the inputR method in the toFloat method, its reference can be passed as an argument to the toFloat method.
    How would I go about passing a scanner as an argument. Currently I have two different scanners as when I try to use the scanner from inputR it prompts me to input the precision value twice. I have all the functions working correctly with each other and the precision works when called later in my output. What I am trying to do is input the value of that variable n for precision in toFloat before I chose the arithmetic function.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Defined Decimal Precision

    How would I go about passing a scanner as an argument.
    The same way you pass any argument to a method and receive it in the method
    Type refToType = new Type();  // define an instance the Type class
    ...
    someMethod(refToType);    //  pass reference to someMethod
    ...
    private void someMethod(Type aType) {   // receive reference to the Type object
       // here the code can use the Type object
    }

    Hard to say why the methods are called in the order that create the shown output. Can you post the code?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Decimal Precision

    Quote Originally Posted by Norm View Post
    The same way you pass any argument to a method and receive it in the method
    Type refToType = new Type();  // define an instance the Type class
    ...
    someMethod(refToType);    //  pass reference to someMethod
    ...
    private void someMethod(Type aType) {   // receive reference to the Type object
       // here the code can use the Type object
    }

    Hard to say why the methods are called in the order that create the shown output. Can you post the code?
     
    import java.text.DecimalFormat;
    import java.util.Scanner;
     
    class Rational {
     
        private int numerator;
        private int denominator;
     
        //constructors
        Rational() {
            numerator = 0;
            denominator = 1;
        }
     
        Rational(int num, int den) {
            numerator = num;
            denominator = den;
        }
     
        //getters
        public int getNumerator() {
            return numerator;
        }
     
        public int getDenominator() {
            return denominator;
        }
     
        //modifiers
        public void setNumerator(int num) {
            numerator = num;
        }
     
        public void setDenominator(int den) {
            denominator = den;
        }
     
        //Take input for rational number
        public Rational inputR() {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter numerator: ");
            numerator = input.nextInt();
            System.out.print("Enter denominator: ");
            denominator = input.nextInt();
            return new Rational(numerator, denominator);
            }
     
            public int precision() {
                int n;
                Scanner input = new Scanner(System.in);
                System.out.print("Enter precision: ");
                n = input.nextInt();
                return n;
            }
     
        // Find the GCD
     
        //Add 2 rational numbers
        public Rational add(Rational x) {
            int n;
            int d;
            n = (numerator * x.denominator) + (x.numerator * denominator);
            d = denominator * x.denominator;
            return new Rational(n, d);
        }
     
        //Subtract 2 rational numbers
        public Rational subtract(Rational x) {
            int n;
            int d;
            n = (numerator * x.denominator) - (x.numerator * denominator);
            d = denominator * x.denominator;
            return new Rational(n, d);
        }
     
     
        //multiply 2 rational numbers
        public Rational multiply(Rational x) {
            int n;
            int d;
            n = numerator * x.numerator;
            d = denominator * x.denominator;
            return new Rational(n, d);
        }
     
        public Rational divide(Rational x) {
            return new Rational(numerator * x.denominator, denominator * x.numerator);
        }
     
        public String toFloat() {
            float num;
            int n = precision();
            float numerator = this.getNumerator();
            float denominator = this.getDenominator();
            num = numerator / denominator;
            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(n);
            return df.format(num);
        }
     
        //Print rational number to string
        public String toString() {
            return numerator + "/" + denominator;
        }
     
    }
     
    public class testRational {
     
     
        public static void main(String[] args) {
     
            Rational x1 = new Rational();
            Rational x2 = new Rational();
            Rational x3;
            x1.inputR();
            x2.inputR();
     
            Scanner input = new Scanner(System.in);
     
        while (true) {
     
     
            System.out.println("1. ADD");
            System.out.println("2. SUBTRACT");
            System.out.println("3. MULTIPLY");
            System.out.println("4. DIVIDE");
            System.out.println("5. EXIT");
            System.out.print("Enter your choice: ");
     
     
            int choice;
     
     
            choice = input.nextInt();
     
            if (choice > 5) {
                System.out.println("Invalid Choice");
            }
     
            switch (choice) {
     
                case 1:
                    x3 = x1.add(x2);
                    System.out.println("a + b = " + x3 + " = " + x3.toFloat());
                    break;
     
                case 2:
                    x3 = x1.subtract(x2);
                    System.out.println("a - b = " + x3 + " = " + x3.toFloat());
                    break;
     
                case 3:
                    x3 = x1.multiply(x2);
                    System.out.println("a * b = " + x3 + " = " + x3.toFloat());
                    break;
     
                case 4:
                    x3 = x1.divide(x2);
                    System.out.println("a / b = " + x3 + " = " + x3.toFloat());
                    break;
     
                case 5:
                    System.exit(0);
                }
            }
        }
    }

    This is what I currently have with everything working except where the prompt is being brought up to format.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Defined Decimal Precision

    Where and when is the precision method called? Is it before the menu is shown?
    I want this to be called before I choose add, subtract, multiply, divide, or exit.
    Then the call to precision must be done before the menu is shown.

    Can you make a list of the steps you want the code to take to show what order you want the methods called?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User Defined Decimal Precision

    Quote Originally Posted by Norm View Post

    Can you make a list of the steps you want the code to take to show what order you want the methods called?
    The precision is being called during my switch statements as that is where the value that needs to be formatted is being output. I need precision to be called before toFormat occurs. The variable n needs to be applied to toFloat from precision.

    List:

    1. inputR to get 2 numerators and 2 denominators
    2. precision to get variable n that is applied to toFloat
    3. User inputs what arithmetic method they want to execute
    4. toFloat
    5. output statement containing the fraction from the arithmetic function and number with decimal format

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: User Defined Decimal Precision

    Where in the list is: create 2 Rational objects?
    Calls to inputR methods for each of the above created Rational objects; What is done with the Rational object created in the method?
    Where will the results of the call to precision be saved? Where will the saved value be used?
    Executing the menu items will create a 3rd Rational object to hold the results of the arithmetic operation chosen in the menu

    What happens to the precision value? Is it only used by the 3rd Rational object? When is the value needed?
    Can its value be passed as an argument to the toFloat() method?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. User defined classes and loops
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 22nd, 2013, 03:20 PM
  2. User Defined Criteria
    By SimonTemplar in forum Java Theory & Questions
    Replies: 0
    Last Post: February 20th, 2013, 07:02 PM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 10:23 PM
  4. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM