Sunday, April 26, 2015

Braille Maker

I'm just uploading this for my own sake; it has nothing to do with recursion or web development. I made a program that translates what you type into Braille, with a chunk of commented-code at the top that explains how Braille works. It mostly utilizes nested for-loops.

You can find this code here.

If some of the characters in the Braille arrays look funny, or just show up as boxes, it's probably because whatever browser/computer you're using does not support Unicode Braille patterns. The program itself should still technically work, however.




Here's that output blown up a bit to improve readability:
"Hello, world!", in Braille, is: ⠠⠓⠑⠇⠇⠕⠂⠀⠺⠕⠗⠇⠙⠖

There are numerous online converters for Braille that can be used to check my program, and it should work for most normal characters.

Tuesday, April 21, 2015

Another recursion post - Character Search!

This is related to another CodingBat recursion problem. Specifically, this one.

Basically, it recursively searches through a number to find a specific digit, which, in this case, is 7. It then counts how many times that digit is found in the number, if any (e.g. 127 would return 1).

I made this into a program that, instead of just searching through numbers for a given digit, can search through any sort of string for a given character (which, in the program, is technically just a string of length 1). While the task could be accomplished with a for loop, I decided to keep with the format of the CodingBat problem, only changing a few if statements to deal with strings as opposed to numbers.

The program can be found here.

Interestingly, it was easier to comprehend the recursive function of the program in terms of strings than it was with numbers. For example, getting the last digit in a number requires the modulo operator and the number 10. So, 126 % 10 = 6.  When doing the same thing with strings, all you need is a substring - specifically, a substring from str.length() - 1 to str.length()

Similarly, to get a number to change to all the digits but the final one, you have to divide by 10, and be sure to avoid floats/doubles: 126 / 10 = 12. With substrings, you just need to go from 0 to str.length() - 1

All of these transformations are needed in the recursive function. After reformatting it for strings and implementing a user input system, the program was pretty much complete. I had a little trouble getting multiple inputs to work (so the user can input the character to search for and the string to search in within the same window), but found some help online.

Here's the program in action:



------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------



An interesting anecdote - while testing this program at my house, I tried to input a large document from Wikipedia into the righthand box. While it technically ran correctly, it ended up forcing my computer into Windows 7 Basic Desktop mode, as opposed to Aero Desktop, and didn't go back until I closed the program. So that's weird.

Friday, April 17, 2015

Some findings on basic recursion and data type limits.

Going off of CodingBat's first recursion problem, factorial (which can be found right here), I implemented the recursive method into Eclipse and made a working program out of it.

You can view/download the program by clicking here.

While making this program, I realized some interesting things about doing factorials in Java. The main problem is that, if you only use the int data type, the highest number the program can successfully compute the factorial of is 12. Anything past that is computed either negative, incorrect, or just plain old 0

When I changed everything to long, it became possible to calculate factorials up through 20. Anything past that continues to be calculated incorrectly. For example, inputting 21 causes this to happen:




Just goes to show how huge factorial numbers are!

EDIT: Just added a bit of code that splits numbers into groups of three, like this:


All it took was a little imported function.

Monday, April 13, 2015

String[] topic = {"Arrays", "101"};

This unit was all about arrays, which are simply "lists" of values.

Arrays can consist of integers, strings, booleans, object references, you name it.

There are two main ways to create arrays, like this:

int[] grades = new int[10];

Which would create an empty int array of length 10, or:

String[] veggies = {"Cucumber", "Corn", "Lettuce"};

Which would create a defined String array, length three, consisting of Cucumber, Corn, and Lettuce.

Arrays are indexed starting at 0, meaning that Cucumber is at index 0. You can call a value from an array like this:

return veggies[1];

Which would return "Corn", since the array starts at 0.

You can find the length of an array similarly to a String, but instead of .length(), we remove the parentheses and just use .length:

return veggies.length;

This returns 3.

Going back to that first array I made, an int array called grades of length 10,  I can pass in some values for the empty array.

grades[5] = 99;

Now the fifth index, or sixth value, is 99.

Arrays are often used in for loops, like this:

for (i = 0; i < veggies.length, i++) {  //i will be 0, 1, and 2
  return veggies[i];   //Cycles through array and returns each value
}

or, this:

for (int i : veggies) {  //Same thing as before, but simplified
  return veggies[i];
}

You can also make use of multi-dimensional arrays, which work like matrices - rows and columns. But since this is for the "1 Dimensional Array Merit Badge", I won't go into it much here.

Basically, it lets you create an array that looks like this:

[ 0, 25,  3]
[ 1,  3, 53]
[ 3, 74,  8]

Which has three rows and three columns. Rows are horizontal whereas columns are vertical. To find the index of a particular value, you would use the format (row, column). So, that 74 right there? It would be (2, 1) because it's in row 2 (which is the third row, but remember that the index starts at 0) and column 1 (which is the second column). 

Hooray!