• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why am I getting NaN?  RSS feed

 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was tasked to write some code which imitates an electronic circuit, I guess the details aren't really relevant but for some reason I'm getting NaN on some of my tests and I can't seem to find out why.

Here is the code I'm using







this is the code which is used for testing:



You might notice there's another class which is tested that I didn't include above, the reason is because it passed the test so I guess I don't need to change it. Here is my test results vs what the website expected:

Running AmplifierTester
=======================

1.82
+ Expected: 1.82
3.67
+ Expected: 3.67
NaN
- Expected: 3.00
7.67
+ Expected: 7.67
NaN
- Expected: 0.67
0.87
+ Expected: 0.87

Score
4/6
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using a different set of r1, r2 variables in your overridden classes than you are in your base class. So your constructor sets the versions in the base class and your getGain() methods use the versions in the overridden class, which haven't been set.

you should only define r1 and r2 in your base class. And you don't need to override the getR1() and so on methods, either, just use the base class's methods.

Once you get rid of the redundant variables and methods from your overridden classes you're going to have to use the getR1() method instead of the r1 variable, and so on, in getGain(). That way you have r1 and r2 encapsulated properly in your base class.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You're using a different set of r1, r2 variables in your overridden classes than you are in your base class. So your constructor sets the versions in the base class and your getGain() methods use the versions in the overridden class, which haven't been set.

You should only define r1 and r2 in your base class. And you don't need to override the getR1() and so on methods, either, just use the base class's methods.

Once you get rid of the redundant variables and methods from your overridden classes you're going to have to use the getR1() method instead of the r1 variable, and so on, in getGain(). That way you have r1 and r2 encapsulated properly in your base class.


Oh right I didn't think about that, thanks. I'm still a bit confused though because, here is some code from the class which already passed the test (the one that I said I didn't mention earlier):



As you can see this class has overridden the base class methods but it still worked and passed the test. Why is that?
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is some code from the class which already passed the test

I ran your code and I get java.lang.ArithmeticException: / by zero at InvertingAmplifier.getGain(InvertingAmplifier.java:59)
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, after following Paul's suggestions, you might want to make the Amplifier class and the getGain method abstract, unless the Amplifier class actually represents a circuit which always has zero gain, regardless of the two resistor values.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!