Friday, March 20, 2009

EXER 3
/*Programmer: Tomarocon, Rolando
Program Name: Combination Lock
Date Finished: March 20, 2009
Program Purpose: To be able to make a program in which in a frame with ten buttons, labeled 0 through 9. If the user exit the program, he/she must click on the correct three buttons in order. 
*/
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("a"));
  cp.add(new JButton("b"));
  cp.add(new JButton("c"));
  cp.add(new JButton("d"));
  cp.add(new JButton("e"));
  cp.add(new JButton("f"));
  cp.add(new JButton("g"));
  cp.add(new JButton("h"));
  cp.add(new JButton("i"));
  cp.add(new JButton("j"));
  cp.setBackground(Color.black);
  pack();
}

}

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

Thursday, March 19, 2009

helloOBJECT
/*Exercise 5
 **Name of Program:helloObject
 *Programmer name:Tomarocon, Rolando
 *Date:March 19,2009
 *Purpose:To enter words then it will reverse.


import java.util.Scanner; 
public class helloObject 

  public static void main(String[] args)  
  { 
  String greetings; // Used for the input string. 
  Scanner input=new Scanner(System.in); 
  System.out.println("pls.........Enter ur Greeting:"); 
  greetings=input.nextLine(); 
  System.out.println(greetings); 
  } 
}
*/OUTPUT:

heloo

heloo

*/
===================================================================
NAME ECHO
/*Exercise 4
 **Name of Program:nameEcho
 *Programmer name:Tomarocon, Rolando
 *Date:March 19,2009
 *Purpose:Enter 2 words then the second word will be capital letter.

import java.util.Scanner;
import javax.swing.*;
import java.io.*;


public class nameEcho{
public static void main(String[] args) throws IOException// main method using catch Exceptions
{
String b;//first input string 
String c;//second input string

String a=JOptionPane.showInputDialog("Enter your Name: ");//ask input from the user

b=a.substring(a.indexOf(" "),a.length()).toUpperCase();// uppercase method
c=a.substring(0,a.indexOf(" "));

System.out.println("Input in normal form : " + a);// Print the normal string
System.out.println("Output: " +c+ " "+b);// Print string in normal and Uppercase

}
}


*/OUTPUT:

rolando tomarocon

rolando TOMAROCON

*/

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

WORD REVERSE
/*Exercise 1
 **Name of Program:Word Reverse
 *Programmer name:Tomarocon, Rolando
 *Date:March 19,2009
 *Purpose:To enter a word that will reverse from the first index to last.
 */

 
public class wordReverse
{
  public static void main(String[] args)
  {
  // The normal sentence that is going to be reversed.
  String words = "Tomarocon, Rolando";
   
  // To reverse the string we can use the reverse() method in the 
  // StringBuffer class. The reverse() method returns a StringBuffer so
  // we need to call the toString() method to get a string object.
  String reverse = new StringBuffer(words).reverse().toString();
   
  // Print the normal string
  System.out.println("Normal : " + words);
  // Print the string in reversed order
  System.out.println("Reverse: " + reverse);
  }
}
 
/* OUTPUT:

Normal : Tomarocon, Rolando
Reverse: odnaloR ,nocoramoT
*/

------------------------------------------------------------------------------
COLOR CYCLE
/*Exercise 2
 *Name of Program:Color Cycle
 *Programmers name:Tomarocon, Rolando
 *Date:March 19,2009
 *Purpose:When you enter the button then the color of the background will change.depend to the color
 */

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 colorcycle extends JPanel implements ActionListener {
private static boolean USE_CROSS_PLATFORM_UI = false;
int buttonLabelIndex = 0;
String buttonLabels[] = { "green", "yellow", "pink", "Red", "gray","black","white","orange","blue"};
Color buttonColors[] = { Color.GREEN, Color.YELLOW, Color.PINK, Color.RED,Color.GRAY,Color.BLACK,Color.WHITE,Color.ORANGE,Color.BLUE,};

JButton button;
public colorcycle() {
super(new BorderLayout());

button = new JButton(buttonLabels[buttonLabelIndex]);
// for buttons since it is designed to match the OS X UI.
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(20,20,20,20));
}

public void actionPerformed(ActionEvent e) {
buttonLabelIndex = ++buttonLabelIndex < frame =" new" contentpane =" new">

Friday, March 13, 2009

UserFriendlyDivision by:Tomarocon Rolando

import java .util.*;
import java.io.*;
public class DivisionPractice
{
 //this will compute the numerator and divisor 
public static int quotient(int x ,int y,int div)throws ArithmeticException{

public static void main (String[] args )
{
Scanner scan = new Scanner(System.in);
char a='m';
  do{
System.out.print("Enter numerator>:");
x = scan.nextInt();
  char x=n.charAt(0);
  try{
  //program will exit if x= 'q'or'Q'
  if(x=='q'||x=='Q'){
  System.exit(0);
  else{

System.out.print("Enter divisor>:");
  }
y = scan.nextInt();
  char y=d.charAt(0);
}
  //program will exit if y= 'q' or 'Q', 
  if(y=='q'||y=='Q'){
  System.exit(0);
  }else{
divide();
}

div = x / y;
System.out.println("The divide is >:" + div);
return div;

}
}


 catch(NumberFormatException numberFormat){
  //print the error message 
  System.err.printf("\nIncompatible type.\nPlease try again.");
  }
  catch(ArithmeticException arithmetic){
  //convert the String value and store in num
  num=Integer.parseInt(n);
  //print the error message
  System.err.printf("\nYou can't divide "+num+" by 0");
  }

 }

Tuesday, March 3, 2009

Iterators and ArrayList

/*
Programmer:Tomarocon, Rolando D.
Date:March 4,2008
Purpose:To learn about iterators.
*/

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

public class IterateThroughArrayListUsingIteratorExample {

public static void main(String[] args) {

//create an ArrayList object
ArrayList arrayList = new ArrayList();

//Add elements to Arraylist
arrayList.add("L");
arrayList.add("M");
arrayList.add("N");
arrayList.add("O");
arrayList.add("P");

//get an Iterator object for ArrayList using iterator() method.
Iterator itr = arrayList.iterator();

//use hasNext() and next() methods of Iterator to iterate through the elements
System.out.println("Iterating through ArrayList elements...");
while(itr.hasNext())
System.out.println(itr.next());

}
}

/*
Output
Iterating through ArrayList elements...
L
M
N
O
P
*/

What I learned in this exercise:

* This Java Example shows how to iterate through the elements of java
ArrayList object using Iterator.
* Use iterator() method to get an iterator object for arraylist
*ArrayList provides additional methods to manipulate the array that actually stores the elements.
*Iterator are usefull in making a programming.