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
anzhelika [568]
3 years ago
5

Write a program the prompts the user to enter a length in feet and inches and outputs the equivalent length in centimeters.If th

e user enters a negative number or a nondigit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers.
Computers and Technology
1 answer:
antiseptic1488 [7]3 years ago
8 0

Answer:

The solution is written in Java.

  1. public class Main {
  2.    public static void main(String[] args) {
  3.        double CM_PER_INCH = 2.54;
  4.        double CM_PER_FOOT = 30.48;
  5.        double feet = 0;
  6.        double inches = 0;
  7.        double centimeter;
  8.        Scanner input = new Scanner(System.in);
  9.        try{
  10.            System.out.print("Please enter feet: ");
  11.            String input_feet = input.nextLine();
  12.            feet = Double.parseDouble(input_feet);
  13.            System.out.print("Please enter inch: ");
  14.            String input_inches = input.nextLine();
  15.            inches = Double.parseDouble(input_inches);
  16.            if(feet >= 0 && inches >=0){
  17.                centimeter = feet * CM_PER_FOOT + inches * CM_PER_INCH;
  18.                System.out.println("The total centimeter is " + centimeter);
  19.            }else{
  20.                throw new IllegalArgumentException();
  21.            }
  22.        }
  23.        catch(NumberFormatException ex){
  24.            System.out.println("The input must be digit.");
  25.            System.out.println("Please re-enter input.");
  26.        }
  27.        catch(IllegalArgumentException e){
  28.            System.out.println("The input must be positive number.");
  29.            System.out.println("Please re-enter input.");
  30.        }
  31.    }
  32. }

Explanation:

There are two types of errors required to captured: Negative value error and non-digit error.

Hence, we can define two error handling blocks as in (Line 25-28 and Line 29 - 32) to handle those two errors, respectively.

To check for non-digit error, we can use parseDouble method (Line 13 & 16). If the input is not a digit, the exception, NumberFormatException will be raised. If the input is negative value, IllegalArgumentException will be raised.

If everything goes smooth, the program will calculate the centimeter and display it to terminal (Line 19-20).

You might be interested in
what type of clause must you always use with DEKETE or UPDATE to avoid inadvertently changing data elsewhere in the database​
Shkiper50 [21]

While you’re using the DELETE or UPDATE to change the data in any database like MySQL or ORACLE, the WHERE clause can be used to avoid inadvertently changing data everywhere.

<u>Explanation:</u>

The WHERE clause checks the condition and the changes mentioned by the DELETE or UPDATE command will be done if the condition is true. There are also other clauses like HAVING, IN which will have range either inclusive or exclusive.

It is also noted that in MySQL, there is a method called WorkBench which enables safe mode and doesn’t execute statements without the WHERE clause.

5 0
3 years ago
Jeanne writes a song, and Raul wants to perform
Goryan [66]
B) ask jeanne for permission
6 0
3 years ago
Read 2 more answers
SP 800-14, Generally Accepted Principles and Practices for Securing Information Technology Systems, provides best practices and
Alika [10]

Answer:

blueprint.

Explanation:

Generally Accepted Principles and Practices for Securing Information Technology Systems, provides best practices and security principles that can direct the security team in the development of a security blueprint.

3 0
3 years ago
Read 2 more answers
Do you know how to change your grades on a printer???????????
BabaBlast [244]

Answer:

To change ur grade make sure to do it on the website first by right clicking your mouse and clicking inspect element and once done changing x out and it will save

Explanation:

5 0
3 years ago
The Mark Table of Contents Entry option is used for marking nonheading text for the table of contents. the options are manually
enyata [817]

Answer:number 1

Explanation:asian culture

5 0
3 years ago
Read 2 more answers
Other questions:
  • Instructions:Type the correct answer in the box. Spell all words correctly.
    14·2 answers
  • Consider the following sequence of items 10, 36, 25, 54, 37, 12, 75, 68, 42, 86, 72, 90. Insert these items in the order above,
    5·1 answer
  • Olivia creates a personal budget. She enters her current savings account balance in cell D3. In cell A3, she calculates her inco
    8·1 answer
  • zeroIt is a function that takes one argument and returns no value. The function stores the value 0 back into the parameter. x is
    8·1 answer
  • ​if a primary key combines two or more fields, then it is called a _____.
    14·1 answer
  • The larger the Word Size of a computer
    6·1 answer
  • Recently, Walmart offered a wireless data contract based on bandwidth used, with a minimum monthly charge of $42 for up to 5 gig
    15·1 answer
  • Is the flow of power reversible in a leadscrew?
    11·1 answer
  • How does xm satellite deter nonsubscribers from listening to its transmissions? Does this make the radio programing a private go
    10·1 answer
  • Global communication and transportation technologies are an example of a(n) ____
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!