Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Need Help

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
To calculate standard deviation, I wrote the following program:
import java.lang.Math.*;
public class StandTest {
public static void main(String args[]) {
Stat record = new Stat();
record.addValue(2);
try {
System.out.println("Standard Deviation is" + Math.sqrt());
} catch (Exception e) {
System.out.println("Exception" + e.getMessage());
}
}
}

import java.lang.Math.*;
public class Stand {
private int N;
private double S;
private double S1;
private double SoS;
public void addValue(double d) {S +=d; S1 = (d*d); SoS+=S1; N++;}
public static double Math.sqrt() throws Exception {
if (N < 0) {
throw new Exception("negative value is not allowed");
}
return ((N*SoS)-(S*S))/(N*(N-1));}
}
When I complied it, two error messages:
Stand.java:9: ';' expected
public static double Math.sqrt() throws Exception {
^
StandTest.java:12: sqrt(double) in java.lang.Math cannot be applied to () System.out.println("Standard Deviation is" + Math.sqrt());
Why? Thanks.
luk

 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i am wrong someone will correct me on this , But if class is final it's methods are final implicitly. Math class is final therefore you can't overwrite it's sqrt Method plus if i am wrong on this too sqrt takes a parameter which value to take square of. Let me feel the heat
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also static methods may not be overriden , i checked and sqrt method takes a value as a parameter of double.
 
rubbery bacon. crispy tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic