Tuesday, May 26, 2015

Final Project - Secret Messager

Download the application here.

This program stems from the Braille translator I made a month or so ago, and utilizes a similar kind of array system to sort character information.

Basically, to "encode" a user's input, the encoder class will create a randomized alphabet (including numbers and symbols), switch each character in the user input with its randomized character, put that into the output, and then stick the full randomized alphabet at the end of the output for the decoder to use. Since this is randomized each time, it would be relatively difficult to crack under normal circumstances if you don't know the format of the original non-randomized alphabet array.

To "decode" the encoded message, the decoder splits the encoded message into the actual user's input and the randomized alphabet that was stuck at the end (it is able to do this because the length of the randomized alphabet doesn't change). It then basically does what the encoder did, but in reverse - flip every mixed up character with the original alphabet, essentially unscrambling the message.

Here's the program in action:





The decoded message, as of v3.1, will now also copy to the clipboard.


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!

Monday, March 16, 2015

Pig Latin Translator v1.0

Finally got the pig latin translator working properly (mostly). It accounts for special cases like qu, ph, ch, and sh. However, I have not yet implemented the ability to search for vowels past the first two characters. This requires a lot of extra booleans and reformatting my entire code. Here's the current code, nonetheless:

import javax.swing.JOptionPane;

public class NoPleaseStop {

 public static void main(String[] args){
  String user_input = JOptionPane.showInputDialog("Enter a word to 
translate:").toLowerCase();
  String[] vowels = {"a", "e", "i", "o", "u"}; //array of vowels
  String result = ""; //creates empty string for final translation
  boolean check = false; //Used for vowel/consonant rules
  
        if (isAlpha(user_input) == false) { //A little code Brian 
showed me, checks for proper notation
            JOptionPane.showMessageDialog(null, "No extra symbols 
or sentences, please. Just one word.", "USER ERROR", JOptionPane
.INFORMATION_MESSAGE); //Explains user error
        } else if (user_input.startsWith("qu")) { //Here's some 
extra rules
         result = user_input.substring(2) + "quay";
         JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ph")) {
         result = user_input.substring(2) + "phay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("sh")) {
         result = user_input.substring(2) + "shay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ch")) {
         result = user_input.substring(2) + "chay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else {
          for (int i = 0; i < vowels.length; i++) {
           if (user_input.startsWith(vowels[i])) {
            check = true;
           }
          }
          if (check) {
           result = user_input + "yay"; //vowel rule
          } else {
           result = user_input.substring(1) + user_input.charAt(0)
 + "ay"; //consonant rule
          }
          JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
      }
 }
 
    public static boolean isAlpha(String user_input) {  
//The other bit of the code Brian showed me
        char[] chars = user_input.toCharArray(); 
//It checks for correct characters!
        for (char c : chars) {
            if(!Character.isLetter(c)) {
                return false;
            }
        }
        return true; 
    }
    
}
 
Sorry for the poor formatting, Blogger doesn't like code. Here's some example inputs/outputs:







Wednesday, March 4, 2015

Basic Battleship v1.1

I edited the code of my basic Battleship program to include option panels as opposed to writing directly in the console. The main things that were added were:

import javax.swing.JOptionPane; //Function used to create panel

/*
 * Pretend the rest of the code is here, nothing was changed
 */

String guess = JOptionPane.showInputDialog("Enter your guess here (0-7): ");  
//Creates a window where the user can input their guess

/*
 * Rest of the code goes here
 */

The remaining code from v1.0 was unchanged and worked smoothly. I want to add a window to display whether or not the user's guess is a hit or a miss, but that goes more into JFrames and JPanels and UI that I'm assuming gets covered more in Chapter 6. I just wanted to make sure I understood JOptionPane for this activity.