Wednesday, March 18, 2009

Project Exercises

--Exercise No.1--

/*
Programmer Name: Jay S. Gil
Program Name: Word Reverser
Purpose: To Learned how to program when the output is 
reverse from the original word.
Date: March 18,2009
Instructor: Dony Dongiapon
*/

/*Java String Reverse example.
This example shows how to reverse a given string*/ 
public class StringReverse {

public static void main(String args[])

{ //declare orinial string

  String strOriginal = "Hello World";

  System.out.println("Original String : " + strOriginal);
  
  /* The easiest way to reverse a given string is to use reverse()
 method of java StringBuffer class.
  reverse() method returns the StringBuffer object so we need to
cast it back to String using toString() method of StringBuffer*/

  strOriginal = new StringBuffer(strOriginal).reverse().toString();

  System.out.println("Reversed String : " + strOriginal);

  }
}

/*Output of the program would be :
Original String : Hello World Reversed String : dlroW olleH*/

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

--Exercise No.2--

/*
Programmer Name: Jay S. Gil
Program Name: Color Cycle
Purpose: To Learned how to program when the output is color background code.
Date: March 18,2009
Instructor: Dony Dongiapon
*/


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

  }
}

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

--Exercise No.3--

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

--Exercise No.4--

/*
Programmer Name: Jay S. Gil
Program Name: Name Echo
Purpose: To Learned how to program the name echo problems
Date: March 18,2009
Instructor: Dony Dongiapon
*/
import java.util.Scanner;
import java.io.*;
public class Echo {  

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

  System.out.print("Enter 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(" "));  
 System.out.println("Normal : " + words);
 System.out.println("Reverse: " +Word2+ " "+Word1); 
 }
}

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

--Exercise No.5--

/*
Programmer Name: Jay S. Gil
Program Name: Name Echo
Purpose: To Learned how to program the name echo problems
Date: March 18,2009
Instructor: Dony Dongiapon
*/


import java.util.Scanner;
import java.io.*;
public class Echo {  

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

  System.out.print("Enter 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(" "));  
 System.out.println("Normal : " + words);
 System.out.println("Reverse: " +Word2+ " "+Word1); 
 }
}

THanK YOU!!!!

Monday, March 9, 2009

User-Friendly Division

//Programmer Name: Jay S. Gil
//Date Finished: MARCH 11, 2009
//Purpose: To apply my knowledge in making a program of division.

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

public class Division {

public static void main(String[] args)
{
division(100, 0);
} public static void division(int numerator, int denominator )throws Exception

{
return numerator/denominator;}

public static void main(String args[])
{
Scanner scan=new Scanner(System.in);

int numerator;
int denominator;
char x=exitchar.charAt(0);

while(x!='q'){if(x != 'Q')
{
try {
if((scan.next().charAt(0)=='q')(scan.next().charAt(0)=='Q')){x=scan.next().charAt(0);
}
else
{
numerator=scan.nextInt();

System.out.println("Enter the divisor:");
denominator=scan.nextFloat();
int average = numerator / denominator;
System.out.println("Average : " + average);
}
}
int average = numerator / denominator;
System.out.println("Average : " + average);
} catch (Exception ex)
{
System.out.println("Divide" + ex.getMessage() + "Exception" + "\n" + "Please check the denominator");

catch(InputMismatchException inputerr)
{
System.err.println("You enter bad data.\nPlease try again.");
}

catch(IndexOutOfBoundsException index)
{
System.err.println("");}
}else
{
x='q';
}
} }
}

Monday, March 2, 2009

Iterator

/*Programmer name: Jay Gil

Title: Iteration Arraylist

Date: March 2, 2009

Purpose: To apply knowledge that I've got from the reporter of Iterators.

*/

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

  }

}

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

/*
 Output: 
Original contents of al: C A E B D F 
Modified list backwards: F+ D+ B+ E+ A+ C+ 

 */