S.O.S. Mathematics CyberBoard

Your Resource for mathematics help on the web!
It is currently Wed, 19 Jun 2013 18:38:54 UTC

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: please help me with those methods. i almost done with class.
PostPosted: Thu, 12 May 2005 04:52:40 UTC 
Offline
Member

Joined: Sun, 20 Mar 2005 05:04:22 UTC
Posts: 41
/ Student Unit Program Version
// This same file is used for 80, 90 and 100 point versions.
// Initially, the file is ready for the 80-point version. You must remove
// comments for the 90 and 100 point versions.


import java.io.*;
import java.util.Random;


public class Lab18bst
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("\nEnter list size ===>> ");
int listSize = Integer.parseInt(input.readLine());
List array = new List(listSize);

array.display();
array.pause();
// System.out.println("\nThe largest number in the list is " + array.getMax());
// System.out.println("\nThe smallest number in the list is " + array.getMin());
array.sortList();
array.display();
array.pause();

System.out.print("\nEnter search number 1 ===>> ");
int searchNumber1 = Integer.parseInt(input.readLine());
System.out.print("Enter search number 2 ===>> ");
int searchNumber2 = Integer.parseInt(input.readLine());
array.linearSearch(searchNumber1);
array.pause();
// array.binarySearch(searchNumber1);
// array.pause();
array.linearSearch(searchNumber2);
array.pause();
// array.binarySearch(searchNumber2);
// array.pause();
}
}


class List
{
private int intArray[]; // stores array elements
private int size; // number of elements in the array

public List(int s)
{
Random rand = new Random(12345);
}

public void display()
{
System.out.println("\nDISPLAYING ARRAY ELEMENTS");
for (int k = 0; k < size; k++)
System.out.print(intArray[k] + "\t");
}

public void pause() throws IOException
{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String dummy;
System.out.print("\nPress <Enter> to continue ===>> ");
dummy = input.readLine();
}

/* Required for 100 point version
public int getMax()
{

}

public int getMin()
{

}
*/

private void swap(int x, int y)
{
int temp = intArray[x];
intArray[x] = intArray[y];
intArray[y] = temp;
}


public void sortList()
{
System.out.println("\nPERFORMING SORTING ALGORITHM");
for (int p = 1; p < size; p++)
for (int q = 0; q < size-p; q++)
if (intArray[q] > intArray[q+1])
swap(q,q+1);


}

public void linearSearch(int sn)
{
System.out.println("\nPERFORMING LINEAR SEARCH");
boolean found = false;
for (int k = 0; k < size; k++)
if (intArray[k] == sn)
found = true;
return found;


}

/* Required for 90 and 100 point versions
public void binarySearch(int sn)
{
System.out.println("\nPERFORMING BINARY SEARCH");

}
*/

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu, 12 May 2005 16:35:30 UTC 
Put them in code tags. Anyway, I am NOT going to help you with the binarySearch method (as in, I will not write it) because the algorithm is very important and there's no reason why, after a little reading, you can't implement it yourself. It'll be in your text, and if for some strange reason it's not, http://www.google.com; It's very important and you need to know how to implement it.

This is what I would do...

Code:
   class List
    { 
         private boolean isSorted = false;
           // rest of variables.
          public List (int s)
          {
              // rest of construction
              // but need to add:
              this.size = s;
          }
         public int getMax()
         {
             if( !isSorted)
                sortList();
             return intArray[size-1];
         }
         public int getMin()
         {
              if(!isSorted)
                  sortList();
             return intArray[0];
         }

         public void sortList()
         {
               // Everything else
               this.isSorted = true;
          }

          public void binarySearch(int sn)
          {
                  if(!isSorted)
                     sortList();
                  // Rest of implementation.
           }
       // Everything else
     }


-- X Conrad X


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Contact Us | S.O.S. Mathematics Homepage
Privacy Statement | Search the "old" CyberBoard

users online during the last hour
Powered by phpBB © 2001, 2005-2011 phpBB Group.
Copyright © 1999-2013 MathMedics, LLC. All rights reserved.
Math Medics, LLC. - P.O. Box 12395 - El Paso TX 79913 - USA