Simple error can't figure out.
Trying to write a standard deviation program. Everything was going good until this 1 error started in my class and I can't figure it out.
My error is at the bottom on Line 70: possible loss of precision
found : double
required: int
Code :
import java.lang.*;
import java.util.*;
class Standard_Deviation
{
static int mean = 0;
static int sums = 0;
static int subtracted = 0;
static int squared = 0;
static int divided = 0;
static int sqroot = 0;
protected int array[];
static Scanner console = new Scanner(System.in);
public Standard_Deviation(int size) //Write out the input/output in this class.
{
System.out.println("How many values?");
size = console.nextInt();
array = new int[size];
System.out.println("Enter your value(s).");
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt();
}
System.out.println();
}
public int getSums()
{
for (int n = 0; n < array.length; n++)
{
sums += array[n];
}
return(sums);
}
public int getMean()
{
for (int n = 0; n < array.length; n++)
{
mean = sums / array.length;
}
return(mean);
}
public int getSquare()
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean;
squared = subtracted * subtracted;
}
return(squared);
}
public int getDivide()
{
for (int n = 0; n < array.length; n++)
{
divided = squared / array.length;
}
return(divided);
}
public int getSquareRoot()
{
for (int n = 0; n < array.length; n++)
{
//Line 70 error sqroot = Math.sqrt(divided);
}
return(sqroot);
}
}
Thanks in advance.
Re: Simple error can't figure out.
Quote:
Originally Posted by
n00bprogrammer
Trying to write a standard deviation program. Everything was going good until this 1 error started in my class and I can't figure it out.
My error is at the bottom on Line 70: possible loss of precision
found : double
required: int
Code :
import java.lang.*;
import java.util.*;
class Standard_Deviation
{
static int mean = 0;
static int sums = 0;
static int subtracted = 0;
static int squared = 0;
static int divided = 0;
static int sqroot = 0;
protected int array[];
static Scanner console = new Scanner(System.in);
public Standard_Deviation(int size) //Write out the input/output in this class.
{
System.out.println("How many values?");
size = console.nextInt();
array = new int[size];
System.out.println("Enter your value(s).");
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt();
}
System.out.println();
}
public int getSums()
{
for (int n = 0; n < array.length; n++)
{
sums += array[n];
}
return(sums);
}
public int getMean()
{
for (int n = 0; n < array.length; n++)
{
mean = sums / array.length;
}
return(mean);
}
public int getSquare()
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean;
squared = subtracted * subtracted;
}
return(squared);
}
public int getDivide()
{
for (int n = 0; n < array.length; n++)
{
divided = squared / array.length;
}
return(divided);
}
public int getSquareRoot()
{
for (int n = 0; n < array.length; n++)
{
//Line 70 error sqroot = Math.sqrt(divided);
}
return(sqroot);
}
}
Thanks in advance.
I think the Math class returns a double.
Try casting it to an int:
sqroot = (int) Math.sqrtrt(dividend);
I think it needs a () around the word int, but try {} if it doesn't work.
or try
squroot = int (Math.sqrt(dividend)); if it still doesn't work.
Re: Simple error can't figure out.
sqroot = (int) Math.sqrt(divided); got it. Thanks bro :-bd
Re: Simple error can't figure out.
Your computations are going to be wrong if you do all the math using ints. For example: 11/6 = 1
Re: Simple error can't figure out.
Quote:
Originally Posted by
Norm
Your computations are going to be wrong if you do all the math using ints. For example: 11/6 = 1
Thanks a lot.. I switched everything to doubles and took the (int) off of the Math.sqrt.. everything compiled but my calculations for the sqrt are showing 0.0
EDIT: I thought it would have been because I have static double sqroot = 0; and that might have been throwing it off so I took off the = 0 but it did nothing; still showing 0.0
Re: Simple error can't figure out.
Did you remove ALL of the integer divisions?
Re: Simple error can't figure out.
You have dividend set to 0. What if you called getDivide()?
Code :
sqroot = Math.sqrt(getDivide());
return(sqroot);
and in getDivide()
dividend = getSquare()/array.length;
Also,
you could've
squared = Math.pow(subtracted,2);
which would have done the same thing as what you did.
May not do much more in this case than what you did, but it works better if you're cubing it or higher.
Math.pow(number,power);
Re: Simple error can't figure out.
Well I removed all of the ones I thought were necessary.. On the methods with division I also tried changing the variablehere / array.length to variablehere / array[size] but it didn't make a difference.
Here's my code now..
Code :
class Standard_Deviation
{
static int size;
static double mean = 0;
static double sums = 0;
static double subtracted = 0;
static double squared = 0;
static double divided;
static double sqroot;
protected int array[];
static Scanner console = new Scanner(System.in);
public Standard_Deviation(int size) //Write out the input/output in this class.
{
System.out.println("How many values?");
size = console.nextInt();
array = new int[size];
System.out.println("Enter your value(s).");
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt();
}
System.out.println();
}
public double getSums()
{
for (int n = 0; n < array.length; n++)
{
sums += array[n];
}
return(sums);
}
public double getMean()
{
for (int n = 0; n < array.length; n++)
{
mean = sums / array[size];
}
return(mean);
}
public double getSquare()
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean;
squared = subtracted * subtracted;
}
return(squared);
}
public double getDivide()
{
for (int n = 0; n < array.length; n++)
{
divided = squared / array[size];
}
return(divided);
}
public double getSquareRoot()
{
for (int n = 0; n < array.length; n++)
{
sqroot = Math.sqrt(divided);
}
return(sqroot);
}
}
Re: Simple error can't figure out.
Wow sqroot = Math.sqrt(getDivide()); just did it. Thanks you guys are awesome.
Re: Simple error can't figure out.
Code :
for (int n = 0; n < array.length; n++)
{
sqroot = Math.sqrt(divided);
}
This loop is doesn't do anything useful. In fact most of your loops are useless. Very few of them use the loop index.
There is a lot of mysterious code in your program that I have no idea what it is supposed to be doing.
Can you add comments on most of the lines of code describing what they do?
Re: Simple error can't figure out.
Sure.
Code :
class Standard_Deviation
{
static int size; //static variables.
static double mean = 0;
static double sums = 0;
static double subtracted = 0;
static double squared = 0;
static double divided;
static double sqroot;
protected int array[]; //protected variable
static Scanner console = new Scanner(System.in);
public Standard_Deviation(int size) //constructor
{
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
public double getSums() //getSums method.
{
for (int n = 0; n < array.length; n++)
{
sums += array[n]; //adds data.
}
return(sums);
}
public double getMean() //getMean method.
{
mean = sums / array[size]; //divides data to get the average.
return(mean);
}
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
}
return(squared);
}
public double getDivide() //getDivide method.
{
divided = squared / array[size]; //divides data.
return(divided);
}
public double getSquareRoot() //getSquare Root method.
{
sqroot = Math.sqrt(getDivide()); //uses Math.sqrt to get the square root of the divided data.
return(sqroot);
}
}
I thought the for loops were needed to loop through array[n] that's why I used them.. I have no idea why I used them for every method but I took out the ones I thought weren't needed.
My instructor is new and isn't all that great so she has been putting out sample programs and I've been having to use them to try to learn and do the projects. This project was to program a standard deviation calculator using a class and the driver with a main method. Here's the driver.
Code :
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
Standard_Deviation sd = new Standard_Deviation(2);
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
Re: Simple error can't figure out.
Another useless loop. squared only set from the last loop.
Code :
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
}
Quote:
I took out the ones I thought weren't needed.
Is this programming by random copy and pasting?
You need to have a design for what you want the program to do before you start coding.
Quote:
divided = squared / array[size]; //divides data..
The comment is redundant. You can see that there is a divide there.
What value is in squared and what value is in the array at index size? Why do you want to divide squared by the element at index size?
Re: Simple error can't figure out.
Quote:
Originally Posted by
n00bprogrammer
Sure.
Code :
class Standard_Deviation
{
static int size; //static variables.
static double mean = 0;
static double sums = 0;
static double subtracted = 0;
static double squared = 0;
static double divided;
static double sqroot;
protected int array[]; //protected variable
static Scanner console = new Scanner(System.in);
public Standard_Deviation(int size) //constructor
{
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
public double getSums() //getSums method.
{
for (int n = 0; n < array.length; n++)
{
sums += array[n]; //adds data.
}
return(sums);
}
public double getMean() //getMean method.
{
mean = sums / array[size]; //divides data to get the average.
return(mean);
}
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
}
return(squared);
}
public double getDivide() //getDivide method.
{
divided = squared / array[size]; //divides data.
return(divided);
}
public double getSquareRoot() //getSquare Root method.
{
sqroot = Math.sqrt(getDivide()); //uses Math.sqrt to get the square root of the divided data.
return(sqroot);
}
}
I thought the for loops were needed to loop through array[n] that's why I used them.. I have no idea why I used them for every method but I took out the ones I thought weren't needed.
My instructor is new and isn't all that great so she has been putting out sample programs and I've been having to use them to try to learn and do the projects. This project was to program a standard deviation calculator using a class and the driver with a main method. Here's the driver.
Code :
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
Standard_Deviation sd = new Standard_Deviation(2);
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
Wouldn't the methods in the first class have to be static too to be used in the main method of the second class? I thought static methods could only call other static methods.
Maybe I'm wrong.
Re: Simple error can't figure out.
Quote:
Originally Posted by
Norm
Another useless loop. squared only set from the last loop.
Is this programming by random copy and pasting?
You need to have a design for what you want the program to do before you start coding.
I never copy and pasted anything. My instructor told me to use the for loop when I asked for her help on the very first method I wrote, then I just assumed I needed to do it for every method. As my user name states.. I am very new to java programming and I don't know much.
What do you mean by design? Like a flow-chart? I've never been taught any kind of outline or flow-chart to make for a program.
Re: Simple error can't figure out.
Code :
System.out.println("Enter your value(s).");
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt();
}
this is going to ask the user to enter the values only once, but expect them to enter it array.length times.
Code :
for (int n = 0; n < array.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array[n] = console.nextInt();
}
this asks them to enter values array.length times and expects array.length inputs.
Code :
public Standard_Deviation(int size) //constructor
{
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
Why would a constructor ask for the size value? Couldn't your test program do that instead?
Code :
public Standard_Deviation(int size) //constructor
{
size = this.size; // hopefully this statement doesn't make it equal to 0.
array = new int[size]; //declared array
System.out.println("Enter your value(s)."); //prompts more input.
for (int n = 0; n < array.length; n++)
{
array[n] = console.nextInt(); //stores input.
}
System.out.println();
}
Code :
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
int size = 0;
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
Standard_Deviation sd = new Standard_Deviation(size); // unless you wanted a 2, but the way it is
// now will work with constructor and every time you need to use the value size.
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
Code :
public double getSums() //getSums method.
{
for (int n = 0; n < array.length; n++)
{
sums += array[n]; //adds data.
}
return(sums);
I think this will go through the loop and only return the value it has at the end of the loop.
Code :
public double getMean() //getMean method.
{
mean = sums / array[size]; //divides data to get the average.
return(mean);
}
this is dividing sums, or getSums() if you changed it, by the value in the index size of your array.
However, arrays start at 0, so your max index is array[size-1]. What you're doing right now is dividing by the value in an index that isn't there!
Code :
public double getDivide() //getDivide method.
{
divided = squared / array[size]; //divides data.
return(divided);
}
Again, that index doesn't exist.
I'm not sure if you're hoping to divide by the element at array[size -1 ] or if you're hoping to get the value
divided by array.length, so I can't be more specific in that area.
Code :
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
}
return(squared);
}
n = 0;
subtracted = array[0] - mean;
squared = Math.pow(subtracted,2);
n = 1;
subtracted = array[1] - mean;
squared = subtracted * subtracted; // same as Math.pow(subtracted, 2)
n = 2;
subtracted = array[1] - mean;
squared = Math.pow(subtracted,2);
n = array.length - 1;
subtracted = array[array.length - 1] - mean;
squared = Math.pow(subtracted, 2);
end loop
return value for last squared value calculated.
I don't think it's saving your values before that.
if you're hoping to get all the values, you could do this:
Code :
public void getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
System.out.println(squared);
}
}
However, if you do this, you'll have to update your method call in Driver and anywhere else you might have called getSquare().
I'm not sure what the code below will do:
Code :
public double getSquare() //getSquare method.
{
for (int n = 0; n < array.length; n++)
{
subtracted = array[n] - mean; //subtracts mean from the array
squared = subtracted * subtracted; //multiplys results.
return(squared);
}
}
but it's very possible your return value will be lost when the loop ends so I wouldn't recommend writing it that way.
Also,
Code :
for (int n = 0; n < array.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array[n] = console.nextInt();
}
should be in the Driver class.
If you made the array public, or at least default (visible to all in package but none outside package), put two classes inside same package.
default looks like this
int array[];
protected is for classes that are in the same package, that can access protected as well I've heard, or for classes that extend, or inherit methods from other classes.
You could do this:
Code :
import java.lang.*;
public class Standard_Deviation_Driver
{
public static void main(String[] args)
{
int size = 0;
System.out.println("How many values?"); //prompts input from the user.
size = console.nextInt(); //stores the input
System.out.println();
Standard_Deviation sd = new Standard_Deviation(size); // unless you wanted a 2, but the way it is
// now will work with constructor and every time you need to use the value size.
int [] array2 = new int[sd.array.length];
for (int n = 0; n < array2.length; n++)
{
System.out.println("Enter your value."); // since it's only asking for one at a time, enter change it
// to singular. True, they could enter them all at once with a space, but still.
array2[n] = console.nextInt();
array2[n] = sd.array[n];
System.out.println();
}
// that may work. If not, you still need to have that line asking for each of the values in the Driver class. //You shouldn't be asking for input directly from the user using console.next Whatever or //Whatever.parseWhatever.
double a = sd.getSums();
double b = sd.getMean();
double c = sd.getSquare();
double d = sd.getSquareRoot();
double e = sd.getDivide();
System.out.println("Sum: " + a);
System.out.println("Mean: " + b);
System.out.println("Squared: " + c);
System.out.println();
System.out.println("Standard deviation: " + d);
}
}
Also, when using the variables in the regular, non-Driver Standard_Deviation class, try to use getValue() instead of value.
Like this:
Code :
mean = getSums() / array[size]; //divides data to get the average.
return(mean);
Code :
divided = getSquare() / array[size]; //divides data.
return(divided);
Also, if you get the message: non-static method cannot be referenced from a static context, or something like that, change your methods to static in first class.