Skip to content

Instantly share code, notes, and snippets.

@minor
Created February 29, 2020 05:21
Show Gist options
  • Save minor/e755d1a234b084db77bae6dea68f468f to your computer and use it in GitHub Desktop.
Save minor/e755d1a234b084db77bae6dea68f468f to your computer and use it in GitHub Desktop.
Professor Estrada: Part 2
package com.saurishsrivastava;
/* name: Saurish Srivastava; strings and loops - part 2: displays total number of items, average price, and standard deviation of the prices given by user */
/* import used packages */
import java.util.ArrayList;
import java.util.Scanner;
public class Statistics {
/**
* Main method
*/
public static void main(String[] args) {
/* declare variables and initialize Scanner */
Scanner input = new Scanner(System.in); // initialize the scanner
double sinput; // declare sinput double
int number_items; // declare the number of items variable
ArrayList<Double> prices = new ArrayList<Double>(); // create an ArrayList for the prices
double averageprice = 0; // declare the average price variable
double sd = 0; // declare the standard deviation variable
System.out.print("Enter first price, or -1 to quit: $"); // prompt user input
sinput = input.nextDouble(); // assign input to the variable sinput
prices.add(sinput); // add input to the array list
if (sinput != -1.0) { // if the input isn't -1 (which means program still runs)
while (sinput != -1.0) {
System.out.print("Enter next price, or -1 to quit: $");
sinput = input.nextDouble();
prices.add(sinput); // adds all values given by user into array list until -1 is entered
}
}
else {
System.out.println("Number of items: 0\n" +
"No data entered. Cannot calculate statistics.");
System.exit(0); // stops the program because no data was entered
}
prices.remove(prices.size() - 1 ); // get rid of the -1 in the prices array list
number_items = prices.size(); // assign the number of items to the amount of items in the array
for(int n = 0; n < prices.size(); n++) {
averageprice += prices.get(n); // go through all elements and add them to averageprice (so far its a sum)
}
averageprice = averageprice/prices.size(); // divide by the size of the array list for the average (sum/items)
for (int z = 0; z < prices.size(); z++)
{
sd += Math.pow((prices.get(z) - averageprice), 2); // adds all the squares of the difference of prices and means
}
sd = sd / (number_items); // divide by the number of items for the mean
sd = Math.sqrt(sd); // take the square root
sd = Math.round(sd*100.0)/100.0; // get standard deviation to two decimal places
averageprice = Math.round(averageprice*100.0)/100.0; // get average price to two decimal places
/* ~ Final Results ~ */
if (number_items == 1) { // if statement for only one item
System.out.println("Number of items: " + number_items);
System.out.println("Average price is: $" + averageprice);
System.out.println("Cannot calculate standard deviation for one item.");
} else { // for all other statements
System.out.println("Number of items: " + number_items);
System.out.println("Average price is: $" + averageprice);
System.out.println("Standard deviation of prices is $" + sd);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment