Question

Can someone help me with the main class of my code. Here is the assignment notes....

Can someone help me with the main class of my code. Here is the assignment notes.

Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook.

Use the definition shown below for the "jumpSearch" method of the SkipSearch class.
Notice that the method is delcared static. Therefore, you do not need to create a new 
instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)"
with the appropriate 4 parameter values.

When objects are compared, you should use the "compareTo" method as in the following example.


item.compareTo(array[k])


However, not every type of object implements the compareTo method of the Comparable interface.
This is resolved with the use of the syntax " T extends Comparable<? super T>" where "T"
represents the type of objects you want to compare. On the other hand, if you only need to use
one type of data object, then you could just define your data class to implement Comparable instead.

public class SkipSearch
{
    public static <T extends Comparable<? super T>> 
                     boolean jumpSearch( T[] array, int size, T item, int skip ) 
    {
        /*  Searches the first n objects in a sorted array for a given item.
        T      -  data type or class name for the objects in the array.
        array  -  An array of Comparable objects sorted into ascending order.
        size   -  Array size.
        item   -  The item sought.
        skip   -  An integer > 0; The gap between examined items.
        returns  true if the item was found, or false if not. */


        // Insert your code here. Also define the main test class in a separate Java file.

    }
}

Here is my code:

public class SkipSearch {
  
   public static <T extends Comparable<? super T>>
   boolean jumpSearch( T[] array, int size, T item, int skip ) {

       for(int i=0; i<size; i=i+skip) {
           if(array[i].compareTo(item) == 0)
               return true;
       }
       return false;
   }
}

How would i do the main test class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package HomeworkLib;
public class SkipSearch {   

   public static <T extends Comparable<? super T>>
   boolean jumpSearch( T[] array, int size, T item, int skip ) {

   for(int i=0; i<size; i=i+skip) {
   if(array[i].compareTo(item) == 0)
   return true;
   }
   return false;
   }
      

}

package HomeworkLib;

import java.util.Scanner;

public class Driver {
  
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       int size = sc.nextInt();
      
       String array[] = new String[size];
      
       for(int i=0; i<size; i++) {
          
           array[i] = sc.next();
       }
      
       String item = sc.next();
      
       int skip = sc.nextInt();
  
       SkipSearch ob = new SkipSearch();
       System.out.println(ob.jumpSearch(array, size, item, skip));
   }

}

The main function of Driver class first takes user input through Scanner class.
These values are then passed to jumpSearch() function of SkipSearch class.

Note: The function jumpSearch() might be static in nature and no object is required to call it, but when it is called from another class, an object of the class which it belongs to is needed. If this is not done, the compiler will not be able to locate the function.

Add a comment
Know the answer?
Add Answer to:
Can someone help me with the main class of my code. Here is the assignment notes....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that t...

    Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When objects are compared, you should use the "compareTo" method as in the following example. item.compareTo(array[k]) However, not every type of object implements the compareTo method of the Comparable interface. This is resolved...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a...

    a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>>              implements MinHeapInterface<T> {    private T[] heap;      // Array of heap entries; ignore heap[0]    private int...

  • Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods...

    Check if an array is a heap in Java. Complete the method isHeapTree Together, these methods are meant to determine if an array of objects corresponds to a heap. The methods are generic, and they should work with an array of any type T that implement Comparable<T>. Implement the second method so that it uses recursion to process the complete tree/subtree whose root is at position i in the array arr. The method should return true if that tree/subtree is...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think...

    I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item)       Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT