1. Implement a generic version of the binary search algorithm in the problem 4.
ID: 3761336 • Letter: 1
Question
1. Implement a generic version of the binary search algorithm in the problem 4.
/**
A class for executing binary searches through an array.
*/
public class BinarySearcher
{
private int[] a;
/**
Constructs a BinarySearcher.
@param anArray a sorted array
*/
public BinarySearcher(int[] anArray)
{
a = anArray;
}
/**
Finds a value in a sorted array, using the binary
search algorithm.
@param v the value to search
@return the index at which the value occurs, or -1
if it does not occur in the array
*/
public int search(int v)
{
int low = 0;
int high = a.length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
int diff = a[mid] - v;
if (diff == 0) // a[mid] == v
return mid;
else if (diff < 0) // a[mid] < v
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
}
2. Write a program that keeps a map in which the keys of the map are objects of class Employee. An employee should have a first name, a last name, and a unique integer ID. For the performance changes and removals, lookup should be by ID. Prompt the user of the program to add or remove employees, to modify the performances, or to print all the performaces. The printout should be sorted by last name. If two employees have the same last name, then use the first name as a tie breaker. If the first names are also identical, then use the integer ID. Supply compatible hashCode and equals methods to the Employee class. Test the hash code by adding the Employee objects to a hash set.
Implementing the following methods:
public static printMenuAndGetChoice()
/**
Adds a employee based on user input. Prints an error if a employee
is added that already exists in the map.
@param PerformanceMap the map to associate employee object with a performance
@param EmployeeMap the map to associate employee id with an employee.
*/
/**
Removes a employee from the map based on user input
@param PerformanceMap the map to associate employee object with a performance.
@param EmployeeMap the map to associate employee id with an employee. */
/**
Modifies an entry based on user input. Prints an error if an invalid employee is modified.
@param PerformanceMap the map to associate employee object with a performance.
@param EmployeeMap the map to associate employee id with a employee
*/
/**
Prints the employees and performances
@param gradeMap the map to print
*/
Hint:
Use two maps.
Calculates a hashcode by combining the hashcodes of the instance variables.
@return a hashcode dependant on the instance variables
*/
public int hashCode()
{
final int HASH_MULTIPLIER = 29;
int h = HASH_MULTIPLIER * firstName.hashCode() + lastName.hashCode();
h = HASH_MULTIPLIER * h + ((Integer)id).hashCode();
return h;
}
Explanation / Answer
import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int i, number, node, array[], first, last, middle;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of elements:");
number = input.nextInt();
array = new int[number];
System.out.println("Enter " + number + " integers");
for (i = 0; i < number; i++)
array[i] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
first = 0;
last = number - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < node )
first = middle + 1;
else if ( array[middle] == node )
{
System.out.println(node + " found at location " + (middle + 1) + ".");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println(node + " is not found. ");
}
}
2)
to implement this program required daatbase...
import java.sql.Connection;
import java.util.Scanner;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
class choices
{
private int id;
private String name;
private String designation;
Scanner sc = new Scanner(System.in);
boolean isAgain = true;
int ch;
Employee em = new Employee();
public void InsertMethod()
{
System.out.println("Enter Emp Id: ");
id = sc.nextInt();
em.setId(id);
System.out.println("Enter Emp Name: ");
name = sc.next();
em.setEmp_Name(name);
System.out.println("Enter Emp Designation");
designation = sc.next();
em.setEmp_Designation(designation);
}
public void UpdateMethod()
{
System.out.println("Enter Emp Id whose record you want to update: ");
id = sc.nextInt();
em.setId(id);
System.out.println("Enter Emp new Name whose id is:"+id);
name = sc.next();
em.setEmp_Name(name);
System.out.println("Enter Emp new Designation whose id is"+id);
designation = sc.next();
em.setEmp_Designation(designation);
}
public void DeleteMethod()
{
System.out.println("Enter Emp Id whose record you want to Delete: ");
id = sc.nextInt();
}
public boolean askAgain()
{
System.out.println("Press 1 to display menu again, press any other key to exit");
ch = sc.nextInt();
if(ch == 1)
return true;
else
return false;
}
public void choice() throws ClassNotFoundException, SQLException
{
do
{
try
{
int choice;
Test ts = new Test();
EmpDbComponent db = new EmpDbComponent();
ResultSet rs;
System.out.println("Enter you choice 1.> 1 to Display 2.> 2 to Insert 3.> 3 to Update 4.> 4 to Delete 5.> 5 to Exit ");
choice = sc.nextInt();
switch(choice)
{
case 1:
{
System.out.println(" ");
System.out.println("Id Name Designation");
rs = db.fetchRecords(ts.getConnection());
while(rs.next())
{
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getInt(3)+" ");
System.out.println(rs.getString(4));
}
break;
}
case 2:
{
InsertMethod();
Employee emp = new Emp(id,name ,designation);
int k = db.insertRecords(ts.getConnection(), emp);
if(k == 0)
System.out.println("Not inserted successfully");
else
System.out.println("Inserted Successfully");
break;
}
case 3:
{
UpdateMethod();
Employee emp = new Emp(id);
int k = db.updateRecord(ts.getConnection(), emp);
if(k == 0)
System.out.println("Not Updated Succesfully");
else
System.out.println(" Record is updated Successfully");
break;
}
case 4:
{
DeleteMethod();
Employee emp = new Employee(id,name ,designation);
int k = db.deleteRecord(ts.getConnection(), emp);
if(k == 0)
System.out.println("Not Deleted ");
else
System.out.println("Deleted Successfully");
break;
}
case 5:
{
System.exit(0);
}
default:
{
System.out.println("Invalid choice..");
break;
}
}isAgain = askAgain();
}
catch(Exception e)
{
e.printStackTrace();
}
}while(isAgain);
}
}
public class Test {
public Connection getConnection() throws ClassNotFoundException, SQLException
{
return con;
}
public static void main(String args[]) throws SQLException, ClassNotFoundException
{
choices ch = new choices();
ch.choice();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.