• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reciprocal does not switch number like it should?  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method that inverts the fractions so that the numerator becomes the denominator.


But when I put that code in main:


The output I get is:


I tried with different numbers, Ex:


and got this as my output:


Why does it get printed like that when my invert method has the numerator and denominator being switched?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remeber that your code runs one line at a time. So, in your invert method, let say you pass in inverts with numerator of 1 and denominator of 2.

now we run line 2:

System.out.println(inverts.numerator = inverts.denominator);

So, we set inverts.numerator to 2, and print that.

Now, we run line 3:

System.out.println(inverts.denominator = inverts.numerator);

so we set denominator to whatever numerator holds...which we just set to 2, so we print that.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:I have a method that inverts the fractions so that the numerator becomes the denominator.
...
But when I put that code in main:


At the risk of sounding like a nag, format your code properly.

This is one of the frustrating things to see with most kids being taught Java programming these days. This is very procedural style code. The longer you program this way, the more bad habits you develop and the more difficult it will be to switch to the kind of thinking you need to write proper object-oriented programs in Java.

With a more object-oriented approach, you would write something like this (and please note how the code is properly aligned and indented—it really isn't that hard to do):

A Rational (note that the name is singular) number has all the information it needs to determine what its own reciprocal would be, therefore, a more object-oriented approach would be for a Rational number to have an invert() or even reciprocal() method that returns its own reciprocal as another Rational object. This is in line with how the standard wrapper classes like Integer and Double work.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:Why does it get printed like that when my invert method has the numerator and denominator being switched?

As Fred explained, your numerator and denominator were *not* switched. What happened is that you set your numerator to whatever your denominator was. When you then set your denominator to be whatever the numerator was, you set it to the new value of numerator, not the old value.

In languages like Java, you need a temporary "holder" variable to save the value of the first variable before you overwrite it with the value of the other variable. The idiom for swapping values in Java is:
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're still having trouble understanding what happened, here's what it looks like:

Now, compare that to this:
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you did it the object-oriented way, you wouldn't have to deal with all that swapping stuff:

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:BTW, if you did it the object-oriented way, you wouldn't have to deal with all that swapping stuff:



I have to do it following these directions:
Write a method called invert that inverts the number by swapping the numerator and denominator. This method is also a modifier that should return void. Add lines to main to test the new method.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how I solved it:
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I know the formatting and variable names are a bother, but I have to use them for my class in order to get a good grade.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:And I know the formatting and variable names are a bother, but I have to use them for my class in order to get a good grade.

I find it hard to believe that the way you format your code, if what you post is a reflection of what you submit, is actually what it takes to get a good grade. If it were up to me, your formatting would be a deduction in points every time.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Lexi Turgeon wrote:And I know the formatting and variable names are a bother, but I have to use them for my class in order to get a good grade.

I find it hard to believe that the way you format your code, if what you post is a reflection of what you submit, is actually what it takes to get a good grade. If it were up to me, your formatting would be a deduction in points every time.


I understand that, but I am just following the examples I am given.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:And I know the ... variable names are a bother, but I have to use them for my class in order to get a good grade.

And regarding the variable/class names, it would benefit you as a student to at least discuss the name with your instructor. Instructors are not infallible and you should at least apply some critical thinking and not just have a "do what you're told just because" mentality. If the instructor insists you use "Rationals" instead of "Rational," you should at least understand why the name he wants you to use is not appropriate and then suggest and discuss the alternative with him/her.  If I were the instructor, I would actually appreciate a student who displays that kind of independent thinking and would be inclined to give him/her extra credit.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sadly, my instructor does not offer feedback or a sense of communication. It is always short answers. This is an online class so I get why it may be hard, but still. I would like to learn things like this but all my instructor does is grade and tell me what to do her way.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I hate to say it but if you're paying for that class, you're not getting your money's worth.
 
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!