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
attashe74 [19]
4 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]4 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
Why is outfitting a workplace with video games in a technology development company consiered a strategic use of money
Jlenok [28]

video games help with hand-eye-coordination and they help with your strategic abilities

3 0
4 years ago
Conceptual note-taking is the act of writing down information in the order it is given. drawing attention to details with a mark
Dvinal [7]

Answer:A

Explanation:Conceptual notetaking is the act of writing down information in the order it is given.

4 0
3 years ago
Read 2 more answers
I need help also this counts as my second giveaway and last for today
kifflom [539]

Answer:

I am pretty sure its the Visual card

8 0
3 years ago
Read 2 more answers
How do i make an Array in C++ Also how do i make it so i can Multithread on Java
DIA [1.3K]

Answer:

To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.

Explanation:

3 0
4 years ago
What are logic gates ?​
Tema [17]

Explanation:

idealized model of computation or physical electronic device implementing a Boolean function, a logical operation performed on one or more binary inputs that produces a single binary output. 

8 0
3 years ago
Read 2 more answers
Other questions:
  • Where is the typical location of a touchpad inside of a laptop?
    14·1 answer
  • Scott is the CISO for a bank. In recent readings, he read about an attack where the attacker was able to enumerate all the netwo
    9·1 answer
  • What ip address cidrs are not allowed to be communicated with by our malware?
    15·1 answer
  • Describe a hybird electrical vehical
    12·1 answer
  • Linux applications are developed using ________ programming language.
    7·1 answer
  • Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the
    7·1 answer
  • 100POINTS!!!!
    9·2 answers
  • What is Data rate?<br> What is BAUD RATE?<br> What is bandwidth?
    8·1 answer
  • Create a relational database table in Microsoft Excel for the grocery store of 20 employers.
    12·1 answer
  • What are the benefits and drawbacks of a desktop utilising virtualisation and a server?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!