Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Amit Ranganathan
- * Period 4
- * 26 November 2014
- *
- * This lab took me about 40 minutes
- * This lab was pretty simple for me. One hard part was the conversion
- * from a long to a double. For a while, i kept getting an error saying
- * that a long was expected but instead a double was being returned. After
- * a while of debugging, i figured out what i was doing wrong, and create a new
- * double variable and type caste the long into a double. Also, findig the algorithm
- * to find the mode was tricky too. But after about 5 minutes, i figured it out
- * and started coding. Overall, this lab was fun and easy.
- *
- */
- import java.util.*;
- import java.io.*;
- public class StatisticsPRanganathanPeriod4{
- private double average;
- private long totalDifference;
- private ArrayList <Double> numbers;
- public StatisticsPRanganathanPeriod4(){
- average = 0;
- totalDifference = 0;
- numbers = new ArrayList <Double> ();
- }
- public double getAverage(){
- long addition = 0;
- double counter = 0;
- File fileInput = null;
- Scanner input = null;
- try{
- fileInput = new File("numbers.txt");
- input = new Scanner(fileInput);
- }catch(IOException e){
- System.out.println(e.getMessage());
- }
- while(input.hasNext()){
- numbers.add((double)(Integer.parseInt(input.next())));
- }
- for(int i = 0; i < numbers.size(); i++){
- addition += numbers.get(i);
- counter++;
- }
- double newAddition = (double)(addition);
- average = newAddition / counter;
- return average;
- }
- public double standardDeviation(){
- File fileInput = null;
- Scanner input = null;
- double counter = 0;
- for(int i = 0; i < numbers.size(); i++){
- totalDifference += Math.pow(numbers.get(i) - average,2);
- counter++;
- }
- double divide = (totalDifference / counter);
- double standardDev = (Math.sqrt(divide));
- return standardDev;
- }
- public double mode(){
- // loop through the array and get each individual element. Inside this loop,
- // loop through the array again to see how many times the specific element is repeated.
- // store that in a counter. if the counter for this number is greater than the counter for the
- // next element, then the current mode is the current element.
- int currentCounter = 1;
- int currentMode = 0;
- int current = 0;
- int howMany = 0;
- for(int i = 0; i < numbers.size(); i++){
- current += numbers.get(i);
- for(int j = 0; j < numbers.size(); j++){
- if(numbers.get(j) == current){
- currentCounter++;
- }
- }
- if(currentCounter > howMany){
- currentMode = current;
- currentCounter = howMany;
- }
- }
- return currentMode;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement