public class GetIndexOfAtSignExample
{
	public int getIndexOfAtSign(String emailAddr)
	{
		int index;
		int length = emailAddr.length();
		for(index = 0; index < length; index++)
		{
			if(emailAddr.charAt(index) == '@')
			{
				return(index);
			}
		}
		return(-1);
	}
	
	public static void main(String args[])
	{
		System.out.println("GetIndexOfAtSignExample");
		GetIndexOfAtSignExample ex = new GetIndexOfAtSignExample();
		String email = "java.joe@sun.com";
		int retVal = ex.getIndexOfAtSign(email);
		System.out.println("Index of @ in " + email + ": " + retVal);
		email = "com!sun!java.joe";
		retVal = ex.getIndexOfAtSign(email);
		System.out.println("Index of @ in " + email + ": " + retVal);
	}
}


public class GetEMailHostExample
{
	public String getEMailHost(String emailAddr)
	{
		int index;
		int length = emailAddr.length();
		for(index = 0; index < length; index++)
		{
			if(emailAddr.charAt(index) == '@')
			{
				return(emailAddr.substring(index +1));
			}
		}
		return(null);
	}
	
	public static void main(String args[])
	{
		System.out.println("GetEMailHostExample");
		GetEMailHostExample ex = new GetEMailHostExample();
		String email = "java.joe@sun.com";
		String retString = ex.getEMailHost(email);
		System.out.println("Email host of " + email + ": " + retString);
		email = "com!sun!java.joe";
		retString = ex.getEMailHost(email);
		System.out.println("Email host of " + email + ": " + retString);
	}
}
	

public class ValidateStringLengthExample
{
	public static final int VALID     = 0;
	public static final int TOO_SHORT = 1;
	public static final int TOO_LONG  = 2;
	
	public int validateStringLength(String inputString)
	{
		int min = 10;	// minimum valid String length
		int max = 15;	// maximum valid String length
		if(inputString.length() < min)
		{
			return(TOO_SHORT);
		}
		if(inputString.length() > max)
		{
			return(TOO_LONG);
		}
		return(VALID);
	}
	
	public void testExamples()
	{
		int ret = validateStringLength("some input text here");
		if(ret == TOO_SHORT)
		{
			System.err.println("The input length is too short");
		}
		else
		{
			// continue with program flow
			System.err.println("The input length is not too short");
		}
		if(ret != VALID)
		{
			// Perhaps check for more specific error type here.
			System.err.println("The input length is not valid");
		}
	}
	
	public static void main(String args[])
	{
		ValidateStringLengthExample ex = new ValidateStringLengthExample();
		ex.testExamples();
	}
}



public class CheckIntArrayExample1
{
	public int checkIntArrays(int[] inputArray)
	{
		int aryIndex;
		int arySize = inputArray.length;
		for(aryIndex = 0; aryIndex < arySize; aryIndex++)
		{
			if(inputArray[aryIndex] > 20)
			{
				return(aryIndex);
			}  
		}
		return(-1);
	}
	
	public void testArray(int array[])
	{
		// Print the array then test it.
		System.out.print("Index of value > 20 for array {");
		boolean firstTime = true;
		for (int i=0; i<array.length; i++)
		{
			if (!firstTime)
				System.out.print(",");
			else
				firstTime = false;
			System.out.print(" " + array[i]);
		}
		System.out.print(" }:");
		System.out.println(checkIntArrays(array));
	}
	
	public static void main(String args[])
	{
		int arrayB[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
		int arrayC[] = { 5, 10, 15, 20, 25 };
		int arrayD[] = { 20, 40, 60, 80, 100 };
		CheckIntArrayExample1 ex = new CheckIntArrayExample1();
		ex.testArray(arrayB); // Question b
		ex.testArray(arrayC); // Question c
		ex.testArray(arrayD); // Question d
	}
}




public class CheckIntArrayExample2
{
	public static final int VALID       = 0;
	public static final int NO_MATCH    = 1;
	public static final int MULTI_MATCH = 2;
	
	public int checkIntArrays(int[] inputArray)
	{
		int aryIndex;
		int matchCount = 0;
		int arySize = inputArray.length;
		for(aryIndex = 0; aryIndex < arySize; aryIndex++)
		{
			if(inputArray[aryIndex] > 20)
			{
				matchCount++;
			}  
		}
		if(matchCount == 1)
		{
			return(VALID);
		}
		if(matchCount == 0)
		{
			return(NO_MATCH);
		}
		return(MULTI_MATCH);
	}
	
	public void testArray(int array[])
	{
		// Print the array then test it.
		System.out.print("Check array {");
		boolean firstTime = true;
		for (int i=0; i<array.length; i++)
		{
			if (!firstTime)
				System.out.print(",");
			else
				firstTime = false;
			System.out.print(" " + array[i]);
		}
		System.out.print(" }:");
		int retVal = checkIntArrays(array);
		switch (retVal)
		{
			case VALID:
				System.out.println("VALID");
				break;
			case NO_MATCH:
				System.out.println("NO_MATCH");
				break;
			case MULTI_MATCH:
				System.out.println("MULTI_MATCH");
				break;
			default:
				System.out.println("Unknown value: " + retVal);
		}
	}
	
	public static void main(String args[])
	{
		int arrayB[] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
		int arrayC[] = { 5, 10, 15, 20, 25 };
		int arrayD[] = { 20, 40, 60, 80, 100 };
		CheckIntArrayExample2 ex = new CheckIntArrayExample2();
		ex.testArray(arrayB); // Question b
		ex.testArray(arrayC); // Question c
		ex.testArray(arrayD); // Question d
	}
}




public class ExceptionTest1
{
	int[] intArray = { 1 };
	
	public ExceptionTest1()	
	{
		// nothing needs to be done here
	}
	
	public static void main(String[] argv)
	{
		System.out.println("Main Called");
		ExceptionTest1 etest = new ExceptionTest1();
		etest.doMethodA();
	}
	
	public void doMethodA()
	{
		// something safe-no Exceptions thown here
		System.out.println("Method A Called");
		// call doMethodB here
		doMethodB();
	}
	
	public void doMethodB()
	{
		System.out.println("Method B Called");
		// something careless-access past end of array
		for(int i = 0; i <= intArray.length; i++)
		{
			System.out.println("Index " + i +
				" Value: " + intArray[i]);
		}
		System.out.println("Method B Done!");
	}
}




import java.text.ParseException;
public class MyFieldParserExample
{
	public String myFieldParser(String inputStr)
		throws ParseException
	{
		int colon = inputStr.indexOf(':');
		if(colon == -1) // simple test for ':'
		{
			throw new ParseException(inputStr, inputStr.length() -1 );
		}
		// found a colon, we are safe to continue parsing
		// ...
		// This next line wasn't in the book, and could be
		// any parsing.  We picked the substring up to, but
		// not inluding, the colon.
		String parsedString = inputStr.substring(0, colon);
		return(parsedString);
	}
	
	public void testValue(String s)
	{
		String beforeColon = null;
		try
		{
			beforeColon = myFieldParser(s);
		}
		catch (ParseException e)
		{
			System.out.println(e);
			e.printStackTrace();
			// You could do more here.
		}
	}
	
	public static void main(String args[])
	{
		MyFieldParserExample ex = new MyFieldParserExample();
		ex.testValue("This is good:it has a colon.");
		ex.testValue("This is bad.  It does not have a colon.");
	}
}



import java.text.ParseException;
public class ColonParseException 
	extends ParseException
{
	public ColonParseException()
	{
		// Note, there is an error in the book.
		super("", 0);
	}
	public ColonParseException(String message)
	{
		super(message, message.length() -1);
	}
}




public class MissingColonException
	extends Exception
{
	public MissingColonException()
	{
		super();
	}
	public MissingColonException(String message)
	{
		super(message);
	}
}



import java.io.IOException;
public class EIEIOException extends IOException
{
	public EIEIOException ()
	{
		super();
	}
	public EIEIOException (String message)
	{
		super(message);
	}
}



public class OldMacDonald
{
	public void hadAFarm(Cow moo, Cow moomoo)
		throws EIEIOException
	{
		if (!moo.here() && !moo.there() && !moomoo.everywhere())
		{
			throw new EIEIOException("Trouble on the farm!");
		}
	}
	
	public static void main(String args[])
	{
		// Need some cows that fail.
		Cow cow1 = new Cow(false, false, false);
		Cow cow2 = new Cow(false, false, false);
		OldMacDonald farmer = new OldMacDonald();
		try
		{
			farmer.hadAFarm(cow1, cow2);
		}
		catch (EIEIOException ex)
		{
			System.out.println(ex);
			ex.printStackTrace();
			System.out.println("Better get some pigs.");
		}
	}
}
	
// Need a class with those method names!
class Cow
{
	private boolean here;
	private boolean there;
	private boolean everywhere;
	public Cow(boolean here, boolean there, boolean everywhere)
	{
		this.here = here;
		this.there = there;
		this.everywhere = everywhere;
	}
	
	public boolean here()
	{
		return here;
	}
	
	public boolean there()
	{
		return there;
	}
	
	public boolean everywhere()
	{
		return everywhere;
	}
}



