Difference between revisions of "CSC212 Lab 5 2014"

From dftwiki3
Jump to: navigation, search
(Throwing Exceptions)
(Throwing Exceptions)
Line 385: Line 385:
 
* Fix the errors by making the method '''get2Ints()''' throw an '''InputMismatchException''' exception.
 
* Fix the errors by making the method '''get2Ints()''' throw an '''InputMismatchException''' exception.
 
* Enclose the call to the method in main by a '''try/catch''' block.
 
* Enclose the call to the method in main by a '''try/catch''' block.
* Run your program again on the bad data, and <font color="magenta">verify that you do not get the inputMismatchException any longer.</font>  You get another type of exception, though.  You should be able to fix it!
+
* Run your program again on the bad data, and <font color="magenta">verify that you do not get the inputMismatchException any longer.</font>  You get another type of exception, though.   
 +
* In Java, if a method needs to throw more than one exception, you can indicate it as follows:
 +
<br />
 +
:::<source lang="java">
 +
public void someMethod( someArgs ) throws A_Exception, B_Exception {
 +
    ...
 +
}
 +
</source>
 +
 
 +
You should be able to fix it!

Revision as of 12:33, 24 September 2014

--D. Thiebaut (talk) 16:12, 23 September 2014 (EDT)


Problem 1: Generic Classes

from research.microsoft.com/en-us/um/people/akenn/generics/cupt.jpg


  • Store the program below in a file called Pair.java


public class Pair<T, P> {
    private T first;
    private P second;

    public Pair( T f, P s ) {
        first = f;
        second = s;
    }

    public T getFirst() { return first; }
    public P getSecond() { return second; }
    public void setFirst( T f ) { first = f; }
    public void setSecond( P s ) { second = s; }
    public String toString( ) { return "(" + first + ", " + second + ")"; }
}


  • Store the program below in a file called TestPair.java


class TestPair {
    static public void main( String[] args ) {
        Pair<Integer, Integer> p1 = null, p2 = null;

        p1 = new Pair<Integer, Integer>( 10, 3 );
        p2 = new Pair<Integer, Integer>( 5, -9 );

        System.out.println( "p1 = " + p1 );
        System.out.println( "p2 = " + p2 );
        p1.setFirst( p2.getSecond() );
        System.out.println( "p1 = " + p1 );
    }
}


Question 1
Compile and run your program:
javac TestPair.java Pair.java 
java TestPair

Verify that your program works.


Question 2
Modify the toString() method of Pair.java, so that it prints a pair this way: [10|3], rather than (10, 3). Verify that your edited program works.


Question 3
Change TestPair.java and replace Integer with Double, so that your program can hold pairs of real numbers. Update the initialization of p1 and p2, so that you store real numbers in the pairs (e.g. 3.14159). Verify that your edited program works. Notice that you do not have to modify Pair.java at all; that's the beauty of a generic class: it will work for any type of data you want it to contain.


Question 4
Change TestPair.java again, and this time modify it so that p1 and p2 will contain ( "Smith College", 1871 ), and ( "Hampshire College", 1965). Verify that your edited program works.


Question 5
Study the solution program for Exercise 2 (seen in class). Modify TestPair.java so that it now holds an array of 6 (String, Integer) pairs. Initialize the array with these university names and the year they were founded:
  • U. Bologna, 1088
  • U. Oxford, 1167
  • U. Cambridge, 1209
  • U. Salamanca, 1218
  • U. Padua, 1222
  • U. Naples, 1224
Make your program output the 6 pairs. Verify that your program works.


Problem #2: Create your Own Generic Class


  • Create a new generic class that will hold a triplet (i.e. 3 items). Call it Triplet.java and use Pair.java as an example.
  • Create a new class called TestTriplet.java that you will adapt from TestPair.java. Make your TestTriplet.java use 3-integer triplets. Verify that your edited program works.



Question 1
Modify TestTriplet.java so that it now holds two ( String, Double, Integer) triplets: p1, and p2. Verify that your edited program works.


Question 2
Modify TestTriplet.java so that now it holds ( String, Integer, Pair( Integer, Integer ) ) !!!
You shouldn't have to modify Triplet.java at all! Initialize p1 to ( "Smith College", 1871, ( 2600, 2014 ) ), and p2 to ( "Hampshire College", 1965, ( 1400, 2014 ) ). Verify that your edited program works.


