Lab 3

Lab Description:

For this lab, I had to use Eclipse to create a script to compute statistics from lab 2, but use method overload to choose the appropriate data type to use. The program is used to find the one way trip cost where input would be in the form of (double) and one way time time in minutes where the input would be in the form of integer). I was not sure how to read an input and see if it is a double or int because when you take an input from a user, you have a specify the type of input.

Code:

import java.util.Scanner;

class Overload {
	void first(int first) {}
	void first(double first) {}
}

public class TrainStats
{   
    static Scanner input = new Scanner (System.in);
    public static void main (String[] args){

    	double last = 0;
    	double max;
    	double min;
    	double a;
    	double b;
    	double c;
    	double sum;
    	double sum1 = 0;
    	int total = 0;

    	System.out.println("Enter Numbers (0 to finish)");
    	double first = input.nextDouble();

    	Overload overload = new Overload();
        overload.first(first);

     	min = first;
    	max = first;

	do {
		a = input.nextDouble();   
		b = a;
		c = a;
		if (a!=0){
			if(b >= max)
				max = b;
			if(c <= min)
				min = c;
		}
		if (a!=0)
			last = a;
  	    sum1 = sum1 + a;
  	    sum = sum1 + first;
  	    total++;;
  	} while (a!=0);
	
	if(first == Math.floor(first))
	{System.out.println("One way trip cost: ");}
	else
	{System.out.println("One way trip time in minutes: ");}
	
    System.out.println("First Number Entered: " + first);
    System.out.println("Last Number Entered: " + last);
    System.out.println("Total Numbers Entered: " + total);
    System.out.println("Maximum: " + max);
    System.out.println("Minimum: " + min);
    System.out.println("Total Sum: " + sum);
    System.out.println("Average: " + sum / total);
    }
}

Screenshots: