• Post Reply Bookmark Topic Watch Topic
  • New Topic

My code isn't working when written in OOP style  RSS feed

 
Drew Noseman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm attempting to make a simple Pythagorean Theorem program (A squared + B squared = C squared) but am running into problems when I write it as object oriented. The darn thing works when written as a simple process, but that isn't Java now is it? Thanks for any suggestions you can provide!

Here's the simple:


Here's the OOP:
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare a1 and b1 as scoped class variables. The way you have it now, they leave scope after your input. ie. When askAPlusB() finishes, they leave scope before calculateC() can read them.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Drew. Welcome to the Ranch!

Roger has explained the problem is with the scoping of variables. You can solve the compiler errors by making them instance variables, although it would probably be better to look at returning values from your methods and passing arguments to them.

But out of interest....why do you consider that more "object-oriented"? You aren't using any additional objects, you've just split a method into multiple smaller ones. There's nothing particularly object-oriented about that.
 
Drew Noseman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.

Your keen eye has revealed that I don't really know what I'm talking about! At the time I wrote the first question, I was confused, thinking that a method could be considered an object. I am in the process of trying to teach myself Java through various online resources (Stanford's programming methodologies, Thinking in Java) with the hope of learning enough to eventually begin a career as a Java programmer. Unfortunately, there aren't any colleges near me that offer courses in Java and I'm finding the learning curve on that particular language to be pretty steep, at least when self-educating. If you have any suggestions regarding this, please let me know!

As far as the topic at hand, I am unfamiliar with: scoping, instance variables, passing arguments. Would this be an occasion to use a getter/setter?

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try here for passing arguments.

The learning curve for Java is not at all steep. The learning curve for programming is very steep.

Suggestion: Maybe:
What you want is to create a RightTriangle class with two sides and use Pythagoras to work out the third side. Start off with a toString method which returns something like this
RightTriangle: a = 123.0 b = 234.0 c = 264.36
That might help you. You would also need a TriangleDemo class, which I shall let you have free That will only work if your RightTriangle class also works.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!