Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 7, Problem 19E

Explanation of Solution

Sequential search method definition:

//Define "SequentialSearchTest" class

public class SequentialSearchTest

{

  /* Define the method "arraySequentialSearch".

This method is used to returns index location of given value if the value is found. Otherwise returns "-1" */

public static int arraySequentialSearch(int[] sorted_arrValues, int value)

{

//Set index position to "-1"

int index_position = -1;

//Compute the given element is found or not using "for" loop

for (int i = 0; i < sorted_arrValues.length; i++)

{

//If the given value is found in array, then

  if (sorted_arrValues[i] == value)

  {

  //Set index position to "i"

  index_position = i;

  break;

  }

  //Otherwise

  else if (sorted_arrValues[i] > value)

  {

  //Break the loop

  break;

  }

  }

  //Returns the index position of given element

  return index_position;

  }

  //Define main function

  public static void main(String[] args)

  {

  //Initializes given array values

  int[] arr = { 2, 4, 6, 8 };

  //Initializes two elements

  int element1 = 6;

  int element2 = 5;

  //Display given statement

System.out.println("The integers values in the given arrays ");

//Display array values

for (int idx = 0; idx < arr.length; idx++)

System.out.println(arr[idx] + "");

  System.out.println();

//Compute index of element 1 by calling the method "arraySequentialSearch"

int value_index1 = arraySequentialSearch(arr, element1);

/* If the given index of element 1 is less than "0", then */

if (value_index1 < 0)

//Display given statement

System.out.println("The given value " + element1 + " is not found in the sorted array. ");

  //Otherwise

  else

  //Display the index of given element

System...

Explanation of Solution

Reasons:

  • From the given question, the given array values 2, 4, 6, and 8 are in sorted order.
  • Here, the searching value for given array is “5”.
  • From the above method definition of sequential search,
    • First compare “5” with integer at each position that is starting from “0”. The value at index position “0” is “2”...

Blurred answer
Students have asked these similar questions
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.
Need Java method please. Thank you.

Chapter 7 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License