Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 11, Problem 3AW

Explanation of Solution

Method for searching a numeric array:

//Method "search_Array" for searching the element

public static int Search_Array(int[] arr, int value)throws Exception

{

//Declare the variable "i" for controlling the loop

int i;

//Variable to hold the value

int var;

//Variable for indicating the search result

boolean found;

//Starting point of the search

i = 0;

//Storing the default values

var = -1;

//Assign "found" to "false"

found = false;

//While lopp to check the condition

while (!found && i<arr.length)

{

//Check the "val" resides in the specified index

if (arr[i] == value)

{

//Assign "found" to be "true"

found = true;

//Assign the "i" to "var"

var = i;

}

//Increment the loop variable

i++;

}

//Check the "var" equals to "-1"

if (var == -1)

//Print the error statement

throw new Exception("Element not found");

else

//Return the value of "var"

return var;

}

Explanation:

The above method “search_Array” is used to search the particular element in the array...

Blurred answer
Students have asked these similar questions
a) Write a method in java that searches a numeric array for a specified value. The method should return the subscript of the element containing the value if it is found in the array. If  the value is not found , the method should throw an exceptionof the Exception class with the error message “Element not found”. b) Write an exception class that can be thrown when a negative number is passed to a method.
Given an array of 10 names, complete the main method that outputs the name specified by the array index entered by the user. Use a try block to output the name and a catch block to catch any ArrayIndexOutOfBoundsException. Output the message from the exception object if an ArrayIndexOutOfBoundsException is caught. Output the first element in the array if the index is negative or the last element if the index is greater than the size of the array. Hint: Format the exception outputs using the getMessage() method from the exception object. Do not hard code the exception messages. Ex: If the input of the program is: 5 the program outputs: Name: Jane Ex: If the input of the program is: 12 the program outputs: Exception! Index 12 out of bounds for length 10 The closest name is: Johnny Ex: If the input of the program is: -2 the program outputs: Exception! Index -2 out of bounds for length 10 The closest name is: Ryley import java.util.Scanner; public class LabProgram {  public static void…
Write an application that displays a series of at least 10 student ID numbers (that you have stored in an array) and asks the user to enter a test letter grade for the student.   Create an Exception class named GradeException that contains a static public array of valid grade letters ('A', 'B', 'C', 'D', 'F', and 'I'), which you can use to determine whether a grade entered from the application is valid.   In your application, throw a GradeException if the user does not enter a valid letter grade.   Catch the Grade Exception and then display an appropriate message.   In addition, store an 'I' (for Incomplete) for any student for whom an exception is caught.   At the end of the application, display all student IDs and grades.   Save the files as GradeException.java and TestGrade.java.

Chapter 11 Solutions

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Ch. 11.1 - What is the call stack? What is a stack trace?Ch. 11.1 - Prob. 11.12CPCh. 11.1 - Prob. 11.13CPCh. 11.1 - Prob. 11.14CPCh. 11.2 - What does the throw statement do?Ch. 11.2 - Prob. 11.16CPCh. 11.2 - Prob. 11.17CPCh. 11.2 - Prob. 11.18CPCh. 11.2 - Prob. 11.19CPCh. 11.3 - What is the difference between a text file and a...Ch. 11.3 - What classes do you use to write output to a...Ch. 11.3 - Prob. 11.22CPCh. 11.3 - What class do you use to work with random access...Ch. 11.3 - What are the two modes that a random access file...Ch. 11.3 - Prob. 11.25CPCh. 11 - Prob. 1MCCh. 11 - Prob. 2MCCh. 11 - Prob. 3MCCh. 11 - Prob. 4MCCh. 11 - FileNotFoundException inherits from __________. a....Ch. 11 - Prob. 6MCCh. 11 - Prob. 7MCCh. 11 - Prob. 8MCCh. 11 - Prob. 9MCCh. 11 - Prob. 10MCCh. 11 - Prob. 11MCCh. 11 - Prob. 12MCCh. 11 - Prob. 13MCCh. 11 - Prob. 14MCCh. 11 - Prob. 15MCCh. 11 - This is the process of converting an object to a...Ch. 11 - Prob. 17TFCh. 11 - Prob. 18TFCh. 11 - Prob. 19TFCh. 11 - True or False: You cannot have more than one catch...Ch. 11 - Prob. 21TFCh. 11 - Prob. 22TFCh. 11 - Prob. 23TFCh. 11 - Prob. 24TFCh. 11 - Find the error in each of the following code...Ch. 11 - // Assume inputFile references a Scanner object,...Ch. 11 - Prob. 3FTECh. 11 - Prob. 1AWCh. 11 - Prob. 2AWCh. 11 - Prob. 3AWCh. 11 - Prob. 4AWCh. 11 - Prob. 5AWCh. 11 - Prob. 6AWCh. 11 - The method getValueFromFile is public and returns...Ch. 11 - Prob. 8AWCh. 11 - Write a statement that creates an object that can...Ch. 11 - Write a statement that opens the file...Ch. 11 - Assume that the reference variable r refers to a...Ch. 11 - Prob. 1SACh. 11 - Prob. 2SACh. 11 - Prob. 3SACh. 11 - Prob. 4SACh. 11 - Prob. 5SACh. 11 - Prob. 6SACh. 11 - What types of objects can be thrown?Ch. 11 - Prob. 8SACh. 11 - Prob. 9SACh. 11 - Prob. 10SACh. 11 - What is the difference between a text file and a...Ch. 11 - What is the difference between a sequential access...Ch. 11 - What happens when you serialize an object? What...Ch. 11 - TestScores Class Write a class named TestScores....Ch. 11 - Prob. 2PCCh. 11 - Prob. 3PCCh. 11 - Prob. 4PCCh. 11 - Prob. 5PCCh. 11 - FileArray Class Design a class that has a static...Ch. 11 - File Encryption Filter File encryption is the...Ch. 11 - File Decryption Filter Write a program that...Ch. 11 - TestScores Modification for Serialization Modify...Ch. 11 - Prob. 10PC
Knowledge Booster
Background pattern image
Computer Science
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
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning