Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating an exception class

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Creating an exception class

    I had to make two classes that search a String array for specific words, and a ItemNotFoundException class. I do not think the exception class is working correctly though. I am not completely sure though because I am unsure of what the final outcome should in fact be.

    Driver:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class HW5Driver {
     
    	public final static String FILE_AND_PATH = "longwords.txt";
    	/* 
    	 * TODO: Be sure to change the FILE_AND_PATH to point to your local 
    	 * copy of longwords.txt or a FileNotFoundException will result
    	 */	
     
     
    	//Note how we deal with Java's Catch-or-Declare rule here by declaring the exceptions we might throw
    	public static void main(String[] args) throws FileNotFoundException {
    		Scanner input = new Scanner(new File(FILE_AND_PATH));
    		int wordCount = 0;
    		ArrayList<String> theWords = new ArrayList<String>();
     
    		//read in words, count them
    		while(input.hasNext())  {
    			theWords.add( input.next() );
    			wordCount++;
    		}
     
    		//make a standard array from an ArrayList
    		String[] wordsToSearch = new String[theWords.size()];
    		theWords.toArray(wordsToSearch);
     
     
     
    		//start with the linear searches
    		tryLinearSearch(wordsToSearch, "DISCIPLINES");
    		tryLinearSearch(wordsToSearch, "TRANSURANIUM");
    		tryLinearSearch(wordsToSearch, "HEURISTICALLY");
    		tryLinearSearch(wordsToSearch, "FOO");
     
    		//and compare these results to the binary searches
    		tryBinarySearch(wordsToSearch, "DISCIPLINES");
    		tryBinarySearch(wordsToSearch, "TRANSURANIUM");
    		tryBinarySearch(wordsToSearch, "HEURISTICALLY");
    		tryBinarySearch(wordsToSearch, "FOO");
     
    	}
     
    	/**
    	 * Method tryBinarySearch
    	 * precondition: wordsToSearch is a nonempty array of Strings, and target is a non-null string to search for 
    	 * 				 in our collection of strings
    	 * postcondition: Uses a BinarySearch object (which implements this style of search) to try to find the target string
    	 */
    	private static void tryBinarySearch(String[] wordsToSearch, String target) {
    		//Todo: Build a LinearSearch class that inherits from SearchAlgorithm, and put it in the same directory as this class to successfully compile
    		SearchAlgorithm bs = new BinarySearch();
     
    		try {
    			System.out.print( target + " found at index: " + bs.search(wordsToSearch,target));
    			System.out.println( " taking " + bs.getCount() + " comparisons.");
    		} 
    		catch( ItemNotFoundException e ) {
    			System.out.println( target + ":" + e.getMessage());
    		}	
    	}
     
    	/**
    	 * Method tryLinearSearch
    	 * precondition: wordsToSearch is a nonempty array of Strings, and target is a non-null string to search for 
    	 * 				 in our collection of strings
    	 * postcondition: Uses a LinearSearch object to try to find the target string
    	 */
    	private static void tryLinearSearch(String[] wordsToSearch, String target) {
    		//Todo: Build a LinearSearch class that inherits from SearchAlgorithm, and put it in the same directory as this class to successfully compile
    		SearchAlgorithm bs = new LinearSearch();
     
    		try {
    			System.out.print( target + " found at index: " + bs.search(wordsToSearch,target));
    			System.out.println( " taking " + bs.getCount() + " comparisons.");
     
    		} 
    		catch( ItemNotFoundException e ) {
    			System.out.println( target + ":" + e.getMessage());
    		}	
    	}
     
    }


    ItemNotFoundException class:

    public class ItemNotFoundException extends Exception{
     
        public ItemNotFoundException(){
            super("Word not found.");
        }
     
        public ItemNotFoundException(String message){
            super(message);
        }
     
    }


    --- Update ---

    LinearSearch class:

    public class LinearSearch extends SearchAlgorithm{
     
        public int search(String[] words, String wordToFind){
     
            for(int i = 0; i < words.length ; i++){ 
                if(words[i].equals(wordToFind)){
                    break;
                }else{
                    incrementCount();
                }
            }
            return getCount();
     
        }
    }

    SearchAlgorithm class:

    public abstract class SearchAlgorithm {
     
    	/**
    	 * Method search: a "to-be-defined" method used to implement a specific search strategy over the given array looking for the target word
    	 * Precondition: words is a nonempty array and target is a non-null string
    	 * Postcondition: if the target word is found, return its index.  
    	 *                If not found, throw an ItemNotFoundException (a subclass which you have to make)
    	 * 
    	 */
    	public abstract int search(String[] words, String wordToFind) throws ItemNotFoundException;
     
    	/**
    	 * Utility Features: This class can be used to track the number of search comparisons
    	 *                   for use in comparing two different search algorithms
    	 */
    	private int count = 0;
    	public void incrementCount() {
    		count++;
    	}
    	public int getCount() {
    		return count;
    	}
    	public void resetCount() {
    		count = 0;
    	}
    }
    Last edited by Kristenw17; May 17th, 2013 at 10:37 PM. Reason: Missed bracket


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Creating an exception class

    You post a lot of code.
    You state that you do not know if the code works
    You do not ask a specific question which we can provide a specific answer.

    Therefore you have placed the onus upon us to compile and run your code despite that it is not our work or problem and we have no idea what the code is supposed to do. You might want to rethink your approach if you want to get help.
    Improving the world one idiot at a time!

Similar Threads

  1. Exception when creating an array
    By Majora94 in forum Collections and Generics
    Replies: 2
    Last Post: February 23rd, 2012, 04:33 PM
  2. Replies: 5
    Last Post: September 5th, 2011, 10:31 AM
  3. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  4. [SOLVED] Creating A Class
    By cb5950 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 31st, 2011, 07:27 AM
  5. [SOLVED] Class Creating Help
    By pitchblack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 10:25 PM

Tags for this Thread