Program ignoring the last row in the file in calculation - java

I have a text file with data that looks like this (TestData.txt):
Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
Richard Perez|100|84|73|81|92|84|95|96|95|100
Ivan Dyer|77|91|90|75|97|94|76|89|90|92
Craig Palmer|91|84|98|89|82|75|78|96|100|97
Madeline Rogers|75|79|78|93|91|76|80|88|100|81
Chelsea Roxas|87|94|89|96|95|85|88|92|86|86
Jasper Bautista|100|83|93|100|98|97|96|97|97|98
Tyler Perez|82|89|90|78|89|96|75|88|90|96
My code parses the file and does some calculations with it.
However, in the method arrangeList() within which calls another method called getTestAvg() (calculates column means), the program ignores Tyler Perez's scores.
I noticed that the results I am getting were inaccurate so I went and printed the whole 2d array with all the test scores and the last column is nowhere to be found.
My entire code is below and I hope someone could point out what causes this.
I keep getting an IndexOutOfBounds error whenever I try to switch N (# of students) and M (# of tests) to see what happens. At first, I have 10 students and 10 tests, and all the calculations were correct, but when I added another student, the calculations became inaccurate.
I apologize in advance if my code isn't as well-designed as I'm not an experienced programmer.
import java.util.*;
import java.io.*;
public class TestAverages
{
private static int[] grades;
private static int[] testTotal;
private static int N;
private static double classTotal;
private static int M;
public static void main(String[] args) throws FileNotFoundException
{
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException
{
TestAverages t = new TestAverages();
in.nextLine();
double total = 0.0;
ArrayList<Double> testScores = new ArrayList<Double>();
int index = 0;
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("\\|");
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i < N; i++){
grades[i] = Integer.parseInt(data[i + 1]);
testScores.add((double)grades[i]);
}
System.out.println(name + "\t");
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
total += t.getStudentAvg(grades);
M++;
}
t.arrangeList(testScores);
System.out.printf("\nClass Average: %.1f%%\n", t.getClassAvg(total));
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i < N; i++){
total += grades[i];}
avg = total / N;
return avg;
}
public double getClassAvg(double total)
{
double classAvg = total / M;
return classAvg;
}
public double[][] arrangeList(ArrayList testScores)
{
double[][] tests = new double[N][N];
int len = tests.length;
for(int i = 0; i < len; i++)
{
for(int j = 0; j < len; j++)
{
tests[i][j] = (Double) testScores.get(i*N + j);
}
}
for(int i = 0; i < len; i++)
{
double avg = getTestAvg(tests, i);
System.out.printf("\nTest " + (i + 1) + " Average: %.1f%%\n", avg);
}
return tests;
}
public double getTestAvg(double[][] testScores, int index)
{
double testAvg = 0.0;
for(int i = 0; i < N; i++)
{
testAvg += testScores[i][index];
}
return testAvg / N;
}
}
Here are the numbers I'm supposed to be getting (top) compared to what my program outputs (bottom).

As the other responses already stated, you had quite the issue with your variables and loops. I now changed N to # of students and M to # of tests to be as you stated in your question.
Next time, maybe try to improve your variable naming, so you don't get confused. (e.g. switch out n and m for s (students) and t (tests), if you like your variable names short).
This should work now. Just check against your code to see the changes.
import java.util.*;
import java.io.*;
public class TestAverages {
private static int[] grades;
private static int n = 0; // amount of students
private static int m; // amount of tests
public static void main(String[] args) throws FileNotFoundException {
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException {
TestAverages t = new TestAverages();
in.nextLine();
double total = 0.0;
ArrayList<Double> testScores = new ArrayList<Double>();
while (in.hasNextLine()) {
String line = in.nextLine();
String[] data = line.split("\\|");
String name = data[0];
grades = new int[data.length - 1];
m = grades.length;
for (int i = 0; i < m; i++) {
grades[i] = Integer.parseInt(data[i + 1]);
testScores.add((double) grades[i]);
}
System.out.println(name + "\t");
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
total += t.getStudentAvg(grades);
n++;
}
t.arrangeList(testScores);
System.out.printf("\nClass Average: %.1f%%\n", t.getClassAvg(total));
}
public double getStudentAvg(int[] grades) {
double total = 0.0;
double avg = 0.0;
for (int i = 0; i < grades.length; i++) {
total += grades[i];
}
avg = total / grades.length;
return avg;
}
public double getClassAvg(double total) {
double classAvg = total / n;
return classAvg;
}
public double[][] arrangeList(ArrayList<Double> testScores) {
double[][] tests = new double[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tests[i][j] = (Double) testScores.get(i * m + j);
}
}
for (int i = 0; i < m; i++) {
double avg = getTestAvg(tests, i);
System.out.printf("\nTest " + (i + 1) + " Average: %.1f%%\n", avg);
}
return tests;
}
public double getTestAvg(double[][] testScores, int index) {
double testAvg = 0.0;
for (int i = 0; i < n; i++) {
testAvg += testScores[i][index];
}
return testAvg / n;
}
}

