-
static/ dynamic help
Code:
public class getHypo
{
private double a;
private double b;
private double c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
double getC()
{
return c;
}
}
public class pythagorian
{
public static void main(String[] args)
{
double a, b, c;
ConsoleIO console = new ConsoleIO();
getHypo **** = new getHypo();
System.out.print("a = ");
a = console.readDouble();
System.out.print("\nb = ");
b = console.readDouble();
System.out.print("\nc = ");
c = ****.getC();
System.out.println(c);
}
}
when i execute it, and input a = 3 b = 4 c should = 5, but it equals 0.0......what do i do/ what did i do wrong?
-
Check my comments
With less code than this getHypo is reduced to a mere wrapper 
Code:
public class getHypo {
/*
private double a;
private double b;
the c you declare here is local to the getHypo class, the a and
b values are also local (and 0).
private double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
*/
double getC(double a, double b) {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
}
public class pythagorian {
public static void main(String[] args) {
double a, b, c;
ConsoleIO console = new ConsoleIO();
getHypo **** = new getHypo();
System.out.print("a = ");
a = console.readDouble();
System.out.print("\nb = ");
b = console.readDouble();
System.out.print("\nc = ");
c = ****.getC();
System.out.println(c);
}
}
eschew obfuscation
-
thanks! it works now. any idea what the problem was before?
-
Similar Threads
-
Replies: 2
Last Post: 06-22-2005, 08:21 AM
-
Replies: 2
Last Post: 08-27-2003, 11:00 PM
-
Replies: 1
Last Post: 11-06-2001, 09:43 AM
-
By max caber in forum .NET
Replies: 13
Last Post: 10-03-2001, 04:05 PM
-
By Anand in forum Enterprise
Replies: 1
Last Post: 08-13-2000, 09:10 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|