-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatistics.java
More file actions
123 lines (94 loc) · 4.02 KB
/
Statistics.java
File metadata and controls
123 lines (94 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Sukhamrit Singh
// Statistics
/*
This is a program that repeatedly asks a user
to enter a priceof an item or -1 when finished.
When the user enters -1, your program will compute
and display the total number of items, the average
price, and the standard deviation of the prices.
If the user enters -1 right away, print a message
that you cannot calculate any statistics. If the
user enters only one number, calculate the average,
but give a message that you cannot calculate a
standard deviation
*/
// Imports the necessary libraries
import java.util.LinkedList;
import java.util.Scanner;
public class Statistics {
public static void main(String[] args) {
// Loop to keep gathering data
while (true) {
// Defines variable for number of items
int itemCount = 0;
// Variable for total price
double totalPrice = 0;
// loop to keep going if not -1
boolean loop = true;
// average of items
double average = 0;
// Variable for standard deviation
double standardDeviation;
// Lined list to store umber of items
LinkedList<Double> prices = new LinkedList<Double>();
// Scanner for users input
Scanner input = new Scanner(System.in);
// Loop that keeps running until -1
while (loop) {
// If statement for number of items
if (itemCount == 0) {
System.out.print("Enter first price, or -1 to quit: $");
} else {
System.out.print("Enter next price, or -1 to quit: $");
}
// Scanner item for user input
double price = input.nextDouble();
// If statement for if user enters -1
if (price == -1) {
// Displays an empty line
System.out.println("");
// If statement to display for number of items
if (itemCount == 0) {
System.out.println("Number of items: 0");
System.out.println("No data entered. Cannot calculate statistics.");
} else if (itemCount == 1) {
average = totalPrice / itemCount;
System.out.println("Number of items: 1");
System.out.printf("Average price: $%.2f \n", average);
System.out.println("Cannot calculate standard deviation for one item");
} else {
average = totalPrice / itemCount;
System.out.println("Number of items: " + itemCount);
System.out.printf("Average price: $%.2f", average);
}
// Turns the loop off, so ends cycle if -1
loop = false;
} else {
// If not -1, does these operations
itemCount++;
totalPrice += price;
prices.add(price);
}
}
// Prints empty line
System.out.println("");
// If statement is number of items is 1
if (itemCount > 1) {
// Defines a variable standard sum
double standardSum = 0;
// For loop to get standard sum for standard deviation
for (int i = 0; i < prices.size(); i++) {
double p = prices.get(i);
standardSum += Math.pow(p - average, 2);
}
// Defining standard deviation
standardDeviation = Math.sqrt(standardSum / (itemCount - 1));
// Prints the standard deviation
System.out.printf("Standard deviation of prices is $%.2f", standardDeviation);
}
// Leaves two lines empty
System.out.println("");
System.out.println("");
}
}
}