Why So Scared

A blog about; Programming, Music and Random Stuff

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;
        }
}
Tags:
posted by Juo in Java, Uncategorized and have Comments (2)

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

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

Java Code – TicTac

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class TicTac implements ActionListener {

  public TicTac(){
    window.setSize(150,150);
    window.setLayout(new GridLayout(3,3));

    // display the buttons, smaller code with loop
    for(int i=0; i<=8; i++){
      buttons[i] = new JButton();
      window.add(buttons[i]);
      buttons[i].addActionListener(this);
    }
 Read more...
Tags:
posted by Juo in Java and have No Comments

Java Code – How Old Are You?

import java.util.Calendar;
import java.io.*;
import javax.swing.JOptionPane;

public class Age{
    public static void main(String args[])
    {
        String year = JOptionPane.showInputDialog
        ("Please Enter Your Year of Birth:");
        String month = JOptionPane.showInputDialog
        ("Please Enter The Month You Where Born:");
        String day = JOptionPane.showInputDialog
        ("Please Enter The Day You Where Born:");

        int y = Integer.parseInt(year);
        int m = Integer.parseInt(month);
        int d = Integer.parseInt(day);

        Calendar rightNow = Calendar.getInstance();
    	int yearnow = rightNow.get(rightNow.YEAR);
    	int monthnow = rightNow.get(rightNow.MONTH);
    	int daynow = rightNow.get(rightNow.DAY_OF_MONTH);
    	monthnow++;
    	yearnow -= y;
    		if (m>monthnow || m == monthnow && d>daynow)
    		{
    			yearnow--;
    		}
        JOptionPane.showMessageDialog
        (null, yearnow);
    }
}

Copy of code hosted at http://pastie.org/684706
Code saved as Age.java

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