Java Programming, Loose-leaf Version
8th Edition
ISBN: 9781337685917
Author: Joyce Farrell
Publisher: Cengage Learning
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 12, Problem 6RQ
Program Description Answer
A Throws Exception is referred to as a method, which detects an error condition or Exception.
Hence, the correct answer is option “A”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
create a method:
public static double division(double a, double b) throws IllegalArgumentException { }
that divides a/b and returns the division value. if b == 0 it throw the IllegalArgumentException since anything divided by O is an Illegal division operations. Complete the logic for this method.
2. Call you method in an application (main() method) be sure to include a try and catchf block, where the try {} block contains the method division that can possible throw an exception, and the following catchf} block(s) to handle the possible Exceptions.
In Java when a method throws a checked Exception, what must we always do when calling that method?
A. Make sure to declare the method as static.
B. Put it inside a try/catch block.
C. Set the permission level of the method to private.
D. Check our input, and if it will be bad input we must throw an exception before calling the method.
We are asked to add an exception to the game. Please see below.
For this part of the assignment you can start by creating your own exception in a file named NumberGuesserIllegalStateException.java, with this code:
public class NumberGuesserIllegalStateException extends Exception {
public NumberGuesserIllegalStateException(String errorMessage) {
super(errorMessage);
}
}
Next modify your code so that the higher and lower methods of both the NumberGuesser and RandomNumberGuesser should throw the exception if there are no more remaining numbers to guess. You might be able to achieve this by adding the logic to your NumberGuesser and letting RandomNumberGuesser inherit the behavior. Or you might need to add the logic to both classes. It will depend on your implementation.
Finally add a try-catch block to your number guessing game so that the user is notified if the user cheats.
Chapter 12 Solutions
Java Programming, Loose-leaf Version
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write a method that will take in a time in 24 hour format (1430) and return the time in 12 hour format (2:30pm). This method should have appropriate exception handling. If an incorrect time is sent as an argument (something out of bounds or 23(#4k in other words garbage), then your method should throw a TimeFormatException which you define. In no case should your method throw an unhandled exception! Show how to test the method to be sure.arrow_forwardQ# When JUnit testing with exceptions, which of the following is true? Group of answer choices A successful test happens whenever an exception is not thrown. Exceptions and tests should not be used together. A failed test results when you catch an exception on valid input. If you give an illegal argument to a method the JUnit test will fail. Q# When you want a method to return nothing, what do you put for the return type? Group of answer choices boolean false void null Q# Which of the following statements are true? Group of answer choices All of the options are true. Methods can take multiple primitives as arguments. Methods can take objects of other classes as arguments. A method for a class can take an object of that class as an argument. Q# What does Java guarantee is part of every class? Group of answer choices AtoString() A set of Getter methods to retrieve class variables. A method to get the binary representation of the class. A set of private class…arrow_forwardGiven the following code, the output is __. try { Integer number = new Integer("1"); System.out.println("An Integer instance."); } catch (Exception e) { System.out.println("An Exception."); } Group of answer choices A- An Exception. B- 1 C- Error: Exception D- An Integer instance. E- None of the optionsarrow_forward
- Which of the following statements is false? A software developer can write his own exceptions in Java. A try statement is associated with one and only one catch statement.. A programmer written Exception implements the Exception interface. None of the above statements are falsearrow_forwardDerive exception classes from the class you wrote in the previous exercise. Each new class should indicate a specific kind of error. For example, InvalidHourException could be used to indicate that the value entered for an hour was not an integer in the range of 1 to 12. (Also the previous answer was not preferd from the teacher cause its not simple and use def which we arent taken it)arrow_forwardi keep getting the errors of InputBook1Author110.99200Book2Author219.99100Book3Author225.00600Book4Author35.0020Book5Author48.00120OutputEnter the title of the book: Enter the name of the author: Enter the price of the book: Unhandled Exception:BookException: For Book1, ratio is invalid. Price is ¤10.99 for 0 pages. at Book.set_Price (System.Decimal value) [0x0005a] in <b0c20e1170ce40c5aed6ba11d9a91b3c>:0 at BookExceptionDemo.Main () [0x0006e] in <b0c20e1170ce40c5aed6ba11d9a91b3c>:0 [ERROR] FATAL UNHANDLED EXCEPTION: BookException: For Book1, ratio is invalid. Price is ¤10.99 for 0 pages. at Book.set_Price (System.Decimal value) [0x0005a] in <b0c20e1170ce40c5aed6ba11d9a91b3c>:0 at BookExceptionDemo.Main () [0x0006e] in <b0c20e1170ce40c5aed6ba11d9a91b3c>:0 ResultsFor Book2, ratio is invalid....Price is $19.99 for 100 pages.For Book4, ratio is invalid....Price is $5.00 for 20 pages.Book1 by Author1 Price $10.99 200 pages.Book2 by Author2 Price $10.00 100…arrow_forward
- # Split input into 2 parts: name and ageparts = input().split()name = parts[0]while name != '-1':# FIXME: The following line will throw ValueError exception.# Insert try/except blocks to catch the exception.age = int(parts[1]) + 1print('{} {}'.format(name, age))# Get next lineparts = input().split()name = parts[0] How to fix this code with try except blockarrow_forwardWrite 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 an appropriate message NB:ALSO SHOW OUTPUTarrow_forwardJava Questions - (Has 2 Parts). Based on each code, which answer out of the choices "A, B, C, D, E" is correct. Each question has one correct answer. Thank you. Part 1 - Which of these keywords is used by the calling method to guard against the exception that is thrown by called method? A. tryB. throwC. throwsD. catchE. finally Part 2 - Which of the following is correct? A. All classes of Exception extend from Error.B. All classes of Error extend from Exception.C. All classes of Exception extend from Throwable.D. All classes of Throwable extend from Exception.E. All classes of Throwable extend from RuntimeException.arrow_forward
- Programming Tip 7.2 suggests that you use a try/except biock to handic exceptrons and a separate try/finally block to close a file. What would happen if you combined the two into a single block, as below, and the open function raised an exception? try: outfile = open(filename, "w") Write output.. except IOError: Handle exception. finally outfile.close()arrow_forwardWrite a program that prompts the user to enter a length in feet and then enter a length in inches and outputs the equivalent length in centimeters. If the user enters a negative number or a non-digit number, throw and handle an appropriate exception and prompt the user to enter another set of numbers. Your error message should read A non positive number is entered and allow the user to try again. Format your output with setprecision(2) to ensure the proper number of decimals for testing!arrow_forwardProgramme Leader of ITMB wants to check whether the student number and GSM number entered by the student for the programming contest is valid or not. Write a Java program to read the Student ID and GSM Number of a student. Use a method called ValidityDetails () for checking the validity of details entered. If the Student ID doesn’t start with the characters ST and contains more than 6 letters or if the Mobile Number does not contain exactly 8 digits, throw a user defined exception InvalidDetailsException. If the details entered are valid, display the message ‘”correct details are entered!!!” otherwise display “Entered invalid details!!!!”arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT