• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java method question

 
Mahlon Berv
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand how to write Java code?
 
Mahlon Berv
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also keep in mind that b.invert() does not what you may think it does since b.num == 0.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic