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.

Recommended Answers

All 4 Replies

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);

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

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!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.