answer.
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
attashe74 [19]
2 years ago
15

Write an application that throws and catches an ArithmeticException when you attempt to take the square root of a negative value

. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays the message Can't take square root of negative number
Computers and Technology
1 answer:
lozanna [386]2 years ago
3 0

The code for the program described in question:

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {

       double number;

       double squareRootOfNumber;

       String userInput = null;

       Scanner scanner = new Scanner(System.in);

       System.out.println("Enter the number: ");

       userInput = scanner.next();

       number = Double.parseDouble(userInput);

       squareRootOfNumber = Math.sqrt(number);

       if (number < 0) {

           throw new ArithmeticException("Can't take square root of negative number");

       }

       System.out.format("Square root of number entered is %.2f %n", squareRootOfNumber);

   }

}

The output of the program will be:

Enter the number:

-90

Exception in thread "main" java.lang.ArithmeticException: Can't take square root of negative number at com.brainly.ans.Test.main(Test.java:18)

Explanation:

Function from standard Java library <em>java.lang.Math.sqrt</em> will <em>not throw</em> an <em>ArithmeticException</em> even its argument is negative so there is no reason to surround your code try/catch block.

Instead, we have thrown ArithmeticException manually by using <em>throw</em> keyword:

<em>throw new ArithmeticException("Can't take square root of negative number");</em>

You might be interested in
Which of the following statements is true? Using existing exceptions makes the program less robust. Always create your own excep
hjlf

Answer:

The third option is correct.

Explanation:

The following option is true because it's a derived with that Throwable class. More than that exception type, it is also other category called Error originating through that Throwable class. As any other class, the exception class will also include fields as well as functions. So, the following are the reason that describes the following answer is true according to the exception class.

The other options are not appropriate according to the following scenario.

8 0
2 years ago
If the__________ is/are out of balance, the driver will feel a pounding or shaking through the steering wheel.
RUDIKE [14]
If the VEHICLE(S) is/are out of balance, the driver will feel a pounding or shaking through the steering wheel.
This pounding <span>shortens the life of all the suspension components and hence</span> <span>produce an uneven tire wear and  which eventually </span>will<span> increase fuel consumption.</span>

4 0
2 years ago
Read 2 more answers
1. PLCs were originally designed as replacements for: a) microcomputers. c) analog controllers. b) relay control panels. d) digi
timama [110]

Answer:

PLCs (Programmable logic controller) were original designed as replacement for relay control panels.  Relay is another word for a switch. Relay control panels were made for the control of electronic instrumentents or devices. Later on PLCs were made to replace these timers and relay control panels. PLCs are more reliable, flexible and can work better in the harsh environmental situations then the relay control panels.

3 0
2 years ago
The tuna marketers' task in the "tunathewonderfish.com" website and related campaign was to ________.
Wewaii [24]
The answer to this question is "to monitor all the elements of the marketing mix for a Oregon winery". This is the main task of the tuna marketer's task in the emailing or website which is very known s tunwonderfish.com and this compaign was exclusively related to the elements of mixing in a large Oregon winery but not including the production.
4 0
2 years ago
In this image, which feature did we most likely use to quickly change the background, fonts, and layout?
MAVERICK [17]

Answer: themes

Explanation:

Took the test

3 0
2 years ago
Other questions:
  • Suppose a digital camera has a storage capacity of 256mb. how many photographs could be stored in the camera if each consisted o
    8·1 answer
  • Which of the following is technically not a programming language?
    12·2 answers
  • Which broad area of data mining applications analyzes data, forming rules to distinguish between defined classes?
    13·1 answer
  • Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. Fo
    12·1 answer
  • Create a class called Date that includes three pieces of information as instance variables—a month (type int), a day (type int),
    11·1 answer
  • NOTE: in mathematics, division by zero is undefined. So, in C++, division by zero is always an error. Given a int variable named
    9·1 answer
  • In this assignment, you are provided with almost-working code that establishes a TCP socket connection over the INET domain (tho
    11·1 answer
  • A script sets up user accounts and installs software for a machine. Which stage of the hardware lifecycle does this scenario bel
    5·1 answer
  • In the next five years there are expected to be over _____ unfilled jobs in the US in computer science.
    10·1 answer
  • Even though Wordpress is basically free, what is the company trying to accomplish?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!