1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mars1129 [50]
3 years ago
10

Write a program to convert a fraction to a decimal. Have your program ask for the numerator first, then the denominator. Make su

re to check if the denominator is zero. If it is, print out "Error - cannot divide by zero."
Computers and Technology
1 answer:
Anton [14]3 years ago
3 0
<h2>Answer:</h2>

import java.util.Scanner;

public class FractionToDecimal {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       

       System.out.println("Please enter the numerator");

       double numerator = input.nextDouble();

       System.out.println("Please enter the denominator");

       double denominator = input.nextDouble();

       

       if(denominator == 0){

           System.err.println("ERROR - Cannot divide by zero.");

       }

       else {

           System.out.println("The result is " + (numerator/denominator));

       }

       

   }

   

}

<h2>Explanation:</h2>

// import the Scanner class to allow for user input

import java.util.Scanner;

// 1. Declare a class

public class FractionToDecimal {

   // 2. Write the main method

   public static void main(String[] args) {

       

       // 3. Create an object of the Scanner class called <em>input</em>

       Scanner input = new Scanner(System.in);

       

       // 4. Prompt the user to supply the numerator

       System.out.println("Please enter the numerator");

       

       // 5. Declare and initialize a variable <em>numerator </em>to hold the numerator

       // supplied by the user.

       // The numerator is of type double since the program does not

       // specify any. With a double both integer and floating point numbers

       // will be supported.

       double numerator = input.nextDouble();

       

       // 6. Prompt the user to enter the denominator

       System.out.println("Please enter the denominator");

       // 7. Declare and initialize a variable <em>denominator </em>to hold the

       // denominator supplied by the user.

       // The denominator is also of type double since the program does not

       // specify any. With a double, both integer and floating point numbers

       // will be supported.

       double denominator = input.nextDouble();

       // 8. Check if the denominator is or is not zero.

       // if it is zero, display an error message        

       if(denominator == 0){

           System.err.println("ERROR - Cannot divide by zero.");

       }

       // if it is not zero, then print out the result of the division

       else {

           System.out.println("The result is " + (numerator/denominator));

       }

       

   }                       // end of main method

   

}                            // end of class declaration

Please Note:

The code above has been written in Java.

The explanation segment contains comments to explain every line of the code.

But a few things are still worth noting;

i. Dividing the numerator by the denominator will convert a fraction to decimal.

ii. The object of the Scanner class <em>input </em> is used to get the inputs (numerator and denominator) from the user. Since the inputs are going to be integers or doubles, the nextDouble() method of the the Scanner class is used.

iii. If the denominator is zero, an error message will be displayed. I have used the System.err.println() method for that rather than the regular System.out.println() method. The difference is that the former will print out the error text in red color.

iv. Notice also that the else statement in the code will perform the division and also print out the result of the division along side with some text.

<em>Hope this helps!</em>

You might be interested in
Apart from the OOPs concepts learned in this lesson, identify additional concepts and write an essay on their functions. Explain
patriot [66]

Explanation:

Object Oriented Programming (OOPS or OOPS for Object Oriented Programming System) is the most widely used programming paradigm today. While most of the popular programming languages are multi-paradigm, the support to object-oriented programming is fundamental for large projects. Of course OOP has its share of critics. Nowadays functional programming seems the new trendy approach. Still, criticism is due mostly to misuse of OOP.

6 0
2 years ago
What is a recent technological breakthrough with an impact that can be compared to the invention of paper nearly 2,000 years ago
Evgesh-ka [11]
<span>Electronic communication</span>
3 0
3 years ago
Alguien porfavor me dice como cambiar de idioma en brainly
Kitty [74]

Answer:

Creo que necesitas crear una nueva cuenta en el Brainly español. Para encontrarlo, tal vez pueda buscar una pregunta aleatoria en Internet seguida de Brainly y, con suerte, encontrará la correcta. Luego crea una cuenta en ese Brainly. Al menos pienso cómo lo haces.

5 0
2 years ago
What does Technology mean to you?​
brilliants [131]
Technology is the collection of techniques, skills, methods and processes used in the production of goods or services or in the accomplishment of objectives, such as scientific investigation. Technology can be the knowledge of techniques, processes, etc. or it can be embedded in machines, computers, devices and factories, which can be operated by individuals without detailed knowledge of the workings of such things.
8 0
2 years ago
Read 2 more answers
The Next Einstein Initiative and the ________ Institute for Mathematical Sciences bring bright young learners and the best lectu
Degger [83]

Answer:

The correct answer is B. Africa

Explanation:

African Institute for Mathematical Sciences (AIMS) was founded in 2003 having its first center in Cape Town, South Africa. AIMS provides training for Africa's talented university graduates needed to enter technical professions or pursue graduate studies in technical fields. The Next Einstein Initiative (NEI) is a strategic plan to build on the success of the first AIMS centre and create a coordinated pan-African network of 15 AIMS centres by 2020, producing 750 well-qualified graduates per annum. AIMS won the Google's Project 10^100 and also the founder of AIMS won the TED Prize and announced his vision to unlock scientific talent across Africa.

5 0
3 years ago
Other questions:
  • 1. [2 points] Write a function to compute the volume of a sphere, given its radius. 2. [2 points] Write a recursive function to
    7·1 answer
  • When including multiple images in a report, it may help to create a _____ on each image so you can cross reference them througho
    11·1 answer
  • (3 points) Write a program to process two large chunks of data (e.g., a large 3D array and an array of self-defined structures w
    11·2 answers
  • How to print wireless from laptop to samsung printer?
    6·2 answers
  • Why is it uncommon for users to perform searches directly in database tables?
    15·1 answer
  • Which of these expressions is used to check whether num is equal to value?
    13·1 answer
  • Mô tả những lợi ích của việc sử dụng đa xử lý không đồng nhất trong một hệ thống di động
    8·1 answer
  • How to design remote access policy using the appropriate access controls for systems,applications, and data access
    6·1 answer
  • 9. Lael wants to determine several totals and averages for active students. In cell Q8, enter a formula using the COUNTIF functi
    9·1 answer
  • Explain the evolution of programming language​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!