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!!!!

No comments:

Post a Comment