The number is given and we need to find if it is a square number or triangle number? This "num" should 1st verify the square method then it has to go for triangle method.
there issue is: how can i call from square to triangle or any other methed to call the Triangle method if my square method won't suit.
Here's my code:
public class HelloWorld{
public static void main(String []args){
class Number{
int num;
public boolean isSquare(){
int squareNumber=1;
while(squareNumber<num){
squareNumber = num*num;
}
if(squareNumber%num==0)
{
System.out.println(num+" is a Square number");
}
else
{
return isTriangle();
}
boolean isTriangle() {
int x=1,triangleNumber=1;
while(triangleNumber<num){
x++;
triangleNumber = triangleNumber + x;
}
if(triangleNumber==num)
{
System.out.println(num+" is a triangle number");
}
else
{
System.out.println(num+" is applicable for both triangle and square numbers");
}
}
}
}
Number mynum = new Number();
mynum.num=2;
System.out.println(mynum.isSquare());
}
}
So first it would be much easier for you to just do this:
public boolean isSquare(int num){
...
}
And so get rid of:
mynum.num = yourNumber;
As very often you will encounter this approach in methods, where the raw input is can be any number/text...
Now for your code, a very good optimization is:
public boolean isSquare(int num){
if (num>0) //To check if the number is null or not...
squareNumber = num*num;
}
Because the while loop condition is always met once and then exited after the first entry...
Moving on to the next part... which for me, is very tricky as I see it useless because it always returns true (the text) as the "squareNumber" always has num as root, and will always pass if (squareNumber%num==0) as true, and isTriangle(...) will never be called.
The more approachable way is:
public class HelloWorld {
static String result;
public static void main(String[] args) {
int mynum = 36;
System.out.print(isSquare(mynum)+isTriangle(mynum));
}
public static String isSquare(int num){
if ((Math.sqrt(num)==(int)Math.sqrt(num))){
return "Given number is a square";
} else return "Given number isn't a square,";
}
public static String isTriangle(int num){
if (isSquare(8*num+1)){
return " but is still a triangular number!";
} else return " but not a triangular number.";
}
}
public static void main(String []args){
class Number{
int num;
public boolean isSquare(){
int squareNumber=1;
while(squareNumber<num){
squareNumber = num*num;
}
if(squareNumber%num==0)
{
System.out.println(num+" is a Square number");
}
return isTriangle();
}
boolean isTriangle() {
int x=1,triangleNumber=1;
while(triangleNumber<num){
x++;
triangleNumber = triangleNumber + x;
}
if(triangleNumber==num)
{
System.out.println(num+" is a triangle number");
return true;
}
else
{
System.out.println(num+" is applicable for both triangle and square numbers");
}
return false;
}
}
Number mynum = new Number();
mynum.num=2;
System.out.println(mynum.isSquare());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.