954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cant Solve this Java Error

I an trying to write a few java classes, which I wrote successfully, but when I am tryin to test it I get some weird errors and I cant figure out why, so i'd be very grateful for your expertise.

The code for the GCD class is:

public class GCD {
	public static long gcd(long x, long y) {
		while (y != 0) {
			long r = x % y;
			x = y; y = r;
		}
	return x;
	}
}

This compiles without any problems.


The code to test the GCD class is :

import java.util.*;
public class GCDTest {
	public static void main(String[] args) {
		long x = (long)(Math.random()*199)+1;
		long y = (long)(Math.random()*199)+1;
		GCD value = new GCD (x,y);
		System.out.println("gcd(" + x + ", " + y + ") = " + value);
	}
}

When I compile it gives me the following error:

GCDTest.java:9: cannot find symbol
symbol : constructor GCD(long,long)
location: class GCD
GCD value = new GCD (x,y);
^

Here is the class for Random Prime Number Generation:

import java.util.*;
public class RandomPrimeNos {
	public static long RandomPrimeNos () {
        long p = (long)(Math.random()*199)+1;
		boolean pIsPrime = false;
		long i=2;
		while(i<=(p-1) && pIsPrime==false){
			if(p%i==0){
				pIsPrime=false;
				p = (long)(Math.random()*199)+1;
				i = 2;
			} else {
				i++;
			}
			if(i>=(p-1)) {
				pIsPrime=true;
			}
		}
return (p);
    }
}

When I compile it, it works fine.

The code to test the Random Prime Number Generation class is :

import java.util.*;
public class RandomPrimeTest {
	public static void main(String[] args) {
		RandomPrimeNos randNum = new RandomPrimeNos ();
		System.out.println(randNum);
	}
}

It compile fine. but give me the following output instead of giving me a random prime number: RandomPrimeNos@c3c749


I hope someone can help me and tell me what I am doing wrong.
Thanks.

prelyptica
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

public class GCD have a static method gdc. Then you do not need to create new instance of this class .
simply use long val = GDC.gdc(l1,l2);

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

so ur saying der is an already built in function that does gcd ? and i can use it by just calling it ?

prelyptica
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
so ur saying der is an already built in function that does gcd ? and i can use it by just calling it ?

Yay I fixed it Thank u sooo much!

prelyptica
Newbie Poster
3 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

Simply, it's failing because you do not have a constructor that takes two longs.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: