Wednesday, March 18, 2009

Combination Lock

Exer. 3 Combination Lock
/*
Programmer: Albert Ampo
Date Started: March 17,2009
Date Ended: March 19, 2009

*/

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

public class CombinationLock extends JFrame {

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

public CombinationLock () {

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


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

pack();


}
}

Tuesday, March 17, 2009

Name Echo

Exer. 4 Name Echo
/*
Programmer: Albert Ampo
Date Started: March 17,2009
Date Ended: March 18, 2009

*/

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: albert ampo

albert AMPO

Word Reverses

Exer.1 Word Reverses
/*
Programmer: Albert Ampo
Date Started: March 17,2009
Date Ended: March 18, 2009

*/

import java.util.Scanner;
import java.io.*;
import java.lang.StringBuffer;
public class reverse {
   
 
  public static void main(String[] args) throws IOException
  {
  // The normal sentence that is going to be reversed.
  System.out.print("Enter Name: ");
  Scanner in=new Scanner(System.in);
  String words = in.nextLine();
   
   
   
  String Word1=words.substring(words.indexOf(" "),words.length());
  String reverse= new StringBuffer(Word1).reverse().toString();
  String Word2=words.substring(0,words.indexOf(" "));
  String 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 );
  }

}

Input: albert ampo

trebla opma

Hello Object

Exer.5 Hello Object
/*
Programmer: Albert Ampo
Date Started: March 17,2009
Date Ended: March 18, 2009

*/


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

Exer.2 Color Cycle
/*
Programmer: Albert Ampo
Date Started: March 17,2009
Date Ended: March 18, 2009

*/

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[] = { "Green", "blue", "Gray", "Red" };
 Color buttonColors[] = { Color.GREEN, Color.BLUE, Color.GRAY, Color.RED,};
 
 JButton button;
 
 public ButtonDemo() {
  super(new BorderLayout());
  
  button = new JButton(buttonLabels[buttonLabelIndex]);
  // In the default UI look and feel you cannot easily alter the background color
  // 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 < buttonLabels.length?buttonLabelIndex:0;

  button.setText(buttonLabels[buttonLabelIndex]);
  this.setBackground(buttonColors[buttonLabelIndex]);
  button.setBackground(buttonColors[buttonLabelIndex]);
 
 }

 
 private static void run() {
  if(USE_CROSS_PLATFORM_UI) {
  try {
  UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  
  JFrame frame = new JFrame("Button Demo");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  JComponent contentPane = new ButtonDemo();
  contentPane.setOpaque(true);
  
  frame.setContentPane(contentPane);
  frame.pack();
  
  frame.setVisible(true);
 }
 
 public static void main(String[] args) {
  run();
 }

}

Friday, March 6, 2009

User-Friendly Division by: Albert Ampo
/*
Programmer: Albert Ampo
Date Started: March 6,2009
Date Ended: March 7, 2009

*/

import javax.swing.*;

public class UserFriendlyDivision{

public staic void main(String args[]){

char ans='y';

while(ans=='y'){

double x=Integer.parseInt(JOptionPane.showInputDialog("Enter numerator:"));//ask user input 
double y=Integer.parseInt(JOptionPane.showInputDialog("Enter divisor:"));//ask user second input

double answer=x/y; //formula to get the answer

System.out.println(+ x + " / " + ); //display the input ask
System.out.println("The quotient is:"+answer); //display the answer

if(y==0)//conditional method 
{
System.out.println("You cannot divide the " + x + " to 0. ");// display th econditional staement
}

String a=(JOptionPane.showInputDialog("Do you want to continue?");//ask user to continue the program
ans=a.chartAt(0);//get the input
}
}//end main method

}//end class UserFriendlyDivision

Tuesday, March 3, 2009

Iterator

/* Programmer : Ampo, Albert S.
  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
*/

import java.util.ArrayList;
import java.util.Iterator;
 
public class IterateThroughArrayListUsingIterator {
 
  public static void main(String[] args) {
 
  //create an ArrayList object
  ArrayList arrayList = new ArrayList();
 
  //Add elements to Arraylist
  arrayList.add("a");
  arrayList.add("l");
  arrayList.add("b");
  arrayList.add("e");
  arrayList.add("r");
  arrayList.add("t");
 
  //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 would be
Iterating through ArrayList elements...
a
l
b
e
r
t
*/

/*source : http://www.java-examples.com/iterate-through-elements-java-arraylist-using-iterator-example



    I learned this topic about iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. An iterator is sometimes called a cursor, especially within the context of a database.