Thursday, March 19, 2009

Java exercises

EXER 1
/*Programmer: Gerlie O. Lidon
Program Name: Word Reverser
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which the words that you enter will reverse with the words and punctuation in the original order.
*/

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

public class ReverseSent {
public static void main(String[] args) throws IOException
{

String a=JOptionPane.showInputDialog("Enter Name: ");//ask input from the user
System.out.print("Word:"+a);
String b=a.substring(a.indexOf(" "),a.length());
String reverse= new StringBuffer(b).reverse().toString();//reverse the first word entered
String c=a.substring(0,a.indexOf(" "));
String reverse2=new StringBuffer(c).reverse().toString();//reverse the second word entered
System.out.println("\nWord in Normal Form : " + a);// Print the normal string
System.out.println("Reverse word: " +reverse2+ " "+ reverse );// Print the string in reversed form

}//end of main method

}//end class Reverse

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

EXER 2

/*Programmer: Gerlie O. Lidon
Program Name: Color Cycle
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which there is only one button in the frame 
*/

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) {
System.exit(0);
}
}

***********************************************************************************
EXER 3
/*Programmer: Gerlie O. Lidon
Program Name: Combination Lock
Date Started: March 16, 2009
Date Finished: March 19, 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();
}
}

***********************************************************************************
EXER 4
/*Programmer: Gerlie O. Lidon
Program Name: Name Echo
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which it will ask the user's name and the name that is being enter will writes back with the first name as entered and the second name all in capital
*/

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

public class NameEcho {
public static void main(String[] args) throws IOException
{
System.out.print("\nEnter your name:");//Input a name
System.out.println("");
Scanner in = new Scanner(System.in);
String name = in.nextLine();

//first input/First name
String Firstname = name.substring(name.indexOf(" "),name.length()).toUpperCase();

//second input/Second name
String Secondname = name.substring(0,name.indexOf(" "));

// Print the Input names
System.out.print(Secondname);
System.out.println(Firstname);

}//end of main method

}//end of class NameEcho

***********************************************************************************
EXER 5 
/*Programmer: Gerlie O. Lidon
Program Name: Hello
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which a version of the HelloObject program the greeting that is being printed by the object is given by the use.
*/

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

Friday, March 13, 2009

Exception Handling( User-Friendly Division)

Programmer: Gerlie O. Lidon 
Program Name: User-Friendly Division
Time Started: March 6,2009 
Time Finished: March 13, 2009 
Program Purpose:To create a program about Exception Handling 

  import java.util.InputMismatchException; 
  import java.util.Scanner; 
   
  public class DivisionPractice 
  { 
  public static int quotient( int numerator, int denominator ) 
  throws ArithmeticException 
  { 
  return numerator / denominator;  
  }  
  public static void main( String args[] ) 
  { 
  Scanner scanner = new Scanner( System.in );  
  boolean continueLoop = true; // determines if more input is needed 
  {  
  {  
  System.out.print( "Enter your numerator: " ); 
  int numerator = scanner.nextInt();  

  System.out.print( "Enter your denominator: " ); 
  int denominator = scanner.nextInt();  

  int result = quotient( numerator, denominator );  
  System.out.printf( "\nResult: %d / %d = %d\n", numerator,  
  denominator, result );  
  continueLoop = false;  
  }  
  catch ( InputMismatchException inputMismatchException )  
  {  
  System.err.printf( "\nException: %s\n", inputMismatchException ); // discard input so user can try again  
  scanner.nextLine();  
  System.out.println( "You must enter integers. Please try again.\n”);  
  }  
  catch ( ArithmeticException arithmeticException )  
  {  
  System.err.printf( "\nException: %s\n", arithmeticException ); 
  System.out.println( "Zero is an invalid denominator. Please try again.\n" ); 
  }  
  } while ( continueLoop );  
  } 
  }


Thursday, March 5, 2009

ArrayListIterator

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("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");

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("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}

}


Things i have learned

Arraylist is an enhanced version of array.

Arraylist does not specifiy index.

Arraylist is more flexible than array.

Iterators is use to manipulater arraylist.