This repository contains a Java implementation of a simple object-based sequential search algorithm. The project demonstrates how to create a custom object that can be compared using the Comparable interface and how to perform a sequential search on an array of such objects.
The main focus of this project is to implement a sequential search algorithm for an array of custom objects, called SearchingObject. The SearchingObject class has a single attribute, identity, which is used to compare different instances of the class. The sequentialSearch method is used to find the index of a specific object within an array by comparing their identities.
To use this code in your project:
- Clone your repository
git clone https://github.com/2denata/SequentialSearchingObject.git
cd SequentialSearchingObject
- Compile the java code
javac SearchingObject.java
- Run the program You can create instances of SearchingObject and use the sequentialSearch method to search for a specific object in an array.
SearchingObject[] objects = new SearchingObject[]{
new SearchingObject(1),
new SearchingObject(2),
new SearchingObject(3)
};
int index = SearchingObject.sequentialSearch(objects, new SearchingObject(2));
System.out.println("Object found at index: " + index);- int identity: The unique identifier for each SearchingObject.
- compareTo(Object t) : Compares two SearchingObject instances based on their identity values.
- sequentialSearch(Object[] o, Object key) : Performs a sequential search on an array of SearchingObject instances to find the specified key.
The sequentialSearch method loops through an array of SearchingObject instances, comparing each element to the target key. If a match is found, it returns the index; otherwise, it returns -1.
