Thursday, March 19, 2009

IT134 EXERCISES

/*Programmer: Cristine A. Salac
Program Name: Word Reverser(exer 1)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program in a sentence from the user and prints it out with each word reversed, but with the words 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

--------------------------

/*Programmer: Cristine A. Salac
Program Name: Color Cycle(exer 2)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose:to create a program that has only one button in the frame. Clicking on the cycles through the colors,on color change per click.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass 

{ public static void main(String args[]) 

{  
 JFrame f = new JFrame("JColorChooser Sample");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JButton button = new JButton("Pick to Change Background");

  ActionListener actionListener = new ActionListener()

 { public void actionPerformed(ActionEvent actionEvent) 

{  
 Color initialBackground = button.getBackground();
  Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground);

  if (background != null)

 { button.setBackground(background);

  }
  }

  };

  button.addActionListener(actionListener);

  f.add(button, BorderLayout.CENTER);
  f.setSize(300, 200);
  f.setVisible(true);

  }
}

------------------------------------------------------
/*Programmer: Cristine A. Salac
Program Name: Combination Lock(exer 3)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a frame with ten buttons, labeled 0-9.If the user wants to exit the program it should have three correct buttons in order and if the wrong combination is used, the frame turns 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.red);
pack();


}
}//end of class CombinationLock

-------------------------------------
/*Programmer: Cristine A. Salac
Program Name: Name Echo(exer 4)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program that asks for user’s name and then writes it back with the first name as entered, and the second name all in capital letters.
*/
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

-------------------
/*Programmer: Cristine A. Salac
Program Name: Hello Object(exer 5)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program where the greeting that is printed by the object is given by the user.
*/
import javax.swing.*;

public class Hello
{

public static void main(String args[]){

String i=JOptionPane.showInputDialog("Enter Greeting:");
System.out.println("Enter Greeting:"+i);
System.out.println("\n"+i);
}//end of main method
} //end of class Hello

Wednesday, March 11, 2009

USER-FRIENDLY DIVISION

Programmer: Cristine A. Salac
Program Name: User-Friendly Division
Time Started: March 6,2009
Time Finished: March 11, 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;  

  } // end method for quotient
 
  public static void main( String args[] )
  {
  Scanner scanner = new Scanner( System.in );  
  boolean continueLoop = true; // determines if more input is needed
   
  //the try block
  do  

  {  
  Try // read two numbers which are the numerator 
  and denominator and calculate quotient  
  {  
  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;  
  } // end try  
  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" );  
  } // end catch  
  catch ( ArithmeticException arithmeticException )  
  {  
  System.err.printf( "\nException: %s\n", arithmeticException );
  System.out.println( "Zero is an invalid denominator. Please try again.\n" );
  } // end catch  
  } while ( continueLoop ); // end do...while  
  } // end main
  
  } // end class 

Tuesday, March 3, 2009

Iterators/Arraylist

/*Programmer: Cristine A. Salac 
  Program Name: Iterate through elements Java ArrayList using Iterator 

  Date Started : March 3,2009  
  Date Finished: March 3,2009 
  Purpose: To create java code using iterator and arraylist 
*/ 

import java.util.Iterator;import java.util.ArrayList;
 
public class IterateThroughArrayListUsingIteratorExample {
 
  public static void main(String[] args) {
 
   
  ArrayList tin = new ArrayList();  
 
  tin.add("math"); 
  tin.add("science");
  tin.add("english");
  tin.add("filipino");
  tin.add("sibika");

Iterator itr = tin.iterator();
 

  System.out.println("Iterating through ArrayList elements:");
  while(itr.hasNext())
  System.out.println(itr.next());
  {
      System.out.print("\n");
System.out.println("Size of ArrayList before removing elements : " 
  + tin.size());
  }

tin.clear();

System.out.print("\n");

  System.out.println("Size of ArrayList after removing elements : " 
  + tin.size());
 
  }

}


Things I have learned:

Arraylist is an enhanced version of an array manipulation since it has methods that manipulate the array to stores the elements. Array has a specific index size unlike arraylist it grows automatically as we add elements.

(I got this code from the web,i just modified it)