Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert fraction to decimal number

i'm doing some exercises in my Java book. I'm very new to programming. Therefore, notice (in the code) that i'm still on Chapter one. Now I already did everything, I just want a confirmation if this is legitimate so I can feel free to move on next.

If not, I would sincerely appreciate to not do my code for me; I want advice.

Here's the question written in the book, "Write an application that prompts/reads the numerator and denominator of a fraction as integers, then prints the decimal equivalent of the fraction."

I'll illustrate this sentence with my code:

I did a revision here. Is this one OK?..

import java.util.*;
public class ExerciseEleven {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);

    double fraction;
    int fractionValue;
    int decimal;
    double value;

    System.out.println("Enter Numerator: ");
    int numerator = sc.nextInt();
    System.out.println("Enter Denominator: ");
    int denominator = sc.nextInt();

    fraction = (double) numerator / denominator;
    fractionValue = (int) (fraction * 10);
    decimal = fractionValue % 10;
    value = decimal * 0.1;


    System.out.println(value);
}
}

It compiles and works fine. Thank you.

like image 661
Racket Avatar asked Dec 27 '10 13:12

Racket


2 Answers

It doesn't do what task says it should. You read doubles instead of integers, and the decimal equivalent is not what you print out. Decimal equivalent for 1/2 is 0.5. And you print 5.

Also, you can pay attention to your code style: variable names are usually written in lowerCamelCase, like that : simpleVariable.

Update

now it prints what you need. However you do it not in the very right way and your indentation can still be improved.

like image 80
Vladimir Ivanov Avatar answered Oct 19 '22 07:10

Vladimir Ivanov


It's fine (I didn't read the assignment very well, did I? Kudos to Vladimir.) ...but some comments:

  • Usually you want to indent methods within the class.
  • Standard practice is to use initial caps (Numerator) only for types (e.g., classes, interfaces, enums). Variable, field, and method names should start with a lower-case letter. Now, you're free to ignore standard practice, but if you do people will have a lot of trouble reading your code. :-)
  • For rounding, you probably want to look at Math.round rather than truncating with a cast. But the assignment didn't say anything about rounding.
  • You might want to handle the case where denominator is zero.

So keeping those in mind:

import java.util.*;

public class ExcerciseEleven  {

    public static void main (String[] args) {
        Scanner sc = new Scanner (System.in);

        System.out.println("Enter Numerator: ");
        int numerator = sc.nextInt();
        System.out.println("Enter Denominator: ");
        int denominator = sc.nextInt();
        if (denominator == 0) {
            System.out.println("Can't divide by zero");
        }
        else {
            double fraction = (double)numerator / denominator;
            System.out.println(fraction);
        }
    }
}
like image 38
T.J. Crowder Avatar answered Oct 19 '22 07:10

T.J. Crowder