Posts

Showing posts from 2013

Graphics Programming

I've been getting a few questions about Java graphics: "Do you know of any good graphics tutorials for java? I am working with one that might work, but any suggestions you have would be great." Below are some links that I found useful:   http://docs.oracle.com/javase/tutorial/2d/index.html http://www.corewebprogramming.com/PDF/ch10.pdf http://www.deitel.com/articles/java_tutorials/20050923/IntroductionToJava2D.html   Java has an advanced graphics 2D API . It is quite sophisticated and might be daunting for beginners who just want do something fun. I found that for casual programming, Processing is more approachable and you can write a fairly complex program with it. Also, one of the most popular Java client side graphics libraries is Android (of course, it's not really Java at runtime on the device).  

Young Coders: Week 4 Problems

The Final Challenges! 1) Create a method called between. Its parameter is an int array. It will return the maximum distance between any two equal values.  For example: between(new int[] {1,0,4,2,5,0,0,5}) should return 6, because the leftmost 0 and the rightmost 0 are 6 elements apart (including themselves). 

Young Coders: Week 4 Notes

Image
Sorting and Searching Bubble Sort, Insertion Sort, Selection Sort Linear Search, Binary Search How to swap two elements We first looked at how to swap two elements. Let's say we have  int a = 2; int b = 3; and we want to switch their values, so that a = 3 and b = 2.  Remember, we can't just say: a = b; b = a; As we saw on the whiteboard yesterday, this will make both a and b equal to 3, instead of switching their values: 

Young Coders: Week 3 Problem

EDIT: Just one extra Candy Challenge problem this week!  The full Candy Challenge = the following problem + two Candy Challenges from previous classes.  For those of you who have finished all the previous Candy Challenges, pick two problems from Week 2's 3,4, and 5, and try to solve them iteratively (with loops),  instead of recursively.  Write a toString() method for our Queue class that prints out its elements.  For example,  public static void main(String[] args) {    Queue animals = new Queue();           animals.enq("Tiger");     animals.enq("Pig");     animals.enq("Zebra");          System.out.println(animals);  } will give us {"Tiger", "Pig", "Zebra"} , not the hashCode() we have now (something like Queue@56ee20fe).  (An example: Look at the outline of a toString() method we wrote in the Pet class.) 

Young Coders: Week 3 Notes

Image
Yesterday, we learned about classes and some data structures.  Classes A “specification” or “blueprint” that says how to construct an object. Here is the Pet class we made:  public class Pet {  //fields  public String name;   public String color;  public int legs;   public int speed;   private int pinNum;   //constructor  public Pet (String name, String color, int legs, int speed) {    this.name = name;    this.color = color;    this.legs = legs;    this.speed = speed;     pinNum = 1111;   }  //methods  public int getPin() {   return pinNum;    }  public void setColor(String color) {    this.color = color;   }  public void exercise(int increase) {    speed += increase;      color = "red";   }  public void sleep() {    speed = 0;     color = "blue";   }  public void wakeUp() {    speed = 5;    color = "green";   }  public String toString() {    return "this works";   }  public static void ma

Interactive Data Story of Forbes Top 100 Celebrities

Image
When looking at the  table of "The World's Most Powerful Celebrities" , compiled by Forbes, it is hard to get insights like the relationships between celebrities and the factors they are ranked by.  I decided to create a project  using D3.js  to do some exploration.  In the project I tried to find how the celebrities' social networking power related to their marketability. I grabbed the data from  Forbes . I added categories (Actors, Actresses, Athletes, etc) so certain groups can be compared with each other. Hovering over the points revealed their names and jobs. (Labels by each point would be too cluttered.)  Here is the story from the data ...

Young Coders: Week 2 Problems

Candy Challenge! Finish at least 3 out of 5 of the following problems correctly to win candy/chocolate. Email your solutions to me, plus two or three of your favorite types of candy/chocolate. 1) Using a Scanner, read in 10 integers into an array. Print out “positive” if most of them are positive. Print out “negative” if most of them are negative. Print out “balanced” if five of them are positive and five of them are negative. (Assume none of the integers are zero.)  2) Read in 5 integers into an array using a Scanner. Print out the numbers shifted left by one.  For example, {1,2,3,4,5} becomes {2,3,4,5,1}  (EDIT: Please solve 3, 4, and 5 recursively!)  3) Create a method called power that takes two parameters: base and exp. It returns the value of base to the exp power. For example: power(2,3) = 8 power(5,3) = 125 power(4,0) = 1 4) Create a method called remove that takes one parameter: a String. It returns the same String, except all the ‘o’ or ‘O’ characters have be