Question 3
Modify TestTriplet.java and make it hold an array of 5 (Integer, Integer, Integer) triplets. Initialize the triplets as you wish. Verify that your edited program works.


Problem #3: Review Continue and Break Statements


  • This tiny section is just to make sure you remember how break and continue work in loops. They work equally well in for-loops as in while-loops.
  • Study the program below.
  • Guess its output.
  • Run the program and verify that your intuition was correct!


public class ContinueBreak {

	public static void main(String[] args) {

		for ( int i=0; i<10000; i++ ) {
			if ( i % 3 == 0 )
				continue;
			if ( i >= 50 )
				break;
			System.out.println( "i = " + i);
		}
	}

}


Problem #4: Exceptions

http://4.bp.blogspot.com/-UfVU-B94K0A/UFH6jxMoy9I/AAAAAAAAFt4/qzZuyn5_-kE/s1600/bomb.jpeg


Simple Exceptions

  • Create a new program called Bomb.java with the following code:


import java.util.Scanner;

public class Bomb {
	
	public static void main(String[] args) {
		Scanner input = new Scanner( System.in );
		
		while ( true ) {
			System.out.println( "Please enter 2 integers"
                                                    +" (-1 to stop): ");
			int n1 = input.nextInt();
                        if ( n1==-1 ) 
                              break;
			int n2 = input.nextInt();
			System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );
		}

	}

}


  • Run your program and enter some numbers. Nothing terribly exciting will happen, until you enter 0 as the second number, of course. Try it.
  • Boom!
  • Note the name of the exception that appears on your screen: java.lang.SomeNameException.
  • Edit your program so that you can now catch the exception and process it. Put the try/catch statement as close to the problematic code as possible (and replace xxxxxxxxxxxxxxx by the actual exception name).
	
		while ( true ) {
			System.out.println( "Please enter 2 integers (-1 to stop): ");
			int n1 = input.nextInt();
			int n2 = input.nextInt();
                        if ( n1==-1 || n2==-1 ) 
                              break;
			try {
				System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );
			}
			catch (  xxxxxxxxxxxxxxx  e ) {
				System.out.println("Exception: " + e.getMessage() + 
									"\nReenter numbers." );
			}
		}


  • Note that we not using a finally block here.


More Complex Exceptions


  • The ArithmeticException exception above is known to the java compiler. We refer to this type of exections as checked exceptions. The compiler knows how to deal with them. However, other exceptions (unchecked) are associated with library functions, and the compiler may not recognize them until your indicate in your program that the code for the exception should be included in the program. We illustrate this situation in this section.


  • Use the same program (Bomb.java) as above. Try inputting a string instead of an integer
  • Note that an exception occurs. Write down its name.
  • Add a try/catch statement around the input sections of your program. Make sure you declare your variables before the try/catch statement. This is because if you do not, n1 and n2 would not "exist" outside the try/catch block.


                        int n1, n2;
			try {
				n1 = input.nextInt();
				if ( n1==-1 ) break;
				n2 = input.nextInt();
			} catch ( InputMismatchException e ) {
                                // add your code here...
                        }


  • Compile your program. Observe that you have an error... The compiler is complaining that the InputMismatchException type is unknown. So we have to import it in our program:


import java.util.Scanner;
import java.util.InputMismatchException;

public class Bomb {
	
	public static void main(String[] args) {
		Scanner input = new Scanner( System.in );
		
		while ( true ) {
			System.out.println( "Please enter 2 integers (-1 to stop): ");
			int n1, n2;
			try {
				n1 = input.nextInt();
				if ( n1==-1 ) break;
				n2 = input.nextInt();
			} catch ( InputMismatchException e ) {
                                // add your code here...
			}
			
        //... some code missing...


  • Make your program skip printing n1 / n2 if the user inputs a string instead of an integer.



  • Are you getting an infinite loop? You should be getting an infinite loop. The reason is that if the user enters a string and your program executes an input.nextInt() statement, nextInt() stops on the first character that is not a number, and never removes the string from the input buffer. So when the loop starts again, the next nextInt() to run gets stuck on the same string the user entered the first time.
Solution: Add an input.nextLine() statement in your catch statement to clear the input buffer.
  • Try it. Verify that your program works.


Still More Complex Exceptions


Create a Data File

  • Use emacs to create a text file called data.txt containing a few lines with 2 numbers on each line:


2 1
10 2
100 3
-10 2
6 3


  • Make sure there are no extra blank lines in your file.


Create a New Java Program

  • Create a program called Lab5ReadFile.java with the code below:


/**
 * Lab5ReadFile.java
 * @author your-name
 */

import java.io.File;
import java.util.Scanner;

public class Lab5ReadFile {

	public static void main( String[] args ) {
		
		String fileName;
		int n1, n2;
		Scanner inFile = null;
		
                // if user forgot the enter file name on command line, remind her
		if ( args.length  == 0 ) {
			System.out.println( "Syntax: java Lab5ReadFile textFileName" );
			System.out.println( "        where textFileName is a name of a data file.");
			return;
		}
		
                // set file name to first argument on command line
		fileName = args[0];		
		inFile = new Scanner( new File( fileName ) );
		
                // read the file
		while ( inFile.hasNext() ) {
			n1 = inFile.nextInt();
			n2 = inFile.nextInt();
			System.out.println( n1 + " / " + n2 + " is " + n1/n2 );
		}
	}
}


Pass 1


  • Compile it!
  • You will get an error:
Lab5ReadFile.java:XX: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
			inFile = new Scanner( new File( fileName ) );
 
The Java compiler knows that the File class is designed to throw an exception if something goes wrong opening a file. The Java compiler is thus forcing you to put a try/catch statement around the new File(...) statement, or forcing you to add a throw statement after main(). Let's worry about throw later.


  • Add a throw/catch statement around the inFile = new Scanner( new File( fileName ) ); statement. The exception you want to catch is a FileNotFoundException, which should be imported with a import java.io.FileNotFoundException; statement.
You probably want your program to stop (i.e. return from main) in your catch block.
  • Run your program, first with an erroneous file name:
javac Lab5ReadFile.java 
java Lab5ReadFile toto.txt

Verify that your program does not crash.
  • Try again, but this time with the name of your data file:
java Lab5ReadFile data.txt

Verify that your program displays the correct numbers.


Pass 2


  • Copy your data file into a new file:
cp data.txt badData.txt

and add some eroneous integers in the new data file, something that the nextInt() function will not like (e.g. 3.145, Toto, Smith, 1E10, etc.)
  • Also make sure one of the input lines has 0 as the second number.
  • Run your program with the new data file:
java Lab5ReadFile badData.txt

and fix every exception that gets sent to the Operating System.
  • You should now be able to protect your program with try/catch statements similar to what you did with the keyboard input version.
  • Go at it! Make sure your program does not crash on any of the erroneous inputs.


Throwing Exceptions


  • Create this new program, called Lab5ReadFileThrow2.java:


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFileThrow2 {

	private static Scanner openFile( String fileName ) throws FileNotFoundException {
		Scanner inFile;
		inFile = new Scanner(new File(fileName));
		return inFile;
	}
	
	private static int[] get2Ints( Scanner inFile ) {
		int n1 = inFile.nextInt(); 
		int n2 = inFile.nextInt();
		return new int[] { n1, n2 };
	}
	
	public static void main(String[] args) {
		
		Scanner inFile = null;

		// if user forgot the enter file name on command line, remind her
		if (args.length == 0) {
			System.out.println("Syntax: java Lab5ReadFile textFileName");
			System.out
					.println("        where textFileName is a name of a data file.");
			return;
		}

		// set file name to first argument on command line
		try {
			inFile = openFile( args[0] );
		} catch (FileNotFoundException e) {
			System.err.println( "Could not find file " + args[0] );
			return;
		}

		// read the file
		while (inFile.hasNext()) {
			int[] a = get2Ints( inFile );
			System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );
		}
	}
}


  • It is slightly different from the previous version.
  • Take a few minutes to read it, and study the new method, and the use of the array int[] a. Make sure you understand the code. Otherwise ask the TA or myself for explanations.
  • Compile, then run your program with your good text file:
java  Lab5ReadFileThrow2  data.txt

  • Verify that you get a valid output.
  • Run your program with the erroneous data file:
java  Lab5ReadFileThrow2  badData.txt

and observe the exception thrown back by your program.
  • Fix the errors by making the method get2Ints() throw an InputMismatchException exception.
  • Enclose the call to the method in main by a try/catch block.
  • Run your program again on the bad data, and verify that you do not get the inputMismatchException any longer. You get another type of exception, though.
  • In Java, if a method needs to throw more than one exception, you can indicate it as follows:


 public void someMethod( someArgs ) throws A_Exception, B_Exception {
     ...
 }
You should be able to fix it!