Wednesday, March 18, 2009
Exercise3- Combination Lock
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that used number buttons from 0-9, and once the user clicked three correct
*unlocked combination, the program will exit, otherwise its background will turn to red.*/
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.white);
pack();
}
}
Exercise2- Color Cycle
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that used button and once it clicked, the background
changed with corresponds to its button name.*/
/*Programmer: Miexie D. Tulid
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that used button and once it clicked, the background
changed with corresponds to its button name.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorCycle extends JFrame
implements ActionListener {
private WindowTerminator wt = new WindowTerminator(); // Code below
private JButton red,blue, green, gray; // the JButtons which will cause the background colors to change
private Container cp; // our contentpane
public ColorCycle( ) {
setSize(300,300);
setLocation(100,100);
setTitle(getClass().getName());
addWindowListener(wt);
red = new JButton("Red");
green = new JButton("Green");
blue = new JButton("Blue");
gray = new JButton("Gray");
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);
gray.addActionListener(this);
cp = getContentPane();
cp.setBackground(Color.white);
cp.setLayout(new FlowLayout());
cp.add(red);
cp.add(green);
cp.add(blue);
cp.add(gray);
show();
}
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource(); //get the source of the event
if ( s == red) cp.setBackground(Color.red);
else if ( s == green) cp.setBackground ( Color.green);
else if ( s == blue) cp.setBackground ( Color.blue);
else if ( s == gray) cp.setBackground ( Color.gray);
cp.repaint();
}
public static void main (String[] args) {
JFrame f = new ColorCycle();
}
}
class WindowTerminator extends WindowAdapter {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
}
Exercise5- Hello
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that reads a greeting from the user then prints it out. */
import java.util.Scanner;
public class HelloTester{
public static void main(String args[]){
String greet;
Scanner scan=new Scanner(System.in);
System.out.println("Enter Greeting:");
greet=scan.nextLine();
Hello h=new Hello(greet);
System.out.println(h.echo());
}
}
Exercise4- Name Echo
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that reads a full name the user and prints it out w/ the same word in firstname
but upper case in last name.*/
public class NameEcho{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String name;
String fname;
String lname;
System.out.println("Enter your name:");
name=scan.nextLine();
int x=name.indexOf(" ");
fname=name.substring(0,x);
lname=name.substring(x);
System.out.printf("\n%s %s\n",fname,lname.toUpperCase());
}
}
Exercise1- Word Reverser
*Date started: March 13, 2009
*Date ended: March 18, 2009
*Description: A program that reads a sentence from the user and prints it out w/ each word
*reversed.*/
import java.util.*;
public class reverseSent{
public static void main(String args[]){
String input;
String r;
Scanner scan=new Scanner(System.in);
System.out.println("Input:");
input=scan.nextLine();
StringTokenizer st=new StringTokenizer(input) ;
while (st.hasMoreTokens()) {
r=st.nextToken();
r=new StringBuffer(r).reverse().toString();
System.out.print(r+" ");
}
}
}
Friday, March 6, 2009
Friendly Division by: Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/
/*Programmer:Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivisionPractice{
public static float quotient(float numerator, float denominator)throws ArithmeticException
{
return numerator/denominator;}
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String exitchar="mix";
char x=exitchar.charAt(0);
float numerator=0;
float denominator=0;
String ask="mix";
while(x!='q'){
if(x != 'Q'){
try{
System.out.println("Enter the numerator:");
if((scan.next().charAt(0)=='q')||(scan.next().charAt(0)=='Q')){
x=scan.next().charAt(0);
}else{
numerator=scan.nextFloat();
System.out.println("Enter the divisor:");
denominator=scan.nextFloat();
float result=quotient(numerator,denominator);
System.out.println(numerator+"/"+denominator+" is "+result);
}
}
catch(ArithmeticException arerr)
{
System.err.println("You can't divide "+numerator+" by "+denominator);
}
catch(InputMismatchException inputerr)
{
System.err.println("You enter bad data.\nPlease try again.");
}
catch(IndexOutOfBoundsException index)
{
System.err.println("");
}
}else{
x='q';
}
}
}
}
Sunday, March 1, 2009
*Date ended: March 02, 2009
*Description: A program that uses ArrayList and Iterator classes.*/
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;
public class ArrayListIteratorTest{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String x="s";
ArrayList
while(!x.equals("q")){
x=JOptionPane.showInputDialog("Enter your favorite phrase. (Enter q to quit..)");
list.add(x);
}
System.out.println("Array List contents : ");
Iterator
while (it.hasNext()) {
String element = it.next();
if(!element.equals("q")){
System.out.println(element);}
}
}
}
Date: March 02, 2009
Things I've learned in this exercise.
- ArrayList cannot hold premitive data types such as int.
- In declaring an ArrayList index may not be specify.
- Iterator class is the class that is used to manipulate ArrayList.