Pages

Thursday, January 19, 2012

Finding Prime Numbers in Java

I was practicing some Java problems the other day; more specifically, I was practicing nested loops. These things are not the easiest to keep track of, but I feel I'm getting better at writing pseudocode and planning the code first instead of just throwing code all over the place! Some exercises I worked on included:

  • Creating a standard 31-day calendar
  • A program that allows the user to input a number, which will then be the dimension for two squares - one empty and one filled (with asterisks)
    • Example: if user types "3," the program spits out: 
    • *** ***
    • *** *  *
    • *** ***
  • A program where the user inputs a number, and the program will print all prime numbers within given number
    • Example: if user types "10," the program prints:
    • 2
    • 3
    • 5
    • 7
That last one gave me the most trouble! Of course, the troubling part was figuring out if a number is prime. Actually, even that wasn't so hard (after I figured it out). It ends up that I somehow forgot that in an if-then statement, not all conditions are checked for truth: as soon as one condition is true, the statements are executed and then it exits!

Put simply: I misused the if-then statement ^_^
Oops!


My excuse is that it was late at night, and I wasn't thinking clearly. :)

Anyway, here is the code I came up with:

//These are possible prime numbers up until the number inputted
for (int prime=2; prime <= input; prime++)
{
      //These are divisors to check if the number is indeed prime
      int divisor;
      for (divisor = 2; divisor <= prime; divisor++)
      {
            //if prime/divisor has remainder of 0
            if (prime % divisor == 0)
           {
                  //AND if prime and divisor are the same number, then it's prime
                  if (prime == divisor)
                  {
                           System.out.println(prime);
                  }
                  //otherwise, it's not. Break out of if-then loop.
                  else
                  {
                            break;
                  }
            }
      }
}

Wednesday, January 11, 2012

How To: Connect to Ymail on an Android Device

I recently had trouble adding a Ymail account to the default email app that comes with the Android device I was using. However, I had no trouble at all adding a regular Yahoo account and signing in. After looking at the settings during manual setup, I figured out that the reason Ymail wasn't working (that is, it wasn't adding the account) was because the app was changing Ymail's settings to be different than Yahoo's settings, when in fact Ymail and Yahoo should have the same exact settings. It is the same company, after all.

So for anyone else having trouble adding a Ymail account and who doesn't want to add another app, just change the settings in manual setup to the same as the Yahoo settings. Here are the steps:


Thursday, January 5, 2012

How To: Create a Fill-in-the-Blank Game Online


This post will [attempt to] teach you (assuming you already have a website) how to code a game similar to the popular Mad Libs. Why would you want to do this? Simple - because Mad Libs is fun (when you choose the right words anyway ;) )!

"So, first, what do I need exactly?"
Well, young one, you must have a place to type your code (like Notepad or a site editor) and a way of viewing typed code. Preferably, you have a website in place.

"That's easy! I already--."
Wait a minute, I'm not finished! You also need to be able to use PHP.

"Awwh :(, my host doesn't allow PHP."
I'm sorry :(

"Just kidding!"
Yay!

Now, onto business.


Tuesday, January 3, 2012

Project: Fleece Blanket

For Christmas, I made a big, warm, fleece blanket for a certain baby. He's really tall for his age, so I wanted to make the blanket huge. I believe I made it 48 x 60, give or take a few inches.

As you can see, I made it two-sided - one side frogs and one side monkeys! It is very soft and very warm - perfect for winter! Being still a beginner in sewing, it didn't come out perfect, of course. You can see in the picture that the edge of the frog side shows up a bit in the monkey side ^_^. That's what I get for not investing in some chalk, right?

I was originally going to do it the lazy way, which requires no sewing at all (just cut some strips along the border, tie them together, and you're good to go - search "no sew fleece blanket" to know what I mean). But I really don't like how it looks. Sure, it's easy, but it's not pretty!

Anyway, I like how it came out, even if it is a bit crooked! Hopefully my next one will have straighter edges!