You need to account for the different sizes. I think you want primarily the number of TESTS (not students), but you can't just use len for both index bounds.
double[][] tests = new double[N][M];
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
tests[i][j] = (Double) testScores.get(i*N + j);
}
}
Note that it didn't just resize the array, but it changed the loop conditions to loop the proper amount for inner and outer loop.

In the line
double[][] tests = new double[N][N];
of function arrangeList
you make your test array as N X N.
I believe youh should do something like
double[][] tests = new double[M][N];
It's just a suggestion as in your code it seems M = number of students and N = number of tests, differently from what you write in your question.
In general you should review all the method arrangeList and probably getTestAvg too (loop on N, instead of M), as the loops on variable len are intended for a N X N array, which is not the case.

Related

How to separate my code into different methods

So I have an assignment where i'm supposed to create a program that generates 100 random integers and stores them in a single dimensional array. I am then supposed to calculate the average, standard deviation and variance. Through a lot of trial and error I was able to make a program that did this but what I missed was that it said to write a separate method for each property(average, standard deviation and variance). How would i go about doing that?
package homeassignment5;
public class HomeAssignment5 {
public static void main(String[] args) {
int n;
n = 100;
int[] random = new int [n];
for (int i = 0; i<random.length; i++)
random[i] = (int) (Math.random()* n);
double total = 0;
double average = 0;
double variance = 0;
double var = 0;
double sd = 0;
for (int element : random){
total += element;
average = total/n;
}
for (int i = 0; i<random.length; i++){
variance += (random[i] - average) * (random[i] - average);
}
var = variance / random.length;
sd = Math.sqrt(var);
//System.out.println("Total is: " + total);
System.out.println("Average is: " + average);
System.out.println("Variance is " + var);
System.out.println("Standard deviation is " + sd);
/*for (int i = 0; i<random.length; i++)
System.out.println(random[i]);
*/
}
}
Might be a bit of a mess but i'm really new to java and this was the product of a ton of trial and error.
So there are several ways of going about this, but the easiest would probably be this:
public class HomeAssignment5 {
private static int[] random;
public static void main(String[] args){
random = = new int[100];
...
}
public static void getAverage(){
// void if you want to print it here, double if you want to print in main
double average = 0;
int total = 0;
for(int i = 0; i < 100; i += 1)
total += random[i];
average = total / n; // you'll want to do this outside of the loop to
// to get the correct result!
System.out.println("Average: " + average);
}
...
}
You could alternatively pass the array to each method as a parameter.
By the way, in what you posted here, you still haven't actually written any random numbers into the array, so remember to do that, it's currently still all zero.
I believe this should help you..
All the functionalities are separated in form of methods,
the initialization is separated.
** WELCOME TO REFACTORING **
public class Refactor {
private int[] random;
private int n;
private double total = 0;
private double average = 0;
private double variance = 0;
private double sd = 0;
public Refactor(int n) {
this.n = n;
this.populateArray();
}
private void populateArray() {
random = new int[n];
for (int i = 0; i < random.length; i++)
random[i] = (int) (Math.random() * n);
}
public double getAverage() {
average = 0;
total = 0;
for (int element : random) {
total += element;
}
average = total / n;
return average;
}
public double getVariance() {
variance = 0;
for (int i = 0; i < random.length; i++) {
variance += (random[i] - average) * (random[i] - average);
}
variance = variance / random.length;
return variance;
}
public double getSD() {
this.getVariance();
sd = 0;
sd = Math.sqrt(variance);
return sd;
}
public static void main(String[] args) {
Refactor ref = new Refactor(100);
System.out.println("Average is: " + ref.getAverage());
System.out.println("Variance is " + ref.getVariance());
System.out.println("Standard deviation is " + ref.getSD());
}
}
How would i go about passing the array to each method?
The first and most straightforward of the three methods is average:
static double average(int [] r)
{ double total = 0;
for (int e: r) total += e;
return total/r.length;
}
For the second method, the standard deviation, I recommend not to pass the array, but rather the variance, since if the program has calculated the variance anyway, it need not be recalculated, and if it doesn't have, the SD method can be called with a call to the variance method as its argument.
static double SD(double variance) { return Math.sqrt(variance); }
For the third method, the variance, I recommend not only to pass the array, but also its average, with similar reasoning as above.
static double variance(int [] r, double average)
{ double var = 0;
for (int e: r) var += (e - average) * (e - average);
return var/r.length;
}
With those three methods defined, you can now replace
double total = 0;
double average = 0;
double variance = 0;
double var = 0;
double sd = 0;
for (int element : random){
total += element;
average = total/n;
}
for (int i = 0; i<random.length; i++){
variance += (random[i] - average) * (random[i] - average);
}
var = variance / random.length;
sd = Math.sqrt(var);
with
double average = average(random);
double var = variance(random, average);
double sd = SD(var);

