• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Creating a search and sort

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the instructions for the question.

Now, develop a test method to read integer values from the user into an array and then call methods LinearSearch() and BinarySearch() and printout the number of comparisons took to find the target values using each search method. Document your code and organized your outputs as follows:

Arrays Values:
Target value:

The following is what I have written. I'm not sure what to do next. My professors aren't answering questions because it is close to the break, and have said they won't until we return. Our final exam is the week we return.


 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joshua Rowland wrote:


A couple of comments on what you have so far:
  • You method name should describe an action, like "getTestValues()".
  • Your method takes a "testValue" parameter but it is not being used so it is not necessary.
  • The method returns an int which might be correct if the method were "getNumberOfTestValues()", otherwise you are probably looking at returning an int[] of testValues.
  • Variable and method names should begin with a lower case letter, "Value" is incorrect.
  • Prompt would be clearer as "Please enter the number of values to test:".

  • The next step would be to add a loop that allows the user to enter the values.
     
    Joshua Rowland
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Joshua Rowland
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So now I get junk values in my array. I would take a screenshot and add it in, but I'm having issues adding an image.
     
    Carey Brown
    Saloon Keeper
    Posts: 9742
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Much better. Need a prompt for individual values.

    Screen snap shots are discouraged here and should only be used if there's no other choice. Instead it's better to cut and paste text into the post.

    How are you displaying the values you think are incorrect? Show us the code.

    For brevity and clarity just use 'values' instead of 'testValues'.
     
    Joshua Rowland
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator







    Please enter the number of values to test :
    4
    4
    4
    4
    4
    Arrays Values: [I@41629346
    Linear search comparisons: 11
    Binary search comparisons: 2
     
    Joshua Rowland
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator



    Please enter the number of values to test :
    1
    1
    1
    Arrays Values: [I@41629346
    Linear search comparisons: 11
    Binary search comparisons: 2


    Now line 14 throws an error.
     
    Marshal
    Posts: 76879
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    No, line 14 won't compile; that is different from throwing an error. You never pass the type of an argument to a method, only its name. Delete the type.
    You appear to be creating lots of arrays and not using them all, as Carey has already mentioned. Work out how many of the arrays you are actually using, and delete the others.
    What are you doing about binary search? How are you sorting the array before searching?

    Don't use underscores in variable/method names. Use mixedCaseNames instead.
     
    Joshua Rowland
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Sheriff
    Posts: 7123
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This looks good.  I have a couple of suggestions.

    * The user interface (UI) could be improved.  Try using System.out.print() for your prompts.  Also, it's not clear when you should entering the values.  Add a prompt for that.
    * You have a lot of code in the main() method and all your methods are static.  Read MainIsAPain (that's a link) to learn a better way to launch your code.
     
    There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic