Wednesday, March 18, 2009

exerCise ##: Combination Lock

//ProgrammerName: Reuben D. matiga
//ProgramName: Combination Lock
//Purpose: To Learned more in java
//Date: March 19,2009
//Instructor: Dony Dongiapon

import java.awt.*;
import javax.swing.*;

public class CombinationLock extends JFrame {

public static void main (String [] args) {
new CombinationLock().setVisible(true);
}

public CombinationLock () {

Container cp = getContentPane();
cp.setLayout(new FlowLayout());


cp.add(new JButton("0"));
cp.add(new JButton("1"));
cp.add(new JButton("2"));
cp.add(new JButton("3"));
cp.add(new JButton("4"));
cp.add(new JButton("5"));
cp.add(new JButton("6"));
cp.add(new JButton("7"));
cp.add(new JButton("8"));
cp.add(new JButton("9"));
cp.setBackground(Color.red);
pack();


}
}

Tuesday, March 17, 2009

exerCise #4: Name Echo

ExerCise #4: Name Echo

//ProgrammerName: Reuben D. matiga
//ProgramName: Name Echo
//Purpose: To Learned more in java
//Date: March 17,2009
//Instructor: Dony Dongiapon



import java.util.Scanner;
import java.io.*;
public class reverse {
   
 
  public static void main(String[] args) throws IOException
  {
  // The normal sentence that is going to be reversed.
  System.out.print("Enter your Name: ");
  Scanner in=new Scanner(System.in);
  String words = in.nextLine();
   
  String reverse2="";
   
  String Word1=words.substring(words.indexOf(" "),words.length()).toUpperCase();
  String Word2=words.substring(0,words.indexOf(" "));
   
  // Print the normal string
  System.out.println("Normal : " + words);
 // Print the string in reversed order
  System.out.println("Reverse: " +Word2+ " "+Word1);
  }

}

INPUT:
reuben matiga

OUTPUT:

reuben MATIGA

exercise # 5: HeLLo Object

ExerCise #5: HeLLo Object

//ProgrammerName: Reuben D. matiga
//ProgramName: HeLLo Object
//Purpose: To Learned more in java
//Date: March 17,2009
//Instructor: Dony Dongiapon


import javax.swing.*;

public class Reverse
{

  public static void main(String args[]){

  String i=JOptionPane.showInputDialog("Enter Greeting:");
  System.out.println("Enter greeting:"+i);
  System.out.println("\n"+i); 
  }
}

CoLor CycLe

ExerCise #2: CoLor Cycle

