The Artima Developer Community
Sponsored Link

Java Answers Forum
telephone lookup using merge sort and binary search

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

telephone lookup using merge sort and binary search Posted: May 5, 2002 4:16 PM
Reply to this message Reply
Advertisement
i was trying to do this program but i was having problems getting the binary search and merge sort to work i dont understand it that much and i was wondering if anyone could set me on the right path..
aight the purpose is to read data from an input file of a set of names and telephone numbers. the program is supposed to hve lookups by name and also by phone number and its supposed to use binary search and merge sort... now most of working so that it reads the name and numbers from the input file and prints what is looked up but i didnt get the binary search and merge sort...just looking for a lil help to understand this..

this is the telephone lookup class that reads the input...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
 
public class TelephoneLookup
{
   public static void main(String[] args)
   {  // read data from a file
      FileReader reader = null;
 
      // prompt user for filename
      ConsoleReader console = new ConsoleReader(System.in);
      System.out.print("Enter the name of the phonebook file: ");
      String fileName = console.readLine();
 
      LookupTable names = new LookupTable(1000);
      LookupTable numbers = new LookupTable(1000);
 
      //open the input file
      try
      {  reader = new FileReader(fileName);
         names.read(new BufferedReader(reader));
         numbers.read(new BufferedReader(reader));
      }
      catch(FileNotFoundException e)
      {
		  System.out.println("Cannot find input file ");
		  System.exit(1);
	  }
      catch (IOException e)
      {  System.out.println("Cannot open input file ");
         System.exit(2);
      }
 
 
 
      boolean more = true;
      while (more)
      {  System.out.println("Lookup N)ame, P)hone number, Q)uit?");
         String cmd = console.readLine().toUpperCase();
         if (cmd.equals("Q")) more = false;
         else if (cmd.equals("N"))
         {  System.out.println("Enter name:");
            String n = console.readLine();
            //call the lookup method in the LookupTable class
            System.out.println("Phone number: " + names.lookup(n));
		 }
         else if (cmd.equals("P"))
         {  System.out.println("Enter phone number:");
            String n = console.readLine();
            //call the lookup method in the LookupTable class
            System.out.println("Name: " + numbers.lookup(n));
         }
      }
   }
}
 

this is the lookup class that is supposed to lookup whichever the user chooses...
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
 
public class LookupTable
{
   private Item[] items;
   int itemCount;
 
 
   public LookupTable(int n)
   {  items = new Item[n];
      itemCount = 0;
   }
 
   public void read(BufferedReader in) throws IOException
   {  boolean more = true;
      while (more && itemCount < items.length)
      {  String k = in.readLine();
         String v = null;
         if (k != null) v = in.readLine();
         if (v != null)
         {  items[itemCount] = new Item(k, v);
            itemCount++;
         }
         else more = false;
      }
      // must sort before you can call binarySearch
      mergeSort(items, 0, itemCount - 1);
   }
 
   // THERE IS CODE IN HERE YOU MUST ADD TO MAKE THIS WORK; PERHAPS YOU WANT TWO DIFFERENT
   // lookup METHODS??
 
 
   public String lookup(String k)
   {  int pos = binarySearch(items, 0, itemCount - 1, k);
      if (pos < 0)
         return "Not found";
      else
         return items[pos].getValue();
   }
 
   public static void merge(Item[] a, int from, int mid, int to)
   {  //YOU WRITE THIS CODE
   }
 
 
   public static void mergeSort(Item[] a, int from, int to)
   {   //YOU WRITE THIS CODE
   }
 
   public static int binarySearch(Item[] v, int from, int to, String a)
   {   //YOU WRITE THIS CODE
   }
 
 
}

this is the item class that handles the keys chosen
public class Item
{  private String key;
   private String value;
 
   public Item(String k, String v)
   {  key = k;
      value = v;
   }
 
   public String getKey()
   {  return key;
   }
   public void setKey(String newKey)
   {
	  key = newKey;
   }
 
   public String getValue()
   {  return value;
   }
 
   public void setValue(String newValue)
   {
	   value = newValue;
   }
 
}

Topic: self launched thread with java Previous Topic   Next Topic Topic: Text File Data ---> Array

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use