Why So Scared

A blog about; Programming, Music and Random Stuff

Java Greatest Common Divisor – Method

1
2
3
4
5
6
7
8
9
10
11
12
13
public class cis229wk3 {
	 static long gcd(long m, long n) {
		   if (n==0)
		     return m;
		   else
		     return gcd(n, m % n);
	} 

	public static void main(String [] args) {
		System.out.println(gcd(4,8));
		// program returns a 4
	}
}
posted by Juo in Java and have No Comments

Java Reverse – Method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
import java.lang.*;

public class reverse {
        public static void main (String []args) {
                        ArrayList a= new ArrayList();
                        a.add("entry 0");
                        a.add("entry 1");
                        a.add("entry 2");
                        a.add("entry 3");
                        a.add("entry 4");

                        System.out.println(a);
                        System.out.println(reverse(a));

                }

        public static ArrayList reverse( ArrayList list ) {
           ArrayList store= new ArrayList();
           for (int i = 0; i < list.size(); i++) {
                store.add(list.get(list.size() -1 - i));
           }
        return store;
        }
}
posted by Juo in Java, Uncategorized and have No Comments
Tags:

select_tag / select input into my database rails

In my posts form the input to the model can only be Dog or Cat, I’ve constructed the following code and assumed it would work, however the information input into the select_tag box was never going into my database.

1
2
3
4
5
6
7
8
9
10
11
<% form_for(@post) do |f| %>

    <%= f.label :title %><br />
    <%= f.text_field :title %>

    <%= f.label :source %><br />
    <%= select_tag :source,  options_for_select([ "Dog", "Cat") %>

    <%= f.submit 'Create' %>

<% end %>

I finally got this to work by using;

1
<%= f.select(:source, options_for_select({ : Dog =>'Dog',:Cat =>'Cat'} %>

If you put the options in a hash then they will be output in a random order, to output the options in a chosen order they need to be put into an array, this left me with the following code.

View

1
2
<%= f.label :source %><br />
<%= f.select(:source, options_for_select(@source)) %>

Controller

1
@source = [['Dog'],['Cat'],]
posted by Juo in Ruby and have No Comments

Vodaphone UK MMS Contract Settings

APN: wap.vodafone.co.uk
Username: wap
Password: wap
MMSC: http://mms.vodafone.co.uk/servlets/mms/
MMS Proxy: 212.183.137.012 (note the leading zero in the last octet)
MMS Max Message Size: (blank)
MMS UA Prof URL: (blank)

MMS_Screenshot

posted by Juo in tidbit and have Comment (1)
Tags:

Java Code – Shuffle A Pack Of Cards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class shuffle {

  public shuffle(){
    window.setSize(1100,550);
    window.setLayout(new FlowLayout());
    JLabel cards[] = new JLabel[52];
  int number = 0;
  int [] ranarray = new int[52];
  boolean used[] = new boolean[52] ;

  Random generator = new Random();

  for (int i = 0; i < 52; i++) {
    do { number = generator.nextInt(52); }
      while(!used[number] == false); //while number has been used before try again
        {
           ranarray[i] = number; //unique number? add it to the array
           used[number] = true; //set number to true for comparison
        }
     }

    for (int i = 0; i < 52; i++) {
    cards[ranarray[i]] = new JLabel(new ImageIcon("cards/" + ranarray[i] + ".png")); //display cards
    window.add(cards[ranarray[i]]); //add cards
     }

    //show the window
    window.setVisible(true);

    }

  private JFrame window = new JFrame("Shuffle");

  //open Shuffle
  public static void main(String[] args){
    new shuffle();

  }

}

Copy of code hosted at http://pastie.org/755948

Code saved as Shuffle.java

posted by Juo in Java and have No Comments
Tags: