4 Replies - 1848 Views - Last Post: 29 July 2011 - 04:57 PM Rate Topic: -----

#1 yakult02  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 26
  • Joined: 04-December 09

Return string value if the number is not a perfect square

Posted 28 July 2011 - 09:44 PM

Im confused how to return or implement a string value if a number is not a perfect square,
I used math.sqrt()

package javax.swing;

import javax.swing.JOptionPane; 
public class Exer6_02 {

	/**
	 * @param args
	 */
	public static String squareRoot( int n ) {
		
		String message = null;
		String input = JOptionPane.showInputDialog("Input a perfect square number: ");
		int sq = Integer.parseInt( input );
		
		if ( sq < 0 ) {
			JOptionPane.showMessageDialog(null,"Invalid input.");
			System.exit(0);
		}
		
		int sRoot = (int)Math.sqrt(sq);
			message = "The square root of " + sq + " is " + sRoot;
		// i tried putting some codes here but still errors so i deleted it
			
		return message;
			
	}
	
	public static void main ( String args[] ) {
		
		int x = 0;
		JOptionPane.showMessageDialog( null, squareRoot( x ));
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Return string value if the number is not a perfect square

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Return string value if the number is not a perfect square

Posted 28 July 2011 - 10:59 PM

Do an if-else statement and use the modulus operator. Check if x modulus 1 is 0. If it is, it's a perfect square. If not, then it ain't. Print the appropriate messages. Plus Math.sqrt takes Double, so use Doubles.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2694
  • View blog
  • Posts: 11,360
  • Joined: 20-September 08

Re: Return string value if the number is not a perfect square

Posted 29 July 2011 - 02:23 AM

e.g.


	int n = 4;
	int root = (int)Math.sqrt(n);
	if ((root * root) == n) {
	    System.out.printf("%d is a perfect square\n", n);
	}


Was This Post Helpful? 2
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Return string value if the number is not a perfect square

Posted 29 July 2011 - 03:07 AM

View PostfromTheSprawl, on 29 July 2011 - 08:59 AM, said:

Do an if-else statement and use the modulus operator. Check if x modulus 1 is 0. If it is, it's a perfect square. If not, then it ain't. Print the appropriate messages. Plus Math.sqrt takes Double, so use Doubles.


hmm anything modulu 1 will be 0, so that won't work.
Was This Post Helpful? 2
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Return string value if the number is not a perfect square

Posted 29 July 2011 - 04:57 PM

If you are working with doubles, the modulo can return fractions so if the root function gives 1.141 then 1.141 % 1 = 0.141.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1