wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static method problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static method problem" Watch "Static method problem" New topic
Author

Static method problem

Ian Dudek
Greenhorn

Joined: Feb 03, 2006
Posts: 9
Well I was messing around with it more and I am getting an error message now that says can not resolve symbol where I have return larger;. Dam I thought I had it there.

Ian

public class Lab13C {
public static void main(String[] args) {
System.out.println("Larger number: " + Calc.larger(7, 3.5));
}
}
class Calc {
public static double larger(double p1,double p2) {
if (p1 > p2) {
System.out.println("1 - the first parameter has the larger value");
}
if (p1 < p2) {
System.out.println("2 - the second parameter has the larger value");
}
else {
System.out.println("0 - the parameters have equal values");
return larger;
}
}
}
[ February 22, 2006: Message edited by: Ian Dudek ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
There are a couple of problems I see. You don't put a semicolon after the parenthesis following the parameter list.

Also you don't put the type of a variable in the if statement.
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
...and you missed an extra curly brace to close class Calc. and you never return anything from larger. and you need to use Calc.larger(7, 3.5) since it otherwise wouldn't know where to find the method. and your second if needs to be an else if or you'll get two messages printing out whenever the first double is larger than the second.

EDIT:

Forgot that you couldn't touch the code in main, so you have two options:

1) Make the larger function part of the Lab13C class.
2) Use a static import for larger. Probably overkill.
[ February 22, 2006: Message edited by: Craig Tyler ]
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
i see your problem here

public class Lab13C {
public static void main(String[] args) {
System.out.println("Larger number: " + larger(7, 3.5));
}
}


in this class you are calling a method outside the class so you have to instantiate the Calc class in order fo you the method on the calc class


class Calc {
public static double larger(double p1, double p2); {
if (double p1 > double p2) {
System.out.println("1 - the first parameter has the larger value");
}
if (double p1 < double p2) {
System.out.println("2 - the second parameter has the larger value");
}
else {
System.out.println("0 - the parameters have equal values");
}
}


in the other class you have extra ; on the line public static double larger(double p1, double p2); {

and you declared the variables p1 and p2 like in the line if (double p1 < double p2) {


jherald
Ian Dudek
Greenhorn

Joined: Feb 03, 2006
Posts: 9
Crap I am still not getting this section... I could still use some more help.
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
try this i modified your code
Jherald Lacambra
Ranch Hand

Joined: Feb 02, 2005
Posts: 129
try this i modified your code

public class Lab13C {
public static void main(String[] args) {
Calc cl = new Calc();
System.out.println("Larger number: " + cl.larger(7, 3.5));
}
}

class Calc {
public static double larger(double p1, double p2){
if (p1 >p2) {
System.out.println("1 - the first parameter has the larger value");
}
if (p1 < p2) {
System.out.println("2 - the second parameter has the larger value");
}else{
System.out.println("0 - the parameters have equal values");
}
return p1;
}

}
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
Originally posted by Jherald Lacambra:
i see your problem here

public class Lab13C {
public static void main(String[] args) {
System.out.println("Larger number: " + larger(7, 3.5));
}
}


in this class you are calling a method outside the class so you have to instantiate the Calc class in order fo you the method on the calc class


larger() is static so an instance isn't needed.

Going back to the original question, the reason the compiler is complaining that it cannot resolve the symbol larger is that you never declared a variable of that name. Also...oh...it's easier for me to post a code sample.



I really think you should spend some time with a good Java book or find a tutorial online. It would probably save you some frustration. Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method problem