java, counting Standard Deviation. stuck in my last code

public int deviasi(){
//sum
int jumlah=0;
for (int i=0; i<banyak; i++){
jumlah = jumlah+nilai[i];
}
//mean
int rata2;
rata2=jumlah/banyak;
//menghitung deviasi
double deviasi = 0;
for (int i=0;i<banyak;i++){
deviasi += Math.pow(nilai[i] - rata2,2);
}
return
Math.sqrt (deviasi/banyak);
}
i got error in my last code. it told me the problem because of different data type between deviasi and banyak. but, i've changed the data type to make them have the same data type. but the error warning still told me the problem because of different data type between deviasi and banyak. i got stuck. T_T
Like mentioned in comments you need to work with double as variable type
public class StandDev {
public double standardDevioation(int[] numbers){
int count = numbers.length;
double sum = 0.0;
for (int i = 0; i < count; i++){
sum += numbers[i];
}
double mean = sum / count;
double sumDiff = 0.0;
for (int i = 0; i< count; i++){
sumDiff += Math.pow(numbers[i] - mean,2);
}
return Math.sqrt (sumDiff/count);
}
public static void main(String[] args) {
int[] numbers = {-5, 1, 8, 7, 2};
StandDev standDev = new StandDev();
System.out.println(standDev.standardDevioation(numbers));
}
}
// Code returns 3.7549966711037173
public class Devs {
public static double deviasi(){
//sum
int jumlah= 0;
int[] nilai = {4,7,6,3,9,12,11,12,12,15};
int banyak = 10;
for (int i=0; i<banyak; i++){
jumlah += nilai[i];
}
//mean
int rata2;
rata2=jumlah/banyak;
//menghitung deviasi
double deviasi = 0;
for (int i=0;i<banyak;i++){
deviasi += Math.pow(nilai[i] - rata2,2);
}
return Math.sqrt (deviasi/banyak);
}
public static void main(String args[]) {
System.out.println(deviasi());
}
}

Use boolean to search for outliers of standard dev and then print the outliers using java

What I'm trying to do is figure out a way to print the outliers of standard deviation here. The outliers are defined as having a variance greater than 2x the standard deviation. I can't figure out how but I've started by creating a boolean flag, however I don't understand the dynamics of this. Could someone please help me figure out how to print out the outliers somehow? Thanks.
public class Main {
public static void main(String[] args)
{
{
Algebra n = new Algebra();
System.out.println(" ");
System.out.println("The maximum number is: " + n.max);
System.out.println("The minimum is: " + n.min);
System.out.println("The mean is: " + n.avg);
System.out.println("The standard deviation is " + n.stdev);
}
}
}
2nd part:
public class Algebra
{
static int[] n = createArray();
int max = displayMaximum(n);
int min = displayMinimum(n);
double avg = displayAverage(n);
double stdev = displayStdDev(n);
public boolean outliers() {
for(int i = 0; i < n.length; i++)
{
boolean flag = (n[i] < stdev*2);
}
return
}
public Algebra()
{
this(n);
System.out.println("The numbers that are outliers are ");
for(int i = 0; i < n.length; i++)
{
System.out.print(" " + (n[i] < stdev*2));
}
}
public Algebra(int[] n)
{
createArray();
}
public static int[] createArray()
{
int[] n = new int[100];
for(int i = 0; i < n.length; i++)
n[i] = (int)(Math.random()*100 + 1);
return n;
}
public int displayMaximum(int[] n)
{
int maxValue = n[0];
for(int i=1; i < n.length; i++){
if(n[i] > maxValue){
maxValue = n[i];
}
}
return maxValue;
}
public int displayMinimum(int[] n)
{
int minValue = n[0];
for(int i=1;i<n.length;i++){
if(n[i] < minValue){
minValue = n[i];
}
}
return minValue;
}
protected double displayAverage(int[] n)
{
int sum = 0;
double mean = 0;
for (int i = 0; i < n.length; i++) {
sum += n[i];
mean = sum / n.length;
}
return mean;
}
protected double displayStdDev(int[] n)
{
int sum = 0;
double mean = 0;
for (int i = 0; i < n.length; i++) {
sum = sum + n[i];
mean = sum/ n.length;
}
double squareSum = 0.0;
for (int i = 0; i < n.length; i++)
{
squareSum += Math.pow(n[i] - mean, 2);
}
return Math.sqrt((squareSum) / (n.length - 1));
}
}
Variance is defined as the squared difference from the mean. This is a fairly straight forward calculation.
public static double variance(double val, double mean) {
return Math.pow(val - mean, 2);
}
You define an outlier as an instance that has a variance greater than x2 the standard deviation.
public static boolean isOutlier(double val, double mean, double std) {
return variance(val, mean) > 2*std;
}
You then just need to iterate through the values and print any values that are evaluated as an outlier.
public void printOutliers() {
for (int i : n) {
if (isOutlier(i, avg, stdev)) {
...
}
}
}
You should note that if one value is defined as an outlier and subsequently removed, values previously classified as an outlier may no longer be. You may also be interested in the extent of an outlier in the current set; One value may be an outlier to a greater extent than another.

Incompatible types: double[][] can not be converted to double

