• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

need help debugging this code:

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class DebugMe1
{
public void main(String [] args)
{
int a;
int result = method1(a);
System.out.println("The result of passing a to method1 is: " + result);
}
public int method1(int y)
{
int b = 2;
int a = b + method2(y);
return a;
}
public int method2(int z)
{
int c = z * 10;
return c;
}
}
/*debug this code. The output should be: "The result of passing a to method1 is: 32"
From the main method, call method1 and pass it the class-level variable.
*/
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"robincelena",
your name is not valid as per the Javaranch name conventions. Please refer to the document located here
Also, your post doesn't belong to this forum so I will move it to: Java in general (intermediate) forum, look there, you'll find more answers to your doubt.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
robincelena
Some of the erros you're getting are these:
--your main method should be declared static, or the JVM will not be able to find it when it is run.
--you never initialize the variable a, the compiler will give you an error that says 'variable may not have been initialized'.
--after you make main static it will tell you that you can't call method1 from a static context. Meaning you cant call an instance method from a class method, it has no idea which instance of the class you want it to act on. To fix this you can either make both method1 and method2 static or, in main, create an instance of your class and call method1 with that instance. Like this:

that should do it for you, if you have more questions feel free to post them.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, but I tried to do what you told me and ended up with many more errors than when I started. Can you actually debug the entire thing? I am fairly new at this stuff and unfortunately need to be walked through the process a little differently. I appreciate your assistance and whatever else you can do to help will be greatly appreciated.

Originally posted by Dave Vick:
[B]robincelena
Some of the erros you're getting are these:
--your main method should be declared static, or the JVM will not be able to find it when it is run.
--you never initialize the variable a, the compiler will give you an error that says 'variable may not have been initialized'.
--after you make main static it will tell you that you can't call method1 from a static context. Meaning you cant call an instance method from a class method, it has no idea which instance of the class you want it to act on. To fix this you can either make both method1 and method2 static or, in main, create an instance of your class and call method1 with that instance. Like this:

that should do it for you, if you have more questions feel free to post them.
[/B]


 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robin
Post what you got after you made the changes. You'll learn more if we can help you with your code and show you your own mistakes instead of someone just giving you code thats works.


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic