Java Primitives Notebook
Define in a Class the following data types Demonstrate use of Primitives: int, double, boolean, string Demonstrate use of Wrapper Class object: String
public class Primitives{
int anInt = 50;
double aDouble = 5.5;
boolean aBoolean = true;
String aString = "I love to code code code";
}
System.out.println(anInt);
System.out.println(aDouble);
System.out.println(aBoolean);
System.out.println(aString);
This program asks for a list of numbers. When you are finished, input 0. It will then report Standard Deviation of this set of numbers.
Note: This program was created to assist with an AP Biology Assignment where we had to calculate the Standard Deviation of a dataset.
public class STDevCalculator {
ArrayList<Double> values; //defines where values will go into
public STDevCalculator() {
this.values = new ArrayList<>();
this.enterValues();
}
private boolean isZero(double value){
double threshold = 0.001;
return value >= -threshold && value <= threshold; //tests if the input is 0
}
private void enterValues() {
Scanner input;
while (true) {
input = new Scanner(System.in);
System.out.print("Enter a Value, 0 to exit: ");
try {
double sampleInputDouble = input.nextDouble(); //asks for a double to inputted
System.out.println(sampleInputDouble);
if (isZero(sampleInputDouble)) break;
else this.values.add(sampleInputDouble);
} catch (Exception e) {
System.out.println("Not an double (form like 9.99), " + e); //has to be in double form
}
input.close();
}
}
public double stdev() {
double total = 0;
double m = 0;
double s = 0;
double sum = 0;
for (double num : this.values) { //Loops through and finds the sum of all of the values in the values array
total += num;
}
m = total / values.size();
for (double num : this.values){
sum += Math.pow((m - num), 2); //Sums up all of the values necessary in the standard deviation equation
}
s = Math.pow((sum / (this.values.size() - 1)), 0.5); //Calculates the standard deviation
return (s);
}
public static void main(String[] args) {
STDevCalculator values = new STDevCalculator();
System.out.println("Standard Deviation of dataset: " + String.format("%.2f", values.stdev()));
}
}
STDevCalculator.main(null);
Standard Deviation is the amount of variation in a dataset and is very vital in many situations regarding data analysis, making this program quite useful.