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

No comments:

Post a Comment