Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Java method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java method question " Watch "Java method question " New topic
Author

Java method question

Mahlon Berv
Greenhorn

Joined: Oct 22, 2008
Posts: 2
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

Joined: Apr 16, 2008
Posts: 2187
Do you understand how to write Java code?
Mahlon Berv
Greenhorn

Joined: Oct 22, 2008
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Also keep in mind that b.invert() does not what you may think it does since b.num == 0.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
jQuery in Action, 2nd edition
 
subject: Java method question
 
Similar Threads
Help with reprinting downloaded text from FTP site onto GUI!
constructor problems
null pointer exception
sorting fractions problem
java program using a class fraction