CSC212 Homework 3 Solutions 2014

From dftwiki3
Revision as of 17:32, 27 September 2014 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Problem #1= <br /> <source lang="java"> import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scann...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 18:32, 27 September 2014 (EDT)


Problem #1


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


public class Hw3_1 {

	private static Scanner openFile( String fileName ) throws FileNotFoundException {
		Scanner inFile;
		//inFile = new Scanner(Hw3.class.getResourceAsStream(fileName) );
		inFile = new Scanner( new File(fileName) );
		return inFile;
	}

	private static double[] getNums( Scanner inFile ) {
		double year;
		double rain, sun;
		
		//--- try to get the year, and if exception return a null array ---
		try {
			year = inFile.nextDouble();
		}
		catch (InputMismatchException e ) {
			inFile.nextLine();
			//System.out.println( "wrong year format..." );
			return null;
		}
		
		//--- skip 4 doubles ---
		for ( double i=0; i<4; i++ ) 
			try {
				inFile.nextDouble();
			}
			catch ( InputMismatchException e ) { 
				inFile.nextLine();
				return null;
			}
		
		// get rain data
		rain = 0;
		sun = 0;
		try {
			rain = inFile.nextDouble();
		}
		catch ( InputMismatchException e ) {
			rain = 0; // since invalid number, we set it to 0
			sun  = 0;
			inFile.nextLine();
			return new double[] { year, rain, 0 };
		}
		try {
			sun = inFile.nextDouble();
		}
		catch ( InputMismatchException e ) {
			sun  = 0;
			inFile.nextLine();
		}
		
		return new double[] { year, rain, sun };
	}
	
	public static void main(String[] args) {
		Scanner inFile;
		
		//--- display syntax if user does not specify input file ---
		if ( args.length == 0 ) {
			System.out.println( "Please specify a file name on the command line.");
			return;
		}
				
		//--- try to open the file. ---
		try {
			inFile = openFile( args[0] );
		} catch (FileNotFoundException e) {
			System.out.println( "Cannot open " + args[0] );
			return;
		} 

		//--- read the file ---
		double maxRain = 0, maxSun = 0;
		double maxRainYear = 0, maxSunYear = 0;
		for ( double i=0; inFile.hasNext(); i++ ) {
			//System.out.println( i );
			double[] nums = getNums( inFile );
			if ( nums != null ) {
				//System.out.println(  nums[0] + " " + nums[1] + " " + nums[2] );
				if ( nums[1] > maxRain ) { maxRain = nums[1]; maxRainYear = nums[0]; }
				if ( nums[2] > maxSun ) { maxSun = nums[2]; maxSunYear = nums[0]; }
			}
		}
		System.out.println( String.format( "rain %1.0f %1.2f", maxRainYear, maxRain ) );
		System.out.println( String.format( "sun  %1.0f %1.2f", maxSunYear, maxSun ) );
		
	}

}


Problem #2


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


public class Hw3_2 {

	private static Scanner openFile( String fileName ) throws FileNotFoundException {
		Scanner inFile;
		//inFile = new Scanner(Hw3.class.getResourceAsStream(fileName) );
		inFile = new Scanner( new File(fileName) );
		return inFile;
	}

	private static double[] getNums( Scanner inFile ) {
		double year;
		double rain, sun;
		
		//--- try to get the year, and if exception return a null array ---
		try {
			year = inFile.nextDouble();
		}
		catch (InputMismatchException e ) {
			inFile.nextLine();
			//System.out.println( "wrong year format..." );
			return null;
		}
		
		//--- skip 4 doubles ---
		for ( double i=0; i<4; i++ ) 
			try {
				inFile.nextDouble();
			}
			catch ( InputMismatchException e ) { 
				inFile.nextLine();
				return null;
			}
		
		// get rain data
		rain = 0;
		sun = 0;
		try {
			rain = inFile.nextDouble();
		}
		catch ( InputMismatchException e ) {
			rain = 0; // since invalid number, we set it to 0
			sun  = 0;
			inFile.nextLine();
			return new double[] { year, rain, 0 };
		}
		try {
			sun = inFile.nextDouble();
		}
		catch ( InputMismatchException e ) {
			sun  = 0;
			inFile.nextLine();
		}
		
		return new double[] { year, rain, sun };
	}
	
	public static void main(String[] args) {
		Scanner inFile;
		
		//--- display syntax if user does not specify input file ---
		if ( args.length == 0 ) {
			System.out.println( "Please specify a file name on the command line.");
			return;
		}
				
		//--- try to open the file. ---
		try {
			inFile = openFile( args[0] );
		} catch (FileNotFoundException e) {
			System.out.println( "Cannot open " + args[0] );
			return;
		} 

		//--- read the file ---
		double maxRain = 0, maxSun = 0;
		double maxRainYear = 0, maxSunYear = 0;
		for ( double i=0; inFile.hasNext(); i++ ) {
			//System.out.println( i );
			double[] nums = getNums( inFile );
			if ( nums != null ) {
				//System.out.println(  nums[0] + " " + nums[1] + " " + nums[2] );
				if ( nums[1] > maxRain ) { maxRain = nums[1]; maxRainYear = nums[0]; }
				if ( nums[2] > maxSun ) { maxSun = nums[2]; maxSunYear = nums[0]; }
			}
		}
		System.out.println( String.format( "rain %1.0f %1.2f", maxRainYear, maxRain ) );
		System.out.println( String.format( "sun  %1.0f %1.2f", maxSunYear, maxSun ) );
		
	}

}


Problem #3


import java.util.Vector;


public class Hw3_3 {

    public static void main(String[] args) {
        // --- Test List ---
        Vector dwarves = new Vector();

        dwarves.add( (String) "Blick");
        dwarves.add( (String) "Flick");
        dwarves.add( (String) "Glick");
        dwarves.add( (String) "Plick");
        dwarves.add( (String) "Quee");
        dwarves.add( (String) "Snick");
        dwarves.add( (String) "Whick");

        System.out.println("Snow White's friends:");
        for (int i = 0; i < dwarves.size(); i++)
            System.out.println( dwarves.get( i ) );

        String dwarf;
        try { 
        	dwarf = (String) dwarves.get(-1);
            System.out.println(dwarf);
        } catch ( ArrayIndexOutOfBoundsException e ) {
        	System.out.println( "invalid index" );
        }
        
        try { 
        	dwarf = (String) dwarves.get(20);
            System.out.println(dwarf);
        } catch ( ArrayIndexOutOfBoundsException e ) {
        	System.out.println( "invalid index" );
        }
         

        int n = dwarves.indexOf( "Quee" );
        if ( n != -1 ) 
            System.out.println( "Quee found at Index: " + n );
        else
            System.out.println( "Quee not found" );

        n = dwarves.lastIndexOf( "Quee" );
        if ( n != -1 ) 
            System.out.println( "Quee found at Index: " + n );
        else
            System.out.println( "Quee not found" );

        n = dwarves.indexOf( "Queen" );
        if ( n != -1 ) 
            System.out.println( "Queen found at Index: " + n );
        else
            System.out.println( "Queen not found" );

    }
}