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

No comments:

Post a Comment