SnowMelt.java


Below is the syntax highlighted version of SnowMelt.java.


/****************************************************************************
 * Name: Donna Gabai
 * NetID: dgabai
 * Precept: P99 
 * Spring14 Exam 1
 * 
 * Description: Simulates how fast the snow will melt 
 *              based on how much salt is used and temp
 * Dependencies: StdIn, StdOut
 * ***************************************************************************/

public class SnowMelt {
    
    // using current snow, temperature and salt solution
    // how much snow will melt?
    public static double meltage(double currSnow, int temp, int salt) {
        double result = Math.pow(1 + currSnow, (temp - 32 + 2*salt)/18.0);
        result = Math.min(currSnow, result);
        return result;
    }
    
    // formatted printing of a double[] array
    public static void printArray(double[] arr) {
        int N = arr.length;
        for (int i = 0; i < N; i++)
            StdOut.printf("%8.3f", arr[i]);

        // new line after all entries printed
        StdOut.println();
    }
    
    // Part 2B: input data file from standard input
    // input salt solution ints from command-line
    // output remaining snow for each day for each salt solution
    public static void main(String[] args) {
        // store salt for scenarios
        int N = args.length;
        int[] salt = new int[N];
        for (int i = 0; i < N; i++)
            salt[i] = Integer.parseInt(args[i]);
            
        // array of snow left on ground for each scenario
        double[] snowLeft = new double[N];
        
        // input snow in inches and temp in degrees F for each day
        while (!StdIn.isEmpty()) {
            // read data from standard input
            double newSnow = StdIn.readDouble();
            int temp = StdIn.readInt();
            
            // compute meltage for one day with each salt value
            for (int i = 0; i < N; i++) {
                snowLeft[i] = snowLeft[i] + newSnow;
                double melt = meltage(snowLeft[i], temp, salt[i]);
                // how much snow left after the melting?
                snowLeft[i] = snowLeft[i] - melt;
            }
            printArray(snowLeft);
        }
    }
}