Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Any java programmers out there?

Any java programmers out there?
Thread Tools
Senior User
Join Date: Sep 2000
Location: Glasgow, Scotland UK
Status: Offline
Reply With Quote
Oct 23, 2003, 01:16 PM
 
I'm a first year CS student and would like some help with some code i'm writting. We are just starting to learn java and this will be the first proper language i've learned, since PHP doesn't really count so please be gentle.


We wrote the following program.

//This program is called GreatDivisor and it should take two integers and return their greatest common divisor.

class GreatDivisor
{
public static int computeGCD(int a, int b)
{
while (b != 0)
{
int c = a % b;
a = b;
b = c;
}
int gdc = a;
return gdc;
}
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
System.out.print("The Greatest Common Divisor For The Specified Number Is ");
System.out.println(computeGCD(a,b));
}

}

However the extension of the question is to convert the Integer stuff to use java.math.BigInteger. Having looked and the Java API documentation i can't make much sense of how the hell this works. i know i can't use the ordinary operators cos they give me compiler errors which makes sense. But the operators for in the documentation appear to be methods and it won't let me apply these methods. Something about them not being able to applied to static methods.

Heres what i've managed so far, but it doesn't compile.

//This program is called GreatDivisor and it should take two integers and return their greatest common divisor.

import java.math.BigInteger;

class GreatDivisor
{
public static void main(String[] args)
{
BigInteger a = Integer.parseInt(args[0]);
BigInteger b = Integer.parseInt(args[1]);
System.out.print("The Greatest Common Divisor For The Specified Number Is ");
System.out.println(BigInteger.gcd(BigInteger.abs(a ),BigInteger.abs(b)));
}

}

If anyones got a minute to help me out then i would be very greatful.

thanks in advance.
"You can't waste a life hating people, because all they do is live their life, laughing, doing more evil."

-ALPHA ROBERTSON,whose daughter was one of four girls killed in the bombing of a Birmingham, Ala., church in 1963.
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Oct 23, 2003, 01:33 PM
 
First off, this thread should probably be in the Developer forum.

Other than that, you might want to look at the Constructor Summary for the BigInteger Object again.

Try this:

Change your code from

Code:
BigInteger a = Integer.parseInt(args[0]); BigInteger b = Integer.parseInt(args[1]);
to

Code:
BigInteger a = new BigInteger(args[0]); BigInteger b = new BigInteger(args[1]);
I don't know if that will work, but give it a shot and see what happens.

/me goes off to see if I can compile this . . .
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Oct 23, 2003, 01:42 PM
 
I think you want to study that javadoc page again. The following code seems to do what you want:

Code:
//This program is called GreatDivisor and it should take two integers and return their greatest common divisor. import java.math.BigInteger; class GreatDivisor { public static void main(String[] args) { BigInteger a = new BigInteger(args[0]); BigInteger b = new BigInteger(args[1]); System.out.print("The Greatest Common Divisor For The Specified Number Is "); System.out.println(a.gcd(b.abs())); } }
     
ntsc  (op)
Senior User
Join Date: Sep 2000
Location: Glasgow, Scotland UK
Status: Offline
Reply With Quote
Oct 23, 2003, 01:50 PM
 
thats brilliant thank you! i forgot about the developer forum, oops! oh well done now.

i'll sit with this and the java documentation and see if i can figure out how the semantics of what they have written actually work. that was what was confusing me more than anything.
"You can't waste a life hating people, because all they do is live their life, laughing, doing more evil."

-ALPHA ROBERTSON,whose daughter was one of four girls killed in the bombing of a Birmingham, Ala., church in 1963.
     
Forum Regular
Join Date: Oct 2000
Location: Berlin / Germany
Status: Offline
Reply With Quote
Oct 23, 2003, 02:02 PM
 
Well, I don't know what to say. You should probably try to understand how object orientation works into this.

The short answer is this:
http://java.sun.com/j2se/1.4.1/docs/...th.BigInteger) which is the function on BigInteger that gives you the Greatest Comon Divisor.

The long answer is that you need to construct a BigInteger Object (which is a true Object instead of the primitive types like int) like this:

BigInteger aBigInt = new BigInteger(aString);

Well, try this and try to understand why and how it works:

--- snip ---
/** This program is called GreatDivisor and it should take two integers and
* return their greatest common divisor.
*/
class GreatDivisor {
public static int computeGCD(BigInteger a, BigInteger b) {
while ( ! b.compareTo(BigInteger.ZERO)) {
BigInteger c = a.mod(b);
a = b;
b = c; }
BigInteger gdc = a;
return gdc; }

public static void main(String[] args) {
BigInteger a = new BigInteger(args[0]);
BigInteger b = new BigInteger(args[1]);
System.out.print("The Greatest Common Divisor For The Specified Number Is ");
System.out.println(computeGCD(a,b));
// It may probably be easier to do it this way though...
System.out.println(a.gcd(b); }

}
--- snap ---

Sorry about the mashed up indention, just paste it into ProjectBuilder and say "ReIndent" to have it fixed.

Have phun!

cu Martin
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 05:55 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2