QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A ICSE2014-09A #7444

Question:

Answer:

import java.util.Scanner;

 public class Search {

 public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter year of graduation: ");
 int graduationYear = scanner.nextInt();
 int[] years = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
 boolean found = false;
 int left = 0;
 int right = years.length - 1;
 while (left <= right) {
 int middle = (left + right) / 2;
 if (years[middle] == graduationYear) {
 found = true;
 break;
 } else if (years[middle] < graduationYear) {
 left = middle + 1;
 } else {
 right = middle - 1;
 }
 }
 if (found) {
 System.out.println("Record exists");
 } else {
 System.out.println("Record does not exist");
 }
 }
 }