Thursday, March 19, 2015

Tree

"Tree"


It's funny, 'cause the code branches

Thursday, March 12, 2015

Anagram creator / Recursion

Earlier, our teacher chalenged us to write a program that outputs a list of anagrams given an input string. A bit later, he said "On second thought, it might be too hard, considering we haven't learned the skills needed yet."

WELL, I DID IT ANYWAYS.


This program uses some serious recursion to return a list of anagrams. For example, "yes" prints out "yes", "yse", "eys", "esy", "sye", and "sey". If the boolean useAllCharacters is set to false, it will print out the string at each step, meaning it will also print anagrams using only some of the characters. For example, "hi" will print "h", "hi", "i", and "ih".

The recursion function works like this- given a string and a list of characters, it will go through the list and each time, call itself with the original string plus a character from the list and the list of characters without the character added to the string. If the list is empty however, it will simply print the completed string, and end without calling itself. More visually demonstrated-


Wednesday, March 11, 2015

Rapid Counter app- available here!

Rapid Counter
an Android app by Ché Young

       To test out and better understand the Android app development and submission process, I have created this app, Rapid Counter. It is designed to be an ergonomically and aesthetically pleasing counter, with buttons to subtract as well as add to the count, along with a reset button and sounds.
     I would have this submitted to the Google Play store... except for the fact that the $50 signup fee is too high for me to comfortably pay for the moment. However, you can download it here instead. You will likely need to go into your settings to allow installing apps from non-market sources.

Regardless of whether or not you liked it, feel free to give me feedback!
Any input would be much appreciated.

Friday, March 6, 2015

Pig Latin converter

So I made this pig latin translator in class today. It doesn't handle capitalization or punctuation, but it can handle multiple words at once, separated by spaces.

import javax.swing.JOptionPane;

public class home {
static String[] vowels = {"a", "e", "i", "o", "u", "y"};
public static void main(String[] args) {
String input;
String out;
boolean x = true;
while (x) {
input = JOptionPane.showInputDialog("Words to change?");
if (input.equals("stop")){
x=false;
break;
}
out = "";
for (String ret: input.split(" ")){
int vpos = find(ret);
System.out.println(vpos);
if (vpos > 0) {
out = out + ret.substring(vpos) + ret.substring(0, vpos) + "ay ";
} else {
out = out + ret + "way ";
}
}
JOptionPane.showMessageDialog(null, out);
}
}
public static int find(String inp) {
int pos = -1;
for (String vwl: vowels) {
int vpos = inp.indexOf(vwl);
if ((vpos >= 0) && ((vpos < pos)||(pos==-1)) && (vpos != inp.indexOf("qu")+1)) {
pos=inp.indexOf(vwl);
}
}
return pos;
}
}


Monday, March 2, 2015

Age checker

Tim and I made a java program that checks to see if you're 18 years old or older.

import javax.swing.JOptionPane;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class home {
public static void main(String[] args) {
int day = 0;
int month = 0;
int year = 0;
boolean workingstr = false;
while (workingstr == false) {
String inputValue = JOptionPane.showInputDialog("Please type in your birthday (mm/dd/yy)");
workingstr=true;
try {
day = Integer.parseInt(inputValue.substring(4,5));
month = Integer.parseInt(inputValue.substring(0,2));
year = Integer.parseInt("19"+inputValue.substring(inputValue.length()-2,inputValue.length()));
} catch (NumberFormatException e) {
workingstr=false;
} catch (StringIndexOutOfBoundsException e) {  
workingstr=false; //This section of the program will repeat if any errors are thrown trying to convert the
}                                      //input string to integers.
}
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cd = Calendar.getInstance();
int todayDay = Integer.parseInt(dateFormat.format(cd.getTime()).substring(0,2));
int todayMonth = Integer.parseInt(dateFormat.format(cd.getTime()).substring(4,5));
int todayYear = Integer.parseInt(dateFormat.format(cd.getTime()).substring(6,10));

if (day<=todayDay) {
month++;
}
if (month<=todayMonth){
year++;
}
if (year+18<=todayYear) {
System.out.println("You're good.");
} else {
System.out.println("Nope! You're underage.");
}
}
}

Friday, February 13, 2015

Chapter 5

For a while, I've been working on a little project called "Infinite Mario Bros". Looks pretty good so far.
Some of Chapter 5's content was actually surprisingly informative and helpful.
Consider this bit of code-
I made this before I discovered that there were two formats for for loops;
for (int i=0; i < Main.mainPanel.Enemies.size(); i++) { Main.mainPanel.enemies.get(i) }
for (Enemy test : Main.mainPanel.enemies) { test }

Using the new for loop, my code is easier to modify and looks much cleaner overall, because instead of typing "Main.mainPanel.enemies.get(i)" each time, i can just type "test".







And if you're wondering what this code does, it goes through a list of enemies, checks if they're overlapping with Mario, and if he's falling, change their state. In other words...
Goomba goes squish.

Tuesday, February 3, 2015

Chapter 4

In Chapter 4 of Headfirst Java, I learned about Instance and Local variables, and how they act. Instance variables are declared as part of the class and have default values, but Local variables are defined within a method and have no initial value. For example...

public class Dog{
    public int size;
    public void printSize() {
        System.out.print(size);
    }
}

Calling the printSize() method on this Dog class will return 0, since the default value of ints are 0. However, you can't do this...

public class Dog{
    public void printSize() {
        int size;
        System.out.print(size);
    }
}

This code won't compile because the size variabe is Local and has no default value.

I also learned that there are two different ways to compare variables, as I discovered in the cat simulator program. The == operator  compares variables on a binary level, meaning the variables have to be exactly the same values and be in the same "heap", whereas you can use .equals() to compare similar objects from different heaps (such as strings with the same characters).