Young Coders: Week 2 Notes

Image
Loops, Arrays, Methods, and Recursion Loops If statements are one way to control how your program flows. Loops are another way. When you need the computer to execute code over and over, use loops! We looked at two types of loops.  A while loop won’t stop until its condition(s) is false. public class WhileLoop1{     public static void main(String[] args) {        int i = 1 ;        while (i <= 10 ) {          System.out.println(i);           i++;        }     } }

Young Coders: Week 1 Problems

Candy Challenge! Finish at least 3 out of 5 of the following problems correctly to win candy/chocolate. Email your solutions to me, plus two or three of your favorite types of candy/chocolate. If you have any questions about the problems themselves, ask in the comments below this post. 1) Read in two Strings using a Scanner. Print them out in an alternating pattern. For example, if the user inputs “hot” and “cold”, print out “hotcoldhotcold”. 2) Read in a String using a Scanner. Print out the first three letters. If the String’s length is less than three, just print out the String. 3) Read in a String using a Scanner. Print out the second half of the word. For example, if the word is “hooray”, print out “ray”. Assume the word’s length is an even number. 4) Read in a String using a Scanner. Print out the word without its first and last letter. For example, if the word was “computer”, print out “ompute”.

Young Coders: Week 1 Notes

Image
We started out by learning how to declare and initialize a variable in Java:  We then looked at if statements: 

Parallel Summing Without Threads

Image
Calculating the sum of an array of numbers is a simple task:       sum = 0;        for each number in an array           sum += number;  However, summing is an important primitive function of databases and other applications, and needs to be sped up when the size of the array increases to a huge number such as 100M+. Using threads is probably the first impulse since this is such an easily parallizeable problem. But for a smaller array, such as one containing 10 numbers, multi-threading will make a summing function slower than the naive version because of the extra resources used. So, how can a summing function be implemented so that it is always faster than the naive version? To answer that question, let's first understand how the machine works.  Memory Hierarchy

DirectByteBuffer in Java

Image
MappedByteBuffer is a way to read and write binary files directly in Java. It allows you to address file data at the bytes level, so it is convenient and potentially high performance. It maps a file region into the virtual address space of the Java process. Here's a simple example of how to use it (also on GitHub ): public class MappedByteBufferTest {        public static void main(String[] args) throws IOException {            //WRITE FILE            int len = 200;            RandomAccessFile file = new RandomAccessFile("output.txt", "rw");                    MappedByteBuffer outputBuffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, len);            for (int i = 0; i < len; i++)                outputBuffer.put((byte) i);            //READ FILE            FileInputStream inputStream = new FileInputStream("output.txt");            MappedByteBuffer mappedByteBuffer = inputStream.getChannel().map(FileChannel.MapMode

MovieSieve

Image
Bubble Chart, by genre's popularity at the box office My family enjoys watching movies together at home. It takes us a long time to find a movie we all want to watch. In iTunes, Rotten Tomatoes, IMDB, and Netflix, all the movies are tabulated in flat lists. It would be easier to browse through hundreds of movies by viewing all the movie titles, release dates, popularity rankings, and genres in one place. This inspired me to create  MovieSieve , an interactive chart that helps people find movies. I focused on making it clear, simple, and informative. My prototype uses  D3.js  and data scraped from  IMDB . The bubble chart shows the popularity of each genre. It is based on what all the movies of that genre have grossed at the box office. Clicking on a genre's bubble generates a TreeMap of the genre, colored by decade. The larger the cell, the larger the movie's box office revenue. I had a lot of fun using MovieSieve. I will be thinking of adding improvements in the

Young Coders: Some Basics

Before our first session, start playing around with Java!  The first step in learning how to program is to learn how to output to the console. Not only will printing show you what your program is doing, but it will also help you debug. That’s why almost everyone’s first program is to print out “Hello, World!” to the console! 1 2 3 4 5 public class HelloWorld{     public static void main(String[] args) {        System.out.println( "Hello, World!" );     } }