Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 9.3, Problem 24STQ
Explanation of Solution
“throws” clause:
- • This clause is used to declare the exception. This provides information to the programmer that the
program might produce an exception so it is better to give the exception handling code which maintains the normal flow of the program. - • It is usually given in the method header.
Syntax:
Method_Name throws ExceptionName
{
//statements
}
Adding “throws” clause to the given method:
//throws clause is added to the Method header
public void ...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
If given the following method:
static int GetValue(int[] array, int index)
{
return array[index];
}
Rewrite this method definition to include the try and catch blocks to catch and handle the
IndexOutOfRangeException exception inside this method. Make up your own handler.
Write all the code within the main method in the Test Truck class below. Implement the following functionality. a) Constructs two truck objects: one with any make and model you choose and the second object with gas tank capacity 10. b) If an exception occurs, print the stack trace. c) Prints both truck objects that were constructed.
import java.lang.IllegalArgumentException ;
public class TestTruck { public static void main ( String [] args ) {
heres the truck class information
A Truck can be described as having a make (string), model (string), gas tank capacity (double), and whether it has a manual transmission (or not). Include the following methods in your class definition. . An overloaded constructor which takes the make and model. This method throws an IllegalArgumentException if the make is "Jeep". An overloaded constructor which takes the gas tank capacity. This method throws an IllegalArgumentException if the capacity of the gas…
answer in java language
Chapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 9.1 - Prob. 1STQCh. 9.1 - What output would the code in the previous...Ch. 9.1 - Prob. 3STQCh. 9.1 - Prob. 4STQCh. 9.1 - Prob. 5STQCh. 9.1 - Prob. 6STQCh. 9.1 - Prob. 7STQCh. 9.1 - Prob. 8STQCh. 9.1 - In the code given in Self-Test Question 1,...Ch. 9.1 - In the code given in Self-Test Question 1,...
Ch. 9.1 - Prob. 11STQCh. 9.1 - Prob. 12STQCh. 9.1 - Prob. 13STQCh. 9.1 - Prob. 14STQCh. 9.2 - Prob. 15STQCh. 9.2 - Prob. 16STQCh. 9.2 - Prob. 17STQCh. 9.2 - Prob. 18STQCh. 9.2 - Prob. 19STQCh. 9.2 - Prob. 20STQCh. 9.2 - Suppose that, in Self-Test Question 19, we change...Ch. 9.2 - Prob. 22STQCh. 9.2 - Prob. 23STQCh. 9.3 - Prob. 24STQCh. 9.3 - Prob. 25STQCh. 9.3 - Prob. 26STQCh. 9.3 - Prob. 27STQCh. 9.3 - Prob. 28STQCh. 9.3 - Repeat Self-Test Question 27, but change the value...Ch. 9.3 - Prob. 30STQCh. 9.3 - Prob. 31STQCh. 9.3 - Prob. 32STQCh. 9.3 - Consider the following program: a. What output...Ch. 9.3 - Write an accessor method called getPrecision that...Ch. 9.3 - Prob. 35STQCh. 9.4 - Prob. 36STQCh. 9.4 - Prob. 37STQCh. 9.4 - Prob. 38STQCh. 9 - Write a program that allows students to schedule...Ch. 9 - Prob. 2ECh. 9 - Prob. 3ECh. 9 - Prob. 4ECh. 9 - Prob. 5ECh. 9 - Write code that reads a string from the keyboard...Ch. 9 - Create a class Rational that represents a rational...Ch. 9 - Prob. 9ECh. 9 - Suppose that you are going to create an object...Ch. 9 - Revise the class RoomCounter described in the...Ch. 9 - Prob. 12ECh. 9 - Write a class LapTimer that can be used to time...Ch. 9 - Prob. 1PCh. 9 - Prob. 2PCh. 9 - Prob. 3PCh. 9 - Write a program that uses the class calculator in...Ch. 9 - Prob. 3PPCh. 9 - Prob. 7PPCh. 9 - Suppose that you are in change of customer service...Ch. 9 - Write an application that implements a trip-time...
Knowledge Booster
Similar questions
- Challenge 3: Scores.java and TestScores.java Implement a Scores class. The class constructor should accept an array of scores as its argument. The class should have a method that returns the average of the scores. If any score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Implement a TestScores class that creates an array and fills it with numbers given by the user. It should catch the exception thrown when the user enters an illegal value.arrow_forwardPlease edit this class Authenticator. Remove breaks. Also create user array of size 100 rather than 3 different arrays. Also edit so it can be more OOP import java.util.Scanner; public class AuthenticatorApp { public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); Authenticator obj=new Authenticator("user.data"); String Username,Password; boolean done=false; int count=0; while(count<3) { System.out.print("Username? "); Username=input.next(); System.out.print("Password? "); Password=input.next(); obj.authenticate(Username,Password); count++; } System.out.print("Too many failed attempts... please try again later"); input.close(); } } ------------------------------------------------------------------------------------------------------------import java.io.File; import…arrow_forward2. The following program read a three-digits number from user input, reverse it and display the reversed number (No need to deal with exceptions or special cases). Read the codes, design the reverse method to complete the program. (5 marks) 浙江工业大学 import java.util.Scanner; public class DigitReverse { protected int num; public DigitReverse(int num) { if(num999) { Systen.exít(e); } else ( this.num = num; I/Write the reverse() method here public static void main(String() args) { Scanner input = new Scanner (Systen.in); System.out.print("Enter a number to reverse (101- 998): "); try { int num = input.nextInt(); DigitReverse dr = new DigitReverse(num); dr.reverse(); Systen.out.printin("The reversed number is" + dr.num); } catch(Exception ex) { System.out.printin(ex. tostring());arrow_forward
- PF Assignment 3 BS ERP 105903 Submission date: 18th April, 2021 via LMS Assignment should include:Hand Wriiten Coding + Screenshot of Output (a) Create a method GetMax() which takes two integer (int) parameters as an arguments and returns maximal of the two numbers. (b) Write a program that reads three numbers from the console and prints the biggest of them. Use the GetMax() method you just created. Write a test program that validates that the methods works correctly. Write a method that returns the digits of a given decimal number in a reversed order. For example 256, must be printed as 652. Write a program in C to check a given number is positive or negative using method. Write a program that calculates and prints the n! for any n in the range [1…10] number should be taken as input from user through main and should pass as an argument to the method. Write a program in C# Sharp to create a function to input a string and return count of number of spaces are in the…arrow_forwardBasic Java Help Each year, the Social Security Administration provides a list of baby names for boy and girls by state. You will write a program to read a file containing a list of baby names and display the top names. To start, implement the following class: baby names util = printTopNames(File, int): void printTopNames Will be a static method. It will accept two parameters, one is a File containing the list of names. The other is an integer that indicates how many names to display (ie, top 10 names, or top 50 names, etc). The integer argument could be any valid integer. You do not have to worry about an integer value large than 5 for the boy/girl names. This method should open the file and parse the file and print the names as shown below: Your program will use the Scanner class to read the data from the file (see your text for examples). You will also need to use a delimiter with the scanner so that the scanner will break the line apart based on the commas. For this file, set…arrow_forward*In Java Problem Description and Given Info Write a public static method named countGreaterThanAverage that will take an int array as it's only argument. This method will return an int value. When called, and passed an array of int values, this method will compute and return the number of values in the argument array that are greater than the average of all the values in the argument array. Here are some examples: Example 1 Given: int[] myArray = {1, 2, 3, 4, 5}; countGreaterThanAverage(myArray) should return 2 Example 2 Given: int[] myArray = {13, 7, 6, 5, 99, 10, 4, 8}; countGreaterThanAverage(myArray) should return 1 Example 3 Given: int[] myArray = {1, 1, 1}; countGreaterThanAverage(myArray) should return 0 Helpful Info: You may want to write a main method to call and test your required method There should be no System.out.print or System.out.println statements in your countGreaterThanAverage method There should be no calls to any Scanner methods (like next, nextLine, or…arrow_forward
- Subject: Object Oriented PrgrammingLanguage: Java Topic: Exception (SEE ATTACHED PHOTO FOR THE PROBLEM) Initial code to be completed:public class Matrix{ private int matrix[][]; public Matrix(){} public Matrix(int row, int column){ matrix = new int[row][column]; } //your methods here}arrow_forward10. Write a static void method named raiseAllDesksToHighest The method should accept an array of StandingDesk references. If the array reference is null, or the array is empty the method can return without doing anything. Otherwise it should adjust the heights of standing desks in the array so that they all have the height of the highest desk in the array.arrow_forwardMethod Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. Argument lists could differ in: 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Write different version of method sum() that display the sum of the values received as parameter according to the following main method content: public static void main(String[] args) { sum ( 10, 10 ); sum ( 10, 10, 10 ); sum ( 10.0, 10.0 ); sum ( 10, 10.0 ); sum ( 10.0, 10); } You have to define five functions with the specified types. Then, demonstrate the Argument Promotion concept by reducing the number of method to two.arrow_forward
- The following code is causing an exception during the runtime. class test{ public static void main(String[] args) { Object o = "Hello"; Object s = new Random(); m(o); m(s); } public static void m(Object o) { String s = (String)o; System.out.println(s.length()); }} Exception can be prevented by adding an if statement and type casting in the method m Exception can be handled by adding try-catch in the main method answers A or B both works Non of the answersarrow_forward1. ArrayIndexOutOfBoundsException Given the following code: public class ExceptionTest{ public static void main( String[] args ){ String nums[] = {“one”, “two”, “three”}; for( int i=0; i<=3; i++ ){ System.out.println(nums[i]); } } } Compile and run the TestExceptions program. The output should look like this: one two three Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ExceptionTest.main(1.java:4) Modify the TestExceptions program to handle the exception. The output of the program after catching the exception should look like this: one two three Exception caught: Array Index 3 is out of boundsarrow_forwardWhat are the errors in the following code?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning