Currency: getInstance(Locale loc)

Tuesday, August 4th, 2009

/*
 * Output: 
Symbol: $
Default fractional digits: 2
 */

import java.util.Currency;
import java.util.Locale;

public class MainClass {
  public static void main(String args[]) {
    Currency c;

    c = Currency.getInstance(Locale.US);

    System.out.println("Symbol: " + c.getSymbol());
    System.out.println("Default fractional digits: "
        + c.getDefaultFractionDigits());
  }
}

           
       

new Stack < E > ()

Sunday, July 26th, 2009

/**
 *Output: 
stack: []
push(2)
stack: [2]
push(6)
stack: [2, 6]
push(9)
stack: [2, 6, 9]
pop -> 9
stack: [2, 6]
pop -> 6
stack: [2]
*/

import java.util.EmptyStackException;
import java.util.Stack;
  
public class MainClass {
  static void showpush(Stack<Integer> st, int a) { 
    st.push(a); 
    System.out.println("push(" + a + ")"); 
    System.out.println("stack: " + st); 
  } 
 
  static void showpop(Stack<Integer> st) { 
    System.out.print("pop -> "); 
    Integer a = st.pop(); 
    System.out.println(a); 
    System.out.println("stack: " + st); 
  } 
 
  public static void main(String args[]) { 
    Stack<Integer> st = new Stack<Integer>(); 
 
    System.out.println("stack: " + st); 
    showpush(st, 2); 
    showpush(st, 6); 
    showpush(st, 9); 
    showpop(st); 
 
    try { 
      showpop(st); 
    } catch (EmptyStackException e) { 
      System.out.println("empty stack"); 
    } 
  } 
}

           
       

Scanner: hasNextInt()

Sunday, July 26th, 2009

/*
String: string
boolean: true
boolean: false
int: 1
int: 2
int: 3
double: 4.12

 */
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) throws IOException {
    int i;
    double d;
    boolean b;
    String str;

    FileWriter fout = new FileWriter("test.txt");
    fout.write("string true false 1 2 3 4.12");
    fout.close();

    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

     while (src.hasNext()) {
      if (src.hasNextInt()) {
        i = src.nextInt();
        System.out.println("int: " + i);
      } else if (src.hasNextDouble()) {
        d = src.nextDouble();
        System.out.println("double: " + d);
      } else if (src.hasNextBoolean()) {
        b = src.nextBoolean();
        System.out.println("boolean: " + b);
      } else {
        str = src.next();
        System.out.println("String: " + str);
      }
    }

    fin.close();
  }
}
           
       

new BitSet(int size)

Monday, July 6th, 2009

/*
 * Output:
Initial pattern in bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2: 
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1: 
{2, 4, 6, 8, 12, 14}

bits2 OR bits1: 
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1: 
{}  
 */

import java.util.BitSet;

public class MainClass {
  public static void main(String args[]) {
    BitSet bits1 = new BitSet(16);
    BitSet bits2 = new BitSet(16);

    // set some bits
    for (int i = 0; i < 16; i++) {
      if ((i % 2) == 0)
        bits1.set(i);
      if ((i % 5) != 0)
        bits2.set(i);
    }

    System.out.println("Initial pattern in bits1: ");
    System.out.println(bits1);
    System.out.println("\nInitial pattern in bits2: ");
    System.out.println(bits2);

    // AND bits
    bits2.and(bits1);
    System.out.println("\nbits2 AND bits1: ");
    System.out.println(bits2);

    // OR bits
    bits2.or(bits1);
    System.out.println("\nbits2 OR bits1: ");
    System.out.println(bits2);

    // XOR bits
    bits2.xor(bits1);
    System.out.println("\nbits2 XOR bits1: ");
    System.out.println(bits2);
  }
}
           
       

Calendar.SUNDAY

Friday, July 3rd, 2009

/**
 *Output:

THURSDAY
 */

import java.util.Calendar;
import java.util.Date;

public class MainClass {
  public static void main(String args[]) {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek == Calendar.SUNDAY)
      System.out.println("SUNDAY");
    if (dayofweek == Calendar.MONDAY)
      System.out.println("MONDAY");
    if (dayofweek == Calendar.TUESDAY)
      System.out.println("TUESDAY");
    if (dayofweek == Calendar.WEDNESDAY)
      System.out.println("WEDNESDAY");
    if (dayofweek == Calendar.THURSDAY)
      System.out.println("THURSDAY");
    if (dayofweek == Calendar.FRIDAY)
      System.out.println("FRIDAY");
    if (dayofweek == Calendar.SATURDAY) {
      System.out.println("SATURDAY");
    }
  }
}