I am trying to resolve an issue for this assignment I was given for Homework. I am currently stuck and would appreciate any help that could guide me in correcting the program.
The original assignment is as follows:
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low.
This is the code I have assembled so far and have an error that I am not able to resolve. It is " incompatible types: converting double[][] cannot be converted to double. The lines in question are Line 8, and Line 110 ( the last return in the program).
import java.util.*;
public class Weather
{
public static void main(String[] args)
{
double[][] tempData = getData();
printTempData(tempData);
double avgHigh = averageHigh(tempData);
double avgLow = averageLow(tempData);
int indexHigh = indexHighTemp(tempData);
int indexLow= indexLowTemp(tempData);
System.out.format("The average high temperature is %4.1f%n", avgHigh);
System.out.format("The average low temperature is %4.1f%n", avgLow);
System.out.format("The index of high temperature is %2d%n", indexHigh);
System.out.format("The index of low temperature is %2d%n", indexLow);
}
private static void printTempData(double[][] tempData)
{
System.out.format("%6s:%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%4s%n","Month","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
System.out.format("%6s:","Low");
for (int i = 0; i < tempData[0].length;i++)
{
System.out.format("%4.1s", tempData[0][i]);
}
System.out.format("%n");
System.out.format("%6s: ","High");
for (int i = 0; i < tempData[1].length; i++)
{
System.out.format("%4.1f", tempData[1][i]);
}
System.out.format("%n");
}
private static int indexLowTemp(double[][] tempData)
{
int index = 0;
double temp = tempData[0][0];
for (int i = 0; i < tempData[0].length; i++)
{
if (temp > tempData[0][i])
{
temp = tempData[0][i];
index = i;
}
}
return index +1;
}
private static int indexHighTemp(double[][] tempData)
{
int index = 0;
double temp = tempData[1][0];
for(int i = 0; i< tempData[1].length; i++)
{
if ( temp < tempData[1][i])
{
temp = tempData[1][i];
index = i;
}
}
return index + 1;
}
private static double averageHigh(double[][] tempData)
{
double avg = 0.0;
for(int i=0; i < tempData[0].length; i++)
{
avg += tempData[0][i];
}
avg /= tempData[0].length;
return avg;
}
private static double averageLow(double[][] tempData)
{
double avg = 0.0;
for(int i=0; i > tempData[1].length; i++)
{
avg += tempData[0][i];
}
avg /= tempData[0].length;
return avg;
}
private static double getData()
{
double[][] tempData = new double[2][12];
Random r = new Random();
for (int j = 0; j < tempData[0].length; j++)
{
tempData[0][j] = 30 + Math.sqrt(j) - r.nextDouble();
tempData[1][j] = 30 + Math.sqrt(j) + r.nextDouble();
}
return tempData;
}
}
Your method private static double getData() should be private static double[][] getData()
You already declared an array
double[][] tempData = getData();
but you are trying to call
private static double getData()
thus the error "converting double[][] cannot be converted to double."
Hence change to
private static double[][] getData()
The method should be private static double[][] getData()

Calling a void Method into a constructor

I have a problem that goes like this. A user inputs 5 grades into an array.
The array must then be sorted from highest to lowest and then averaged.
the methods for the selectionSort and calculateMean must be void.
How do i call them into my constructor? then i need to print the data out to a toString.
This is what i have so far.
import java.util.Scanner;
public class Average {
//the array which will contain the scores
private int data[];
//the average of the scores
private double mean;
public Average(){
Scanner sc = new Scanner(System.in);
double data[] = new double[6];
for(int i = 1; i < data.length; i++){
System.out.println("Enter score number " + i);
data[i] = sc.nextDouble();
}
/*for(int p = 1; p < data.length; p ++){
System.out.println(data[p]);
}*/
//selectionSort();
}
public void calculateMean(){
double total = 0;
double sum = 0;
double average = 0;
for(int counter = 0; counter < data.length; counter++){
sum = sum + data[counter];
average = sum / data.length;
}
}
public void selectionSort(){
int temp = 0;
for(int joey = 0; joey<data.length; joey++){
for(int i = 1; i < data.length; i++) {
if(data[i - 1] > data[i]) {
temp = data[i-1];
data[i-1] = data[i];
data[i] = temp;
}
}
}
for(int p = 0; p < data.length; p++){
System.out.println(data[p]);
}
}
public String toString(){
return null;
}
}
You can call the 2 methods in your constructor like this
// Inside constructor
selectionSort(); // call the method
calculateMean(); // call the method
System.out.println(toString()); // print the data using toString
And your toString() can look like this
public String toString() {
return Arrays.toString(data);
}
Also, the logic in your calculateMean() is a bit wrong.
for (int counter = 0; counter < data.length; counter++) {
sum = sum + data[counter];
}
average = sum / data.length; // Calculate the average after finding the sum and not at every iteration.
Moreover, I see that the mean instance variable is not. You might want to assign the average calculated in the calculateMean() method to it, and you might want to print it some method or the toString() itself, depending in your needs.
Edit:
You're shadowing your instance variable int data[]; with the local variable in the constructor, double data[] = new double[6]; and that is the reason you're getting the NPE. Remove the local variable from the constructor and instead initialize it like this
data[] = new int[5];
And the following for loop to get the input from the user to start from the index 0 and not 1.
for(int i = 0; i < data.length; i++){

Categories