Thursday, March 19, 2009

EXER2-COLOR CYCLE

/*Program Name: Gezelle Re-ann Mandabon
Program Name: Color Cycle
Date-Started: 03-18-09
Date-Ended: 03-20-09
*/

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(250,205);
setLocation(100,100);
setTitle(getClass().getName());


addWindowListener(wt);

green = new JButton("Red");
blue = new JButton("Green");
gray = new JButton("Blue");
red = new JButton("Gray");
green.addActionListener(this);
blue.addActionListener(this);
gray.addActionListener(this);
red.addActionListener(this);
cp = getContentPane();
cp.setBackground(Color.black);
cp.setLayout(new FlowLayout());
cp.add(green);
cp.add(blue);
cp.add(gray);
cp.add(red);

show();
}


public void actionPerformed(ActionEvent e) {

JButton s = (JButton) e.getSource(); //get the source of the event

if ( s == green) cp.setBackground(Color.green);
else if ( s == blue) cp.setBackground ( Color.blue);
else if ( s == gray) cp.setBackground ( Color.gray);
else if ( s == red) cp.setBackground ( Color.red);


cp.repaint();
}


public static void main (String[] args) {

JFrame f = new ColorCycle();
}

}

class WindowTerminator extends WindowAdapter {

public void windowClosing (WindowEvent e) {
System.exit(0);
}
}

EXER3-Combination Lock

/*Program Name: Gezelle Re-ann Mandabon
Program Name:
Date-Started: 03-18-09
Date-Ended: 03-20-09
*/

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();
}
}

EXER5-HELLO

/*Program Name: Gezelle Re-ann Mandabon
Program Name: Hello
Date-Started: 03-18-09
Date-Ended: 03-20-09
*/

import java.util.Scanner;
public class Hello
{
public static void main(String[] args)
{
String greetings; // Used for the input string.
Scanner input=new Scanner(System.in);
System.out.println("Enter Greeting:");
greetings=input.nextLine();
System.out.println();
System.out.println(greetings);
}
}

sample output:

Enter Greeting: Hello YHANG!

                             hello YHANG!

Exer4-Name Echo

/*Program Name: Gezelle Re-ann Mandabon
  Program Name: Name Echo
  Date-Started: 03-18-09
  Date-Ended: 03-20-09
*/    

import java.util.*;  

import java.io.*;  

public class nameEcho 
{  
      public static void main(String[] args) throws IOException  
     {  
     String yourName; 
     String firstName; 
     String lastname; 
     System.out.println("Enter your name:");  
     Scanner input = new Scanner(System.in);  
     yourName = input.nextLine();  
     firstName = yourName.substring(0,yourName.indexOf(" "));  
     lastname= yourName.substring(yourName.indexOf(""),
     yourName.length()).toUpperCase();  
     System.out.println();  
     System.out.print(firstName); 
     System.out.print(lastname); 
     System.out.println();  
     }  

sample output: 

Enter your name:     Yhang-yhang
                                    YHANG-YHANG
Process completed.

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


   

EXER1-Word Reverse

/*Program Name: Gezelle Re-ann Mandabon
  Program Name: Word Reverser
  Date-Started: 03-18-09
  Date-Ended: 03-19-09
  Purpose: To be able to Reverse the wor and manipulate Swing.....
*/

import javax.swing.*;

public class WordReverse 
{
    public static void main(String[] args) 
    {
      String input;         
      String reversed;     

      input = JOptionPane.showInputDialog(null, "Enter a string"); 
      reversed = "";
      for (int i=0; i reversed = input.substring(i, i+1) + reversed;
      }
      JOptionPane.showMessageDialog(null, "Reversed:\n" + reversed);
  }
}

sample output

Enter a String:    Go to the main menu
                              unem niam eht ot oG


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



Tuesday, March 10, 2009

User-Friendly Division

/*Programmer Name:Gezelle Re-ann B. Mandabon
Program Name: User-Friendly Division
Date-Started: 03-08-09
Date-Ended: 03-10-09
Description: This program will calculate the quotient, then
if you enter any character it will return an error
message.If the user will press q it will exit.
Purpose: To know more about Exception
*/


import java.util.Scanner;
import java.util.InputMismatchException;

public class UserFriendlyDivision
{
public static int quo(int num,int den)throws ArithmeticException
{
return num/den;

}
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
int den;
int result;
char exit='y';
while (exit=='y')
{

try
{
System.out.print("Enter the numerator:");
num=input.nextInt();
System.out.print("Enter the denominator:");
den=input.nextInt();
result=quo(num,den);
System.out.println(num+"/"+den+" is " +result);
System.out.println("");
}

catch(InputMismatchException num)
{
System.err.printf("\n Exception %s\n");
System.out.println("Please try again");
}

catch(ArithmeticException den)
{
System.err.printf("\n Exception %s\n");
System.out.println("You cannot divide "+num+"by "+den);
}

System.out.println("Enter the numerator:");
}

}
}

Saturday, March 7, 2009

ArrayListIterator

public class ArrayListIterators
{

public ArrayListIterators()
{

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

public class MainClass
{

public static void main(String args[]) {
ArrayList al = new ArrayList();

al.add("LYHN");
al.add("JHONG");
al.add("MHING");
al.add("JHEN");
al.add("PHAU");
al.add("YHANG");

System.out.print("Original contents of al: ");
Iterator itr = al.iterator();
while (itr.hasNext())
{
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();
ListIterator litr = al.listIterator();
while (litr.hasNext())
{
String element = litr.next();
litr.set(element + "+");
}

// Now, display the list backwards.

System.out.print("List Backwards: ");
while (litr.hasPrevious())
{
String element = litr.previous();
System.out.print(element + " ");
}
}
}
}