• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java method question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having trouble understanding a certain Java problem.

Below is the class for the problem



Question

1. What will be printed out when the following code is run?

Fraction a = new Fraction(2,3);
Fraction b = new Fraction();
b.invert();
b.addToFraction(new Fraction(3,4));
a.addToFraction(b);
a.print();

(1) 7/6
(2) 17/12
(3) 5/7
(4) 6/7
(5) 2/1

we are also supposed to trace the program. I get stuck on b.addToFraction(new Fraction(3,4));, what is happening there? If somebody could explain the whole program for me that would be great.

Thanks
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand how to write Java code?
 
Mahlon Berv
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i'm pretty new to java, I'm taking AP computer science, but I know the basic stuff, I just need help distinguishing what that particular method, AddTofraction does, what objects are manipulated and how the variables are affected by it.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I get stuck on b.addToFraction(new Fraction(3,4));, what is happening there? If somebody could explain the whole program for me that would be great.



It would help if you actually take a shot at it. You can't learn if you are unwilling to take a guess on what it is doing... but... to answer your question....

It is calling the addToFraction() method, on the object referenced by the b variable. And it is passing it a Fraction object which is instantiated on the same line.

Henry
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also keep in mind that b.invert() does not what you may think it does since b.num == 0.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic