A Square or Triangular number?

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.

 

​Android number guessing game!

In the continuing adventures of higher education and personal learning, I made a simple little guessing game for Android, as instructed by my online course for Android app development. It is a very basic game, where the user must guess the number Android is thinking of. You can download it here:
https://www.mediafire.com/download/rf7du2ysaddwos5

Android chooses a new number every time you open the app, so it has infinite replay value!

Here is the breakdown of the major components:
androidmanifest.xml

[CODE]

[/CODE]
main.xml

[CODE]

[/CODE]
mainactivity.java

[CODE]

package com.alaskalinuxuser.guessthenumber;
import android.app.*;

import android.os.*;

import java.util.*;

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

    }

Random appRandom = new Random();

int hidRandom = appRandom.nextInt(81) + 10;

public void takeGuess(View v)

{

EditText userGuess = (EditText) findViewById(R.id.myGuess);

Double uGuess = Double.parseDouble(userGuess.getText().toString());

if (uGuess hidRandom) {
Toast toast = Toast.makeText(getApplicationContext(), “Too high!”, Toast.LENGTH_SHORT);

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

toast.show();
} else {
Toast toast = Toast.makeText(getApplicationContext(), “That’s right, you guessed it!”, Toast.LENGTH_SHORT);

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

toast.show();
}

}

}

[/CODE]
As you can see, there is not much to this app. I was pleased that I was able to put it together in about 15 minutes (Praise God!), and, while simple, it is somewhat fun, if you’re board!
Linux – keep it simple.

​A currency converter app.

I never realized just how much goes into an app before trying to learn how to make one work! At the same time, I never realized how simple the individual pieces of the app can be.

 

Here is another app that I made for the course:

 

https://www.mediafire.com/download/npzaxxd0p0qzfw1
What I have been coming to understand is that looking at an app is a lot like building a puzzle. At first, you dump all of the pieces out of the box, and it looks overwhelming! Obviously, the scope of the app is similar to the size of the puzzle. If it has hundreds or thousands of pieces, it just seems impossible to put it together. But, as I learn more about making apps, I have come to realize that it is simpler if you break your puzzle down into groups, and then focus on one piece at a time.

 

For instance, if you sort your puzzle out by edge pieces, and put those together, then you can group center pieces by colors or objects from the box, and by working on just one area or group, you can get it done! You don’t have to figure out every piece at the beginning, you can instead focus on just one set of pieces, and everything will eventually hook together.

 

Similarly, with this currency converter app, I managed (after Googling) to take a number, and multiply it by another number from the user, and present it in a toast, per the instructions. I looked at the numbers, however, and realized that I didn’t want six+ decimal places, just 2. So, one piece at a time, back to Google, and now, praise God, it’s working!

 

You can check out the code here:

 

Here is my ANDROIDMANIFEST.XML

[CODE]

   

       

           

               
               

           

       

   

[/CODE]
Then my MAIN.XML

[CODE]

[/CODE]
Finally, my MAINACTIVITY.JAVA

[CODE]

 package com.mycompany.ustoau;
import android.app.*;

import android.os.*;
import android.app.*;

import android.os.*;

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.*;

import java.text.DecimalFormat;
public class MainActivity extends Activity 

{

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

public void convert (View view){

DecimalFormat precision = new DecimalFormat(“0.00”);

EditText usDollar = (EditText) findViewById(R.id.myDollar);

Double valueUs = Double.parseDouble(usDollar.getText().toString());

Double valueAu = 1.35;

Double calculatedValue = valueUs*valueAu;

Toast toast = Toast.makeText(getApplicationContext(), “You have: $” + precision.format(calculatedValue), Toast.LENGTH_LONG);

toast.show();

}

}
[/CODE]


Linux – keep it simple.

Making Toast

As I have mentioned previously, I am continuing my studies with an online course from Udemy by Rob Perceval about how to build Android apps. Here is my latest creation:

https://www.mediafire.com/download/1l8ba54llk4c2y4
It’s a simple app, but I am excited because it is the first truely interactive app that I have built. Not only can the user input something, but the user gets a reply from Android!

The course is really fun and, for the most part, intuitive. The course is designed around using Android Studio, but I have also been using AIDE, the Android equivalent.


It is a little less intuitive than using Android Studio, but it is still a great tool with auto complete and error checking. Not to mention, when I click run, to test my app, it builds the app and installs it on my phone! No emulator needed, and you know exactly what your app will look like when you hold it in your hands.

It is also great for working on the go. It even has a visual editor for layout designing.

Linux – keep it simple.

AIDE: Building Android apps on your phone!

After spending a little bit of time with my Android Studio app development course, it occurred to me that there must be a way to build Android apps on your Android phone. Five minutes on Google gave me more choices then I could shake a stick at! So, I tried a few out and came to this conclusion: at this time AIDE is my favorite.

I like to hope that I make educated decisions, and below are my reasonings.

1. It is free. Well, there are in app purchases for more learning tools and perhaps more features, but for my basic skill level, it can do everything I need whilst being free.


2. I can load my Android Studio projects and run them. That’s right, I can save my files from my laptop and pick up right where I left off on my phone. While Android Studio does have a built in emulator, this takes it to a whole new level when you can hold your homemade app in your hands and really see how it feels.


3. Github integration. Do you want to fork a github project? Or need to download an open source app on github for a base? Perhaps your own app is on github, and you use that as a way to sync between your desktop and phone. Either way, it is great to punch in an URL, and start working on that app’s code!


4. A built in teaching program. Granted, the built in program does not compare with a Rob Percival course, but the free portion was fun, short, and interactive. I did not try the longer paid portion, as I am keeping my hobbies on a budget, but if it is as interactive as the free portion, then it will likely be a help to new comers like myself.


5. A simple user interface. The interface is very simple and to the point. That is nice these days, with so many “overcrowded” apps. The focus is on content, and there are all the basic tools, a design view, code view, file manager of sorts, etc.


If you are into dabbling with Android apps as a fellow nonprofessional like me, I recommend giving AIDE a try.

Linux – keep it simple.