Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 16, Problem 3PC
Program Plan Intro

Searching for Objects with the Binary search Algorithm

Program plan:

  • Create the class “ObjectBinarySearcher”,
    • Define the “search()” method,
    • Declare the required variables.
    • Use the while loop to check whether the searching value is found in the array or searching reaches end of the array.
      • Calculate the middle value.
    • Check whether the middle value is at index “0”. If yes, then set the Boolean value for “found” to be “true”.
      • Assign the middle value index to “position” variable.
    • Check whether the searching value when compared to middle is greater than 0.
      • If yes, search the left half of the array otherwise search the right half of the array.
    • Return the position of the element.
  • Create the class “ObjectBinarySearchTest”,
    • Define the “main()” function,
      • Assign the string values.
      • Read the input from the user through scanner to search the value in the array.
      • Call the “quicksort()” method to sort the values.
      • Execute the do…while loop to search the value in the array until it iterates all the elements in the array.
      • Call the “search()” method to find the searching element in the array.
      • If the searching value is found, then print the corresponding message otherwise print the message “element not found in the array”.
      • If the user needs to continue the choice to search the element. Press yes or “y” to continue otherwise exit the program.

Blurred answer
Students have asked these similar questions
JAVA PLEASE FAAST Write a java method that receives a string containing letters of the English alphabet and then prints the  Letters whose frequencies are not unique. Notes:  1) Ignore letter case; Uppercase and lowercase letters are considered the same.  2) Do not use methods from the Arrays class.
Functions with 2D Arrays in Java Write a function named displayElements that takes a two-dimensional array, the size of its rows and columns, then prints every element of a two-dimensional array. Separate every row by a new line and every column by a space.   In the main function, call the displayElements function and pass in the required parameters.   Output 1 2 3 4 5 6 7 8 9
DNA Max Write code to find which of the strands representing DNA in an array String[] strands representing strands of DNA has the most occurrences of the nucleotide represented by parameter nuc. Complete the definition of the class DnaMax and method definition maxStrand shown below. If more than one strand has the same maximal number of the specified nucleotide you should return the longest strand with the maximal number. All DNA strands have different lengths in this problem so the maximal strand will be unique when length is accounted for. Return this uniquely maximal strand. Each String representing a DNA strand will contain only cytosine, guanine, thymine, and adenine, represented by the characters 'c', 'g', 't', and 'a', respectively. If no strand in the array contains the specified nucleotide return the empty string "". public class DnaMax { public String maxStrand(String[] strands, String nuc) { // fill in code here } } Constraints strands will contain no more than 50 elements,…

Chapter 16 Solutions

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

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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,