Doesn’t sound like a very useful tool to me, but it was the app I was required to build as part of my course. Here’s the download if you want to give it a try:

http://www.mediafire.com/file/3ee5m33ynhcxasw/SquareOrTriangularNumber_1.0.apk

While not very complicated for the seasoned programmer, it was the most difficult app to build that I have built so far, which is good, since the course should progress and force me into harder and harder to solve problems.

Overall, the complicated part was the math involved with checking for a square root or not. Fortunately, I found an article on StackOverflow to answer that. I even added a note about it in my app, so that others would know where I got the math function from. Then there was the series of if/then statements, which wheedled the answer down for the appropriate toast pop up. I learned a lot, and here is what I did:

[CODE]

package com.mycompany.sotn;

import android.app.*;

import android.os.*;

import java.util.*;

import android.widget.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;

public class MainActivity extends Activity

{

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

class MyNumber {

int chosenNumber;

public boolean zeroNumber() {

if (chosenNumber == 0) {

return true;

} else {

return false;

}

}

public boolean squareNumber() {

// found method to check for square from Jaskaranbir Singh on StackOverflow

double sqrt = Math.sqrt(chosenNumber);

int x = (int) sqrt;

if(Math.pow(sqrt,2) == Math.pow(x,2)) {

return true;

} else {

return false;

}

}

public boolean triNumber() {

// 8x + 1 is a square number then the number is triangular, per wikipedia.

// So let’s get that number.

int mathNumber = (chosenNumber*8+1);

// And check if it is a square number.

double sqrt = Math.sqrt(mathNumber);

int x = (int) sqrt;

if(Math.pow(sqrt,2) == Math.pow(x,2)) {

return true;

} else {

return false;

}

}

}

public void onClick (View v) {

EditText userQuestion = (EditText) findViewById(R.id.userNumberField);

if (userQuestion.getText().toString().equals(“”)) {

Toast toast = Toast.makeText(getApplicationContext(), “Please enter a number!”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

} else {

MyNumber thisTime = new MyNumber();

int myNewNumber = Integer.parseInt(userQuestion.getText().toString());

thisTime.chosenNumber = myNewNumber;

if (thisTime.zeroNumber()) {

Toast toast = Toast.makeText(getApplicationContext(),thisTime.chosenNumber + ” is not a Triangular number, but is a Square number.”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

} else if ((thisTime.squareNumber()) && (thisTime.triNumber())) {

Toast toast = Toast.makeText(getApplicationContext(),thisTime.chosenNumber + ” is a Triangular and a Square number.”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

} else if (thisTime.squareNumber()) {

Toast toast = Toast.makeText(getApplicationContext(),thisTime.chosenNumber + ” is a Square number.”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

} else if (thisTime.triNumber()) {

Toast toast = Toast.makeText(getApplicationContext(),thisTime.chosenNumber + ” is a Triangular number.”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

} else {

Toast toast = Toast.makeText(getApplicationContext(),thisTime.chosenNumber + ” is neither a Triangular number, nor a Square number.”, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER, 0, 0);

toast.show();

}

}

}

}

[/CODE]

First, I practiced relentlessly the if/then statements on http://www.browxy.com/ to make sure that the java code worked. It is a web browser based application that lets you input java code and run it. With this website, I could run dozens of iterations without having to compile my app every time. It worked rather well for the quick check, then I just needed to add that code to AIDE, my Android IDE compiler, and build the “app” around the code.

Be sure to give it a try! However, I think only math wiz’s will find this tool remotely useful. I did double check the math from 1 to 36, which seems to be accurate. I am not sure if this will work on really large numbers or not, but I did try a few 6 digit numbers from a truth table I found online, and it was accurate. I was wondering how large a number would be too large, so I Googled it. Java int numbers can go up to 2,147,483,647, which the app says is neither a triangular nor square number, and adding 1 to that value forces the app to crash. Interesting.

Linux – keep it simple.