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
ki77a
1 month 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:
Harlamova29_29 [932]1 month ago
3 0

The code relevant to the problem 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("Please enter a number: ");

       userInput = scanner.next();

       number = Double.parseDouble(userInput);

       squareRootOfNumber = Math.sqrt(number);

       if (number < 0) {

           throw new ArithmeticException("Cannot compute the square root of a negative number");

       }

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

   }

}

The program will output:

Please enter a number:

-90

Exception in thread "main" java.lang.ArithmeticException: Cannot compute the square root of a negative number at com..ans.Test.main(Test.java:18)

Explanation:

The standard Java library function java.lang.Math.sqrt does not throw an ArithmeticException for negative arguments, hence there’s no need to enclose your code in a try/catch block.

Instead, we manually trigger ArithmeticException using the throw keyword:

throw new ArithmeticException("Cannot compute the square root of a negative number");

You might be interested in
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename
amid [800]

This code is in Python. I'm not certain what programming language you need.

Refer to the image:

7 0
1 month ago
Read 2 more answers
Which of the following best describes how computing devices represent information? A. A computer will either represent informati
oksian1 [797]

Answer:

The answer is B.

Explanation:

The justification for choosing B is that "bytes" represent the correct method for storing data in binary form of 0s and 1s.

5 0
1 month ago
Read 2 more answers
During normal Windows operations, you receive a BSOD, and your computer shuts down. You restart the computer and receive the err
Amiraneli [921]
The correct answer to this query is "Hard drive." Explanation: Given the context of the inquiry, it's suggested that the most likely cause of this issue is the improper connection of the video cable. An incorrectly connected video cable can lead to display disruptions, such as the screen flashing black. While overheating of the GPU could also be a potential cause, it typically results in visual artifacts rather than shifting displays. Therefore, "Hard drive" is indeed the right answer.
4 0
16 days ago
When handling project scope creep, which are two things that all parties involved need to be aware of?
zubka84 [942]

Additional resources required for the projects

Added time necessary for the project

Clarification:

In any project management scenario, there will naturally be unexpected changes and additional needs, hence to successfully complete a project, one must allocate more time and resources. It is advisable that, based on the project specifics, the end user should maintain a sufficient buffer to accommodate any variations in human resources and the extra time necessary for project completion.

When planning the project, a consideration of extra time per each task is essential.

Every task within project management is categorized under distinct scopes of work.

3 0
27 days ago
In mathematics, the factorial of a positive integer n, denoted as n! , is the product of all positive integers less than or equa
oksian1 [797]
public static int factorial(int n) { if (n >= 1 && n <=12) { if (n == 1) return 1; else return n * factorial(n - 1); } else return -1; } Explanation: The factorial method takes a single integer n as input. It checks if n is within the range of 1 to 12. If it is, it further checks if n equals 1. If it is indeed 1, it returns 1 as the factorial. Otherwise, it recursively calls itself with n decreased by 1, multiplying the result by n. If n is outside this range, the method returns -1 indicating the input is invalid.
6 0
10 days ago
Read 2 more answers
Other questions:
  • Why were the practitioners of alternative software development methods not satisfied with the traditional waterfall method?
    9·1 answer
  • In the Scrum board, prioritizing the issue backlog is done in the ———- mode.
    7·1 answer
  • Edhesive 2.3 code practice question 1​
    11·1 answer
  • Splunk In most production environments, _______ will be used as the source of data input?
    12·1 answer
  • Charlie has a large book collection. He was keeping track of it in a spreadsheet, but it has grown big enough that he wants to c
    10·1 answer
  • 1. Write the Python code needed to perform the following:2. Calculate state withholding tax (stateTax) at 6.5 percent3. Calculat
    9·1 answer
  • Which of the following is true? a. Pseudocode is used to describe an algorithm. b. Pseudocode is not an actual computer programm
    11·1 answer
  • On the planet Sigma, robots excavate chunks of a very precious cristaline. These chunks can be divided into smaller part only on
    15·1 answer
  • A wireless network does not benefit like a wired network does, when it comes to collision reduction. Which device reduces collis
    6·1 answer
  • What problem does the DNS solve? How does the DNS help the world wide web scale so that billions of users can access billions of
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!