need help to add main class to test this program.

public class Rational
{
	private int num;
	private int den;

	public Rational (  )
	{
		num = 0;
		den = 1;
	}

	public Rational (int a, int B)
	{
		num = a;
		den = b;
		simplify ( );
	}

	private void simplify ( )
	{
		int GCD = getGCD (num,den);
		num /= GCD;
		den /= GCD;

	}


	private int getGCD ( int a, int b )
	{
		int dividend = Math.abs (a);
		int divisor = Math.abs (B);
		int rem = 0;

		do
		{
			rem = dividend % divisor;
			if (rem != 0)
			{
				dividend = divisor;
				divisor = rem;
			}
		} while (rem != 0);

		return divisor;
	}

	public double getValue ( )
	{

		return (double) num / den;

	}

	public String toString ( )
	{
		String outString = "";
		if ((num * den) < 0)
			outString += "-";

		outString += (Math.abs (num));

		if (Math.abs (den) != 1)
			outString += ("/" + Math.abs (den));

		return outString;
	}
}

Recommended Answers

All 8 Replies

What code is in the Main class? Will the Main class be an inner class?
Or is the Main class the class with a main() method that will create instances of the Rational class and call its methods.

There is no need to create a separate class just to hold the main() method that you are using for testing. You can add it directly to the Rational class.

What code is in the Main class? Will the Main class be an inner class?
Or is the Main class the class with a main() method that will create instances of the Rational class and call its methods.

There is no need to create a separate class just to hold the main() method that you are using for testing. You can add it directly to the Rational class.

i m trying to create main class to ask users enter rational no.

What will your Main class use the Rational class for?

What will your Main class use the Rational class for?

to test the result.

What are your questions or problems?

I m trying to add scanner class to ask the users to type rational no.

NormR1 is repeating "Main class" for a reason. You don't want a Main class. You don't need a Main class. As he already told you, you need to implement a main method (and yes, there is a big difference).

no matter what course you are taking, or what book you are using to teach yourself how to program in Java, they all cover the main method before they start with OO principles.

So, if the code above is written by you, it is safe to assume you (are supposed to) know how to implement a main method. The next step for you now is: try to implement the main method, and if it doesn't work, show us what you tried and ask for help understanding your errors. We don't mind people making silly mistakes, yet we do mind people who are to lazy to make silly mistakes. By trying you'll learn a lot more than by being given code, trust me on that.

what you need is a testing framework like jUnit.

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.