5 Replies - 3763 Views - Last Post: 27 January 2010 - 03:48 PM Rate Topic: -----

#1 bcky22309  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-April 09

Linear Search

Posted 26 January 2010 - 04:46 PM

import java.util.*;
import java.util.Scanner;

public class Week3Lab {
	public static void main(String[] args) {
		Person rebecca = new RebeccaZbyradowski();
		rebecca.printName();
		rebecca.printSortedLettersOfMyName();
		rebecca.printVowelsOfMyName();
		rebecca.printVowelsOfMyName2();
	}
}

abstract class Person {
	
		String name;

	public Person(String name) {
		this.name = name;
	}

	
	 // Prints the name
	 
	void printName() {
		System.out.println("My name is " + name);
	}

	//sorts
	
	abstract void printSortedLettersOfMyName();

	//searces for a,e,i,o,u
	
	abstract void printVowelsOfMyName();

	abstract void printVowelsOfMyName2();
	
	abstract void printVowelsOfMyName3();	
	
}

//represents my name

class RebeccaZbyradowski extends Person {
	
	//new instance of me
	public RebeccaZbyradowski() {
		super("Rebecca Zbyradowski");
	}

	void printSortedLettersOfMyName() {
		char[] letters = name.toLowerCase().toCharArray();
		Arrays.sort(letters);
		String sortedLetters = String.valueOf(letters);
		System.out.println("Sorted letters of my name are " + sortedLetters);
	}

	void printVowelsOfMyName() {
		char[] letters = name.toLowerCase().toCharArray();
		char[] keys = { 'a', 'e', 'i', 'o', 'u' };

		Arrays.sort(letters);
		
		//binary search
		System.out.println("Searching for vowels in my name using Binary Search: ");
		for (char key : keys) {
			System.out.print("Key: " + key);
			int index = Arrays.binarySearch(letters, key);
			if (index > 0) {
				System.out.println(" ==> FOUND");
			} else {
				System.out.println(" ==> NOT FOUND");
			}
		}
	}

	void printVowelsOfMyName2() {
		char[] letters = name.toLowerCase().toCharArray();
		char[] keys = { 'a', 'e', 'i', 'o', 'u' };
		
		//sequential search
		System.out.println("Searching for vowels in my name using Sequential Search: ");
		for (char key : keys) {
			System.out.print("Key: " + key);
			boolean found = false;
			for (char letter : letters) {
				if (letter == key) {
					found = true;
					break;
				}
			}
			//prints out wheather or not a,e,i,o,u was found within my name
			if (found) {
				System.out.println(" ==> FOUND");
			} else {
				System.out.println(" ==> NOT FOUND");
			}
		}
	}//end of vowels2
	
	//an attempt at a linear search
	
	void printVowelsOfMyName3(){
		char[] letters = name.toLowerCase().toCharArray();
		char[] keys = { 'a', 'e', 'i', 'o', 'u' };
		LinearArray searchArray = new LinearArray( letters, keys);
		
		System.out.println("Searching for vowels in my name using a Linear Search: ");
		
	   int linearSearch = searchArray.linearSearch(letters, keys);
		
		do{
			if (index > 0) {
				System.out.println(" ==> FOUND");
			} else {
				System.out.println(" ==> NOT FOUND");
			}
		}while(index != 0);
	}//end of vowels3
}



I am trying to add a Linear search to this program and all i have so far is

	//an attempt at a linear search
	
	void printVowelsOfMyName3(){
		char[] letters = name.toLowerCase().toCharArray();
		char[] keys = { 'a', 'e', 'i', 'o', 'u' };
		LinearArray searchArray = new LinearArray( letters, keys);
		
		System.out.println("Searching for vowels in my name using a Linear Search: ");
		
	   int linearSearch = searchArray.linearSearch(letters, keys);
		
		do{
			if (index > 0) {
				System.out.println(" ==> FOUND");
			} else {
				System.out.println(" ==> NOT FOUND");
			}
		}while(index != 0);
	}//end of vowels3



that of course is at the end of the enitre program and i can not for the life of me get this to work....any ideas are grateful

Is This A Good Question/Topic? 0
  • +

Replies To: Linear Search

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear Search

Posted 26 January 2010 - 08:17 PM

Please post the LinearArray Class.
Was This Post Helpful? 0
  • +
  • -

#3 bcky22309  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-April 09

Re: Linear Search

Posted 26 January 2010 - 09:00 PM

View Postn8wxs, on 26 Jan, 2010 - 07:17 PM, said:

Please post the LinearArray Class.



i thinks thats what i need.....so i need that?
I don't even know where to begin for a linear sort of characters, all the sites ive seen talk about numbers.
Was This Post Helpful? 0
  • +
  • -

#4 bcky22309  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-April 09

Re: Linear Search

Posted 26 January 2010 - 09:09 PM

View Postbcky22309, on 26 Jan, 2010 - 08:00 PM, said:

View Postn8wxs, on 26 Jan, 2010 - 07:17 PM, said:

Please post the LinearArray Class.



i thinks thats what i need.....so i need that?
I don't even know where to begin for a linear sort of characters, all the sites ive seen talk about numbers.



this is what i found, and I've tried altering both files to fit....i dont know what im doing wrong

import java.util.*;

public class LinearSearch
{
	public static void main(String[] args)
	{
		int[] data;
   	int size;
		 
	public boolean linearSearch(int key)
	{
		int index = 0;
		
		while(index < size)
		{
			if(data[index] == key) 
			{
				return true;
		 }
		 if(data[index] < key))
			{
			return false;
		 }
			 index++;
	  }
		return false;
	}
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear Search

Posted 26 January 2010 - 09:22 PM

Let's back up a second. :)

What are you trying to accomplish:
  • find the first char in keys also in letters
  • find any char in keys also in letters
  • find letters contains all chars in keys

This post has been edited by n8wxs: 26 January 2010 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 bcky22309  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-April 09

Re: Linear Search

Posted 27 January 2010 - 03:48 PM

View Postn8wxs, on 26 Jan, 2010 - 08:22 PM, said:

Let's back up a second. :)

What are you trying to accomplish:
  • find the first char in keys also in letters
  • find any char in keys also in letters
  • find letters contains all chars in keys



that does help a lil, i will give it another go
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1