//ProgrammerName: Reuben D. matiga
//ProgramName: CoLor CycLe 
//Purpose: To Learned more in java
//Date: March 17,2009
//Instructor: Dony Dongiapon


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonDemo extends JPanel implements ActionListener {

 private static boolean USE_CROSS_PLATFORM_UI = false;
 
 int buttonLabelIndex = 0;
 String buttonLabels[] = { "yellow", "red", "black", "pink" };
 Color buttonColors[] = { Color.YELLOW, Color.RED, Color.BLACK, Color.PINK,};
 
 JButton button;
 
 public ButtonDemo() {
  super(new BorderLayout());
  
  button = new JButton(buttonLabels[buttonLabelIndex]);
 
  if(USE_CROSS_PLATFORM_UI) {
  button.setBackground(buttonColors[buttonLabelIndex]);
  } else {
  button.setForeground(buttonColors[buttonLabelIndex]);
  }
  
  button.addActionListener(this);
 
  
  this.add(button, BorderLayout.CENTER);
  
  this.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
 }
 
 public void actionPerformed(ActionEvent e) {
  buttonLabelIndex = ++buttonLabelIndex < frame =" new" contentpane =" new">

Word Reverser

ExerCise #1: Word Reverser

//ProgrammerName: Reuben D. matiga
//ProgramName: Word Reverser 
//Purpose: To Learned more in java
//Date: March 17,2009
//Instructor: Dony Dongiapon


 
public class reverse
{
  public static void main(String[] args)
  {
  // The normal sentence that is going to be reversed.
  String words = "Go to the main menu";
   
  String reverse2="";
  String reverse = new StringBuffer(words).reverse().toString();
  String Word1=words.substring(words.indexOf(" "),words.length());
  reverse= new StringBuffer(Word1).reverse().toString();
  String Word2=words.substring(0,words.indexOf(" "));

  reverse2=new StringBuffer(Word2).reverse().toString();


  // Print the normal string

  System.out.println("Normal : " + words);


 // Print the string in reversed order
  System.out.println("Reverse: " +reverse2+ " "+reverse);
  }
}
 

exer.#1_Word Rverser

Friday, March 6, 2009

User Friendly Division by: Reuben D. Matiga

User-Friendly Division by: Reuben D. Matiga
//ProgrammerName: Reuben D. matiga
//ProgramName: DivisionPractice
//Purpose: To Learned more in java
//Date: March 06,2009
//Instructor: Dony Dongiapon

import java.util.Scanner;

public class DivisionPractice 
{

  private static int quotient(int numerator, int denominator)

 {
  //throws Arithmetic Exception
  if (denominator = = 0)
  throw new ArithmeticException();
  return (numerator / denominator);

  }
  public static void main (String args [] ) 
{
   
  Scanner input = new Scanner(System.in);
   
  int number1 = 0, number2 = 0, result = 0;
  String num;
  String div;
  char x = ' ';
  //determine if the user wants to continue or quit
  while( ( x != 'q') || ( x != 'Q' )) 
{
  try {
  System.out.print ("\nEnter the numerator: ");
  num = input.next();
  x = snum.charAt(0);
  if( (x == 'q') || ( x == 'Q') )
  System.exit(-1);
  number1 = Integer.parseInt(num);
  System.out.print("Enter the divisor: ");
 div = input.next();
  number2 = Integer.parseInt(div);
  result = quotient(number1,number2);
  System.out.print(number1 + " / " + number2+" is "+result);
  }
  //cathes the Arithmetic Exception it detects
  catch (Exception e) {
  System.out.println(e.toString());
  System.out.println("You can't divide "+number1+" by "+number2);
  }
  }
}
   
}


Explanation of this code is, when you enter a value in numerator (any kind of value) and the value of you divisor is 0, the result is unknown because you can’t divide nay number in 0.

============================================================== 

User Friendly Division by: Reuben D. Matiga

User-Friendly Division by: Reuben D. Matiga
//ProgrammerName: Reuben D. matiga
//ProgramName: DivisionPractice
//Purpose: To Learned more in java
//Date: March 06,2009
//Instructor: Dony Dongiapon

import java.util.Scanner;

public class DivisionPractice 
{

  private static int quotient(int numerator, int denominator)

 {
  //throws Arithmetic Exception
  if (denominator = = 0)
  throw new ArithmeticException();
  return (numerator / denominator);

  }
  public static void main (String args [] ) 
{
   
  Scanner input = new Scanner(System.in);
   
  int number1 = 0, number2 = 0, result = 0;
  String num;
  String div;
  char x = ' ';
  //determine if the user wants to continue or quit
  while( ( x != 'q') || ( x != 'Q' )) 
{
  try {
  System.out.print ("\nEnter the numerator: ");
  num = input.next();
  x = snum.charAt(0);
  if( (x == 'q') || ( x == 'Q') )
  System.exit(-1);
  number1 = Integer.parseInt(num);
  System.out.print("Enter the divisor: ");
 div = input.next();
  number2 = Integer.parseInt(div);
  result = quotient(number1,number2);
  System.out.print(number1 + " / " + number2+" is "+result);
  }
  //cathes the Arithmetic Exception it detects
  catch (Exception e) {
  System.out.println(e.toString());
  System.out.println("You can't divide "+number1+" by "+number2);
  }
  }
}
   
}


Explanation of this code is, when you enter a value in numerator (any kind of value) and the value of you divisor is 0, the result is unknown because you can’t divide nay number in 0.

============================================================== 

Wednesday, March 4, 2009

Iterator arrayList

/* Programmer : Matiga, Reuben D.
  Program name : Iterators
  Date Started : March 2,2009
  Date Finished : March 3,2009
  Purpose : Iterate through elements Java ArrayList using Iterator
  Instructor : Dony Dongiapon
  Subject : IT134
*/
package collection;

import java.util.ArrayList;
import java.util.Iterator;

public class IteratorDemo {
  public static void main(String arg[])
  {
  ArrayList reu=new ArrayList();
  for(int i=0;i<10;i++)
  {
  reu.add(new Integer(i));
  }
  System.out.println(reu); // 0,1,2,3,……9
  Iterator itr=reu.iterator();
  while(itr.hasNext())
  {
  Integer i=(Integer)itr.next();
  if(i.intValue() % 2==0)
  {
  System.out.println(i);
  }
  else
  {
  itr.remove();
  }
  }
   
  }

}


output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
0
2
4
6
8

Source: http://www.javaexamples4u.com/2009/02/iterator-source-code.html

I learned that iterator is an object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data is available a StopIteration exception is raised.


***********************************************************************