Notices
Results 1 to 1 of 1

Thread: Computer Science Array Search help (Can't figure out how to correct code, code I currently have in topic)

  1. #1 Computer Science Array Search help (Can't figure out how to correct code, code I currently have in topic) 
    New Member
    Join Date
    Feb 2012
    Posts
    1
    In this assignment we are supposed to write a java applet that will compare the Sequential Search to the Binary Search. Your applet should contain just one JTextArea. First the JTextArea will contain the heading "The Array", followed by a blank line, followed by the first 300 elements of the following sequence...
    {1, 2, 4, 7, 11, 16, ...}

    with 20 lines of data & 15 elements per line. Next, the JTextArea should contain the title "Search Comparisons using the # of visits to the Array", followed by a blank line, followed by the headings "Number Sequential Binary", followed by the actual search comparisons. The following numbers I want you to search for and they should be placed in an array:
    {2, 2629, 11176, 27032, 43661}


    I'm having large amounts of trouble with the array and printing out all the numbers in the specified order, here's my code so far:

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JApplet;
    import javax.swing.JTextArea;
    import java.awt.Container;
    import java.awt.Font;
    import java.util.Random;

    public class U6A3 extends JApplet
    {
    private int[] array = new int[300];
    private int[] array2 = {2, 2629, 11176, 27032, 43661};
    private JTextArea output = new JTextArea();

    public void init()
    {
    Container container = getContentPane();
    output.SetText("The Array\n\n");
    buildArray();
    Sequential(array2);
    Binary(array2);
    printarray();
    container.add(output);
    }

    public void buildArray()
    {
    Random generator = new Random();
    for (int i=0; i<300; i++)
    {
    int x = generator.nextInt();
    array[i] = x;
    }
    }

    public int Sequential(int []target)
    {


    for (int i=0; i<array.length; i++)
    {
    if (array[i] == target[i])
    {
    return i+1;
    }
    }
    return-1;
    }

    public int Binary(int []target)
    {
    int low = 0;
    int high = array.length - 1;
    int mid, dif, count = 0;
    while (low<= high)
    {
    count++;
    mid = (low+high)/2;
    dif = array[mid] - target[mid];

    if ( dif == 0)
    {
    return count;
    }
    else if (dif < 0)
    {
    low = mid + 1;
    }
    else
    {
    high = mid - 1;
    }
    }
    return-1;
    }

    public void printarray()
    {
    for (int k=0; k<15; k++)
    {
    output.append(array[k] + "\t");
    }
    output.append("\n");
    }

    output.append("Search Comparisons using the # of visits to the Array \n");
    output.append("Number\tSequential\tBinary \n");
    output.append("------\t----------\t------\n");
    }

    This assignment is about two weeks overdue, but I can at least get it turned in for some credit if I finish by Friday... though at the rate I'm going that won't be possible without some help. Anyway, thanks for any help!


    Reply With Quote  
     

  2.  
     

Similar Threads

  1. The Da Vinci Code
    By doomsday2012 in forum Scientific Study of Religion
    Replies: 6
    Last Post: November 23rd, 2010, 10:01 AM
  2. Morse code
    By Holmes in forum General Discussion
    Replies: 0
    Last Post: August 17th, 2010, 10:28 AM
  3. Pseudo-Code Help
    By nxtgr8one in forum Computer Science
    Replies: 0
    Last Post: May 4th, 2010, 02:39 AM
  4. Replies: 0
    Last Post: March 23rd, 2009, 07:12 AM
  5. Source Code
    By blacksage in forum Computer Science
    Replies: 5
    Last Post: October 24th, 2007, 05:42 PM
Tags for this Thread

View Tag Cloud

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •