Question
Q1 :Write a Java program which finds the number of occurrences of an element x in a sorted array A using binary search. 1- Ask
Q1 :Write a Java program which finds the number of occurrences of an element x in a sorted array A using binary search.
1- Ask the user to enter the array elements.
2- Ask the user to enter the target to search for occurrences.
3- Write a function: BinarySearch (int[] nums, int target, boolean searchFirst) to find the first or last occurrence of a given number in a sorted integer array. If (searchFirst) is true, return the first occurrence of the number; otherwise, return its last occurrence.
Hint: First occurrence of key 3 occurs at index_a = (1) and the last occurrence of 3 occurs index_b= (4) ,and the number of occurrence is the difference between index_a and index_b.
***********************************
Q2 :A. Write a class Student which contains the following data and methods
Data:
Name (string)
ID (integer)
Level (integer)
GPA (double)
Methods:
Get and set for each data item
Print all students
B. Main class contain :
BinarySum Method:
Binary recursion method which receives an array of students, and calculate the sum of the GPA of all students then returns the sum.
Main Method:
- Create an array of 4 students then assign GPA values for each student (no need
to read information from user).
print GPA values for each student.
Compute the GPA sum of all students using method BinarySum method
Compute the average GPA of all students
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started