Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m in java one and i need help with this program. please follow the directions

ID: 3674490 • Letter: I

Question

I'm in java one and i need help with this program. please follow the directions. I will appreciate your help. thank you.

Programming Concepts

1. Methods
2. do-whileorwhileloop

Assignment Description

You will write a program that converts a fraction to a decimal.

To do this, you must ask the user to input the numerator and denominator, and you will calculate and print out the result.

You must implement the following methods:

The main method

A method to get the numerator

A method to get the denominator

A method to calculate the result

A method to print the result

Assignment Requirements

You must write five total methods: the main() method, plus four more, to complete the program.

Use nextDouble() to get a double from user input.

You must print out the result up to three numbers after the decimal point.

You must re-prompt the user if the denominator is 0.

Assignment Suggestions

1. The main() method
a. Declare three variables:

i. double numerator: to store the numerator
ii. double denominator: to store the denominator

iii. double result: to store the result of numerator / denominator

b. Call the method getNumerator() and store it into numerator

c. Call the method getDenominator() and store it into denominator

d. Call the method calculateResult() and store it into result, passing in the values of numerator and denominator as arguments

e. Call the method printResult(), passing in result as its argument

2.getNumerator()–Noparameters

a.Prompt the user to enter in the numerator

b.Get the number from the user, and store it into a double

c.Return the number (a double)

3. getDenominator()–Noparameters

a. Prompt the user to enter in the denominator

b. Get the number from the user, and store it into a double

c. Return the number (a double)

4. calculateResult()–Takesintwoparameters,thenumerator(adouble),andthedenominator (a double)

a. Divide the two numbers and store it into a double

b. Return the result (a double)

5. printResult()–Takesinoneparameter,theresult(adouble)

a. Print out the result

sample output

user@loki:~$ java Fraction

Enter the numerator: 2

Enter the denominator: 5

The decimal value is 0.400

user@loki:~$ java Fraction

Enter the numerator: 10

Enter the denominator: 5

The decimal value is 2.000

user@loki:~$ java Fraction

Enter the numerator: 1

Enter the denominator: 3

The decimal value is 0.333

user@loki:~$ java Fraction

Enter the numerator: 5

Enter the denominator: 0

Enter the denominator: 0

Enter the denominator: 1

The decimal value is 5.000

Explanation / Answer

import java.text.DecimalFormat;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class Fraction {
   static Scanner scanner = new Scanner(System.in);

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       double numerator;
       double denominator;
       double result;
       try {

           Fraction fraction = new Fraction();
           numerator = fraction.getNumerator();
           denominator = fraction.getDenominator();
           result = fraction.calculateResult(numerator, denominator);
           fraction.printResult(result);

       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           scanner.close();

       }

   }

   /**
   * method to read return the numerator
   *
   * @return
   */
   public double getNumerator() {
       System.out.print("Enter the numerator:");
       double numerator = scanner.nextDouble();
       return numerator;

   }

   /**
   * method to read return the denominator
   *
   * @return
   */
   public double getDenominator() {
       double denominator;
       do {
           System.out.print("Enter the denominator:");
           denominator = scanner.nextDouble();
       } while (denominator == 0);
       return denominator;

   }

   /**
   * method to return the divison of numerator and denominator
   *
   * @param numerator
   * @param denominator
   * @return
   */
   public double calculateResult(double numerator, double denominator) {

       double result = numerator / denominator;
       return result;
   }

   /**
   * method to print the result
   *
   * @param result
   */
   public void printResult(double result) {
       String pattern = "#.000";
       DecimalFormat decimalFormat = new DecimalFormat(pattern);
       System.out.println("The decimal value is "
               + decimalFormat.format(result));
   }

}

OUTPUT:
Enter the numerator:2
Enter the denominator:5
The decimal value is 0.400

Enter the numerator:10
Enter the denominator:5
The decimal value is 2.000

Enter the numerator:1
Enter the denominator:3
The decimal value is 0.3333333333333333

Enter the numerator:5
Enter the denominator:0
Enter the denominator:0
Enter the denominator:1
The decimal value is 5.000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote