// Name: Anita Blake              Partner: Ted Forrester
// ID:   014916                   ID:      642708
// Sec:  Yan T 2:30               Sec:     Artemov R 1:25

// Date: Feb 10, 2000
// Project 2: Loops Ahoy!

import java.text.DecimalFormat;

// 1. Compute miles per gallon of a trip.  Assume no input errors.
// Input: gas used, start odometer reading, end odometer reading
public class Project2_1 {
    public static void main(String[] args) {

	double gas;		// gallons of gas used
	int start_mile;		// beginning odometer reading in mile
	int end_mile;		// end of trip odometer reading in mile

	// Initialize Text object in to read from standard input.
	    TokenReader in = new TokenReader(System.in);
	System.out.println("1.  Miles per gallon");
	System.out.println("Enter gallons of gas used and odometer readings at");
	System.out.println("start and end of trip rounded to the nearest mile");
	gas = in.readDouble();
	start_mile = in.readInt();	
	end_mile = in.readInt();	

	System.out.println("Gas economy for this trip is " + 
		(end_mile-start_mile)/gas + " miles per gallon.");
    }
}

// 2. Count multiples of 5 in an input sequence.
public class Project2_2 {
    public static void main(String[] args) {

	// Initialize Text object in to read from standard input.
	    TokenReader in = new TokenReader(System.in);
	System.out.println("2.  Count multiples of 5 in an input sequence");
	System.out.println("Enter a sequence of positive integers " +
		"terminated by a non-positive integer.");

	int count=0;		     // no. of multiples of 5 found so far
	int input = in.readInt();    // item in input sequence to be processed
	while (input > 0) {
	    if (input%5 == 0) count=count+1;
	    input = in.readInt();
	}
	System.out.println(count +" multiple(s) of 5 found " +
		"before stopping signal");
    }
}
	    
// 3. Count number of sign changes in an input sequence.
public class Project2_3 {
    public static void main(String[] args) {

	// Initialize Text object in to read from standard input.
	    TokenReader in = new TokenReader(System.in);

	int LIMIT = 100;             // input range is -LIMIT to +LIMIT

	System.out.println("3.  Count number of sign changes in input sequence");
	System.out.println("Enter integers between -" + LIMIT + " and +" + 
		LIMIT + ".  End sequence with out-of-range number.");

	int change = 0;		     // number of sign changes found so far
	int current = in.readInt();  // current input value
	int previous = current;	     // previous input value, initialize to be 
	                             // same sign as 1st input value
	while ( Math.abs(current) <= LIMIT ) {
	    // Compare previous and current.  There is a sign change if product
	    // is negative, or if product is zero AND not both are zeros.
		if ( previous*current<=0 && !(previous==0 && current==0) )
		    change = change+1;
            /* Another solution: the test could be that both numbers
               are negative, both 0, or both positive:
                
		if ( previous <  0 && current <  0
                  || previous == 0 && current == 0
                  || previous >  0 && current >  0
                )
		    change = change+1;
            */
	    // Update previous and read next input
		previous = current;
		current = in.readInt();
	}
	System.out.println(change + " sign change(s) found before " +
		"stopping signal");
    }
}

// 4. Find two neighboring elevations with the steepest gradient.
public class Project2_4 {
    public static void main(String[] args) {

	int current;		// current contour value
	int previous;		// previous contour value
	int gradient;		// gradient between current, previous contours
	int maxGradient;	// steepest gradient found so far
	int elevationL;		// left neighbor of steepest gradient so far
	int elevationR;		// right neighbor of steepest gradient so far
	int stopValue=-1;       // stopping value to indicate end of input

	// Initialize Text object in to read from standard input.
	    TokenReader in = new TokenReader(System.in);

	System.out.println("4.  Neighboring elevations with steepest gradient");
	System.out.println("Enter contour readings terminated by -1");

	maxGradient = 0;	// initialize to smallest possible value
	elevationL = stopValue; // initialize to stopValue
	elevationR = stopValue; // initialize to stopValue
	current = in.readInt();	// read first input value
	previous = current;	// initialize to get bogus gradient of 0
	while ( current != stopValue ) {
	    // If computed gradient greater than maxGradient,
	    // update maxGradient, elevationL, elevationR
		gradient = Math.abs(previous-current);
		if (gradient>maxGradient) {
		    maxGradient = gradient;
		    elevationL = previous;
		    elevationR = current;
		}
	    // Update previous and read next input
		previous = current;
		current = in.readInt();
	}
	if (elevationL == stopValue)
	    System.out.println("Insufficient number of samples to have " + 
		"a maximum gradient");
	else
	    System.out.println("Steepest gradient occurs between elevations "
		+ elevationL + " and " + elevationR);
    }
}

===============================================================================

1.  Miles per gallon
Enter gallons of gas used and odometer readings at
start and end of trip rounded to the nearest mile
5 13 140
Gas economy for this trip is 25.4 miles per gallon.


2.  Count multiples of 5 in an input sequence
Enter a sequence of positive integers terminated by a non-positive integer.
5 21 1 21 25 32 -1
2 multiple(s) of 5 found before stopping signal

2.  Count multiples of 5 in an input sequence
Enter a sequence of positive integers terminated by a non-positive integer.
0 5 -1
0 multiple(s) of 5 found before stopping signal

2.  Count multiples of 5 in an input sequence
Enter a sequence of positive integers terminated by a non-positive integer.
150 -1
1 multiple(s) of 5 found before stopping signal


3.  Count number of sign changes in input sequence
Enter integers between -100 and 100.  End sequence with out-of-range number.
-99 -97 12 -10 1 -2 101
4 sign change(s) found before stopping signal

3.  Count number of sign changes in input sequence
Enter integers between -100 and 100.  End sequence with out-of-range number.
1 -1 0 1 -1 101
4 sign change(s) found before stopping signal

3.  Count number of sign changes in input sequence
Enter integers between -100 and 100.  End sequence with out-of-range number.
1 -101
0 sign change(s) found before stopping signal

3.  Count number of sign changes in input sequence
Enter integers between -100 and 100.  End sequence with out-of-range number.
2000
0 sign change(s) found before stopping signal


4.  Neighboring elevations with steepest gradient
Enter contour readings terminated by -1
52 63 63 95 100 -1
Steepest gradient 32 is between elevations 63 and 95

4.  Neighboring elevations with steepest gradient
Enter contour readings terminated by -1
0 -1
Insufficient number of samples to have a maximum gradient