0% found this document useful (0 votes)
28 views11 pages

#7 - Jobsheet Searching Eng

The document provides steps to implement searching algorithms like sequential search and binary search in Java. It includes creating classes for students and searching, adding students to an array, searching by ID, and displaying the results. It also covers sorting arrays using merge sort and searching a 2D array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views11 pages

#7 - Jobsheet Searching Eng

The document provides steps to implement searching algorithms like sequential search and binary search in Java. It includes creating classes for students and searching, adding students to an array, searching by ID, and displaying the results. It also covers sorting arrays using merge sort and searching a 2D array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JOBSHEET VI

SEARCHING

1.1. Learning Objective


After learning this practicum course, students will be able to:
1. Define Searching algorithm
2. Create and declare searching algorithm structure
3. Implement searching algorithm

1.2. Sequential Search Method


Take a look on following class diagram! Use this class diagram as blueprint of program code in
Students class

Students
Nim: int
name: String
age: int
gpa: double
Students(ni:int, nm: String, age: int, gpa: double)
display(): void

Create a Students class to make instantiation process of Students class which will be added in an
array. There is a constructor with parameter and display() method to print all attributes available in
Students class

SearchStudent
listStd: Student[5]
idx: int
add(mhs: Mahasiswa): void
display(): void
FindSeqSearch(int cari): int
showPosition(int x,int pos): void
showData(int x,int pos) :void

Next, above class diagram will represents a class to manipulate array of objects instantiated
from Students class. For example, to add a student, display all student’s information, to search by
NIM, and to display searched student’s data later on

1.2.1. Steps
1. Create a new project in NetBeans called TestSearching
2. Then, create a new package week7.
3. Create new Students class, then declare following attributes:
4. Create a constructor in Students class with parameters (int ni, String nm, int age, double
gpa). Convert it to program code as follows:

5. Create display() method with void as its return type

6. Create a new SearchStudent class as follows.

7. Create method add() at that class! This will be used for adding objects from Students
class to listStd attribute

8. Create method display() in class SearchStudent! This display() method will be used to
print all students data available in this class. Pay attention on how we use for loops
differently. Even so, the concepts is still the same
9. Create method FindSeqSearch with integer as its return type. Then fill in the function
with sequential search algorithm.

10. Create method displayPosition with void as its return type. And write these following
code as follows

11. Create method displayData with void as its return type. And write these following code
as follows

12. Create a main class named StudentsMain and add main method as follows
13. In main method, instantiate an object in SearchStudent that consist of 5 Students, then
add all students object by calling add function in object SearchStudent

14. Add method display to print all inserted data

15. To search students by their NIM, create a search variable to hold input from user. Then
call method FindSeqSearch with its parameter is the search variable we’ve declared
before

16. Call method displayPosition from class SearchStudent.

17. Call method displayData from class SearchStudent

18. Run the program and see the result


1.2.2. Result
Match the output of your program code with following image
1.2.3. Question
1. What is the difference of method displayData and displayPosition in StudentSearch
class?
2. What is the function of break in this following program code?

3. If inserted NIM data is not sorted from smallest to biggest value, will the program
encounter an error? Is the result still correct? Why is that?

1.3. Binary Search Method


1.3.1. Steps
1. in step 1.2.1 (Sequential search), create method FindBinarySearch with integer as its data
type in class SearchStudent. Then declare the content of method FindBinarySearch with
using binary search as its searching algorithm
2. Call method FindBinarySearch from SearchStudent class in StudentsMain. Then call method
displayPosition and displayData

3. Run and see the result

1.3.2. Result
Match the output of your program code with following image
1.3.3. Question
1. Show the program code in which runs the divide process
2. Show the program code in which runs the conquer process
3. If inserted NIM data is not sorted, will the program crash? Why?
If inserted NIM data is sorted from largest to smallest value (e.g 20215, 20214 20212,
20211,20210) and element being searched is 20210. How is the result of binary search? does
it return the correct one? if not, then change the code so that the binary search executed
properly
4. Modify program above so that the students amount inserted is matched with user input

1.4. Review Divide and Conquer


1.4.1. Steps

1. Create a new package in NetBeans named MergeSortTest


2. Add class MergeSorting in this package
3. In this class, create method mergeSort that receives an array in its parameter

4. Create merge method to do data merging process from left side to the right

5. Implement merge process as follows:


6. Create sort method

7. Implement these following codes in sort method

8. In method mergeSort, call method sort with the data that wants to be sorted and initial data
range as its parameter
9. Add method printArray

10. Finally, declare the data to be sorted by using sorting process in SortMain class

1.4.2. Result
Match the output of your program code with following image

1.5. Assignments
1. Modify the searching program above with these requirements:
a. Before we search using binary search, we have to sort the data first. You can use
whichever sorting algorithm that you are comfortable with
2. Modify the searching above with these requirements:
- Search by student’s name with Sequential Search algorithm
- How is the output of the program if there is any duplicate name?
3. There is 2d array as follows:
Index 0 1 2 3 4

0 45 78 7 200 80

1 90 1 17 100 50

2 21 2 40 18 65

Based on data above, create a program to search data in 2d array, which the data to be
searched is defined by user input (using sequential search)

4. There is a 1D array as follows:


5. 0 1 2 3 4 5 6 7 8 9

12 17 2 1 70 50 90 17 2 90

Create a program to sort the array, search & display the biggest value, and print the amount
of biggest value available alongside with its position.

You might also like