/*
Exercise 2
Program Name:Color Cycle
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:15pm
Date Ended: March 18, 2009,9:15pm
Aim: When you enter the button then the color of the background will change depends on the color that are required or stated on the button.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ButtonDemo extends JPanel implements ActionListener {
private static boolean USE_CROSS_PLATFORM_UI = false;
int buttonLabelIndex = 0;
String buttonLabels[] = { "Green", "blue", "Gray", "Red" };
Color buttonColors[] = { Color.GREEN, Color.BLUE, Color.GRAY, Color.RED,};
JButton button;
public ButtonDemo() {
super(new BorderLayout());
button = new JButton(buttonLabels[buttonLabelIndex]);
// In the default UI look and feel you cannot easily alter the background color
// for buttons since it is designed to match the OS X UI.
if(USE_CROSS_PLATFORM_UI) {
button.setBackground(buttonColors[buttonLabelIndex]);
} else {
button.setForeground(buttonColors[buttonLabelIndex]);
}
button.addActionListener(this);
this.add(button, BorderLayout.CENTER);
this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
public void actionPerformed(ActionEvent e) {
buttonLabelIndex = ++buttonLabelIndex < buttonLabels.length?buttonLabelIndex:0;
button.setText(buttonLabels[buttonLabelIndex]);
this.setBackground(buttonColors[buttonLabelIndex]);
button.setBackground(buttonColors[buttonLabelIndex]);
}
private static void run() {
if(USE_CROSS_PLATFORM_UI) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
JFrame frame = new JFrame("Button Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent contentPane = new ButtonDemo();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
run();
}
}
Wednesday, March 18, 2009
COMBINATION LOCK
/*
Exercise 3
Program Name:Combination Lock
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:15pm
Date Ended: March 18, 2009,9:15pm
Aim: The user must click the correct combination button. If user will click the correct button then it is a right code and then exit, else the background of the JFrame will turn into color Red if wrong combination..
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Combination extends JPanel {
String[] password={"7","3","5"};
int Check=0;
int counter=0;
public Combination() {
JButton btn1 = new JButton("0");
btn1.addActionListener(new ButtonListener());
add(btn1);
JButton btn2 = new JButton("1");
btn2.addActionListener(new ButtonListener());
add(btn2);
JButton btn3 = new JButton("2");
btn3.addActionListener(new ButtonListener());
add(btn3);
JButton btn4 = new JButton("3");
btn4.addActionListener(new ButtonListener());
add(btn4);
JButton btn5 = new JButton("4");
btn5.addActionListener(new ButtonListener());
add(btn5);
JButton btn6 = new JButton("5");
btn1.addActionListener(new ButtonListener());
add(btn6);
JButton btn7 = new JButton("6");
btn7.addActionListener(new ButtonListener());
add(btn7);
JButton btn8 = new JButton("7");
btn8.addActionListener(new ButtonListener());
add(btn8);
JButton btn9 = new JButton("8");
btn9.addActionListener(new ButtonListener());
add(btn9);
JButton btn10 = new JButton("9");
btn10.addActionListener(new ButtonListener());
add(btn10);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener {
ButtonListener() {
}
public void actionPerformed(ActionEvent e)
{
if(counter<3)
{
if (e.getActionCommand().equals(password[counter]))
{
Check=1;
}
else
{
Check=2;
}
counter++;
}
else
{
if (e.getActionCommand().equals(password[counter]))
{
Check=1;
}
else
{
Check=2;
}
counter=0;
if(Check==3)
{
this.setBackground(Color.RED);
}
else
{
System.exit(0);
}
}
}
}
Exercise 3
Program Name:Combination Lock
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:15pm
Date Ended: March 18, 2009,9:15pm
Aim: The user must click the correct combination button. If user will click the correct button then it is a right code and then exit, else the background of the JFrame will turn into color Red if wrong combination..
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Combination extends JPanel {
String[] password={"7","3","5"};
int Check=0;
int counter=0;
public Combination() {
JButton btn1 = new JButton("0");
btn1.addActionListener(new ButtonListener());
add(btn1);
JButton btn2 = new JButton("1");
btn2.addActionListener(new ButtonListener());
add(btn2);
JButton btn3 = new JButton("2");
btn3.addActionListener(new ButtonListener());
add(btn3);
JButton btn4 = new JButton("3");
btn4.addActionListener(new ButtonListener());
add(btn4);
JButton btn5 = new JButton("4");
btn5.addActionListener(new ButtonListener());
add(btn5);
JButton btn6 = new JButton("5");
btn1.addActionListener(new ButtonListener());
add(btn6);
JButton btn7 = new JButton("6");
btn7.addActionListener(new ButtonListener());
add(btn7);
JButton btn8 = new JButton("7");
btn8.addActionListener(new ButtonListener());
add(btn8);
JButton btn9 = new JButton("8");
btn9.addActionListener(new ButtonListener());
add(btn9);
JButton btn10 = new JButton("9");
btn10.addActionListener(new ButtonListener());
add(btn10);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class ButtonListener implements ActionListener {
ButtonListener() {
}
public void actionPerformed(ActionEvent e)
{
if(counter<3)
{
if (e.getActionCommand().equals(password[counter]))
{
Check=1;
}
else
{
Check=2;
}
counter++;
}
else
{
if (e.getActionCommand().equals(password[counter]))
{
Check=1;
}
else
{
Check=2;
}
counter=0;
if(Check==3)
{
this.setBackground(Color.RED);
}
else
{
System.exit(0);
}
}
}
}
NAME ECHO
/*
Exercise 4
Program Name:NAME ECHO
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 9:15am
Date Ended: March 18, 2009,1:24pm
Aim: When enter two word second word will turn to capital letter or in upper case form.
*/
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
public class nameEcho{
public static void main(String[] args) throws IOException//start the main method that will catch Exceptions
{
String b;//first input string
String c;//second input string
String a=JOptionPane.showInputDialog("Enter your Name: ");//ask input to the user in correct form
b=a.substring(a.indexOf(" "),a.length()).toUpperCase();// uppercase method
c=a.substring(0,a.indexOf(" "));
System.out.println("Input in normal form : " + a);// Print the normal string
System.out.println("Output: " +c+ " "+b);// Print the string in normal and in Uppercase
}//end main method
}//end class nameEcho
Sample Output:
Enter name: dan ver
Input in Normal Form: dan ver
Output: dan VER
source:SAVITCH, WALTER"Java: AN INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING",THIRD EDITION
Exercise 4
Program Name:NAME ECHO
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 9:15am
Date Ended: March 18, 2009,1:24pm
Aim: When enter two word second word will turn to capital letter or in upper case form.
*/
import java.util.Scanner;
import javax.swing.*;
import java.io.*;
public class nameEcho{
public static void main(String[] args) throws IOException//start the main method that will catch Exceptions
{
String b;//first input string
String c;//second input string
String a=JOptionPane.showInputDialog("Enter your Name: ");//ask input to the user in correct form
b=a.substring(a.indexOf(" "),a.length()).toUpperCase();// uppercase method
c=a.substring(0,a.indexOf(" "));
System.out.println("Input in normal form : " + a);// Print the normal string
System.out.println("Output: " +c+ " "+b);// Print the string in normal and in Uppercase
}//end main method
}//end class nameEcho
Sample Output:
Enter name: dan ver
Input in Normal Form: dan ver
Output: dan VER
source:SAVITCH, WALTER"Java: AN INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING",THIRD EDITION
WORD REVERSE
/*
Exercise 1
Program Name: WORD REVERSE
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:30am
Date Ended: March 18, 2009,11:24am
Aim: To enter a word that will reverse from the first index to last. if two words entered, then the place of the word won't change but its index will reverse.
*/
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class Reverse {
public static void main(String[] args) throws IOException//start the main method that will catch Exceptions
{
String a=JOptionPane.showInputDialog("Enter Name: ");//ask input to the user in correct form
System.out.print("Input:"+a);//print output of the user
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("\nInput in Normal Form : " + a);// Print the normal string
System.out.println("Reverse input: " +reverse2+ " "+ reverse );// Print the string in reversed order
}//end main method
}//end class Reverse
Sample Output:
Enter name: dan ver
Input in Normal Form: dan ver
Reverse input: nad rev
source:SAVITCH, WALTER"Java: AN INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING",THIRD EDITION
Exercise 1
Program Name: WORD REVERSE
Programmer name: Danver G. Palmiano
Date Started: March 18,2009, 8:30am
Date Ended: March 18, 2009,11:24am
Aim: To enter a word that will reverse from the first index to last. if two words entered, then the place of the word won't change but its index will reverse.
*/
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class Reverse {
public static void main(String[] args) throws IOException//start the main method that will catch Exceptions
{
String a=JOptionPane.showInputDialog("Enter Name: ");//ask input to the user in correct form
System.out.print("Input:"+a);//print output of the user
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("\nInput in Normal Form : " + a);// Print the normal string
System.out.println("Reverse input: " +reverse2+ " "+ reverse );// Print the string in reversed order
}//end main method
}//end class Reverse
Sample Output:
Enter name: dan ver
Input in Normal Form: dan ver
Reverse input: nad rev
source:SAVITCH, WALTER"Java: AN INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING",THIRD EDITION
Monday, March 16, 2009
EXERCISE 5: Greetings..
/*
Programmer: Danver G. Palmiano
Date Started: March 17,2009
Date Ended: March 17, 2009
*/
import javax.swing.*;
public class helloObject{//name of class
public static void main(String args[])//start main method
{
String a=JOptionPane.showInputDialog("Enter Greeting:");//ask input to user
System.out.println("Enter greeting:"+a);
System.out.println("\n"+a); //display input
}//end main method
}//end class Reverse
SAMPLE OUTPUT:
Enter greeting:
Hello!
OUTPUT:
Enter greeting:Hello!
Hello!
Programmer: Danver G. Palmiano
Date Started: March 17,2009
Date Ended: March 17, 2009
*/
import javax.swing.*;
public class helloObject{//name of class
public static void main(String args[])//start main method
{
String a=JOptionPane.showInputDialog("Enter Greeting:");//ask input to user
System.out.println("Enter greeting:"+a);
System.out.println("\n"+a); //display input
}//end main method
}//end class Reverse
SAMPLE OUTPUT:
Enter greeting:
Hello!
OUTPUT:
Enter greeting:Hello!
Hello!
Friday, March 6, 2009
User-Friendly Division by: Danver G. Palmiano
/*
Programmer: Danver G. Palmiano
Date Started: March 6,2009
Date Ended: March 6, 2009
*/
import java.util.InputMismatchException;
import javax.swing.*;
public class UserFriendlyDivision{
public static void main(String args[]){
char ans='y';
while(ans=='y'){
double x=Integer.parseInt(JOptionPane.showInputDialog("Enter numerator:"));//ask user input
double y=Integer.parseInt(JOptionPane.showInputDialog("Enter divisor:"));//ask user second input
double answer=x/y; //formula to get the answer
System.out.println(+ x + " / " + ); //display the input ask
System.out.println("The quotient is:"+answer); //display the answer
if(y==0)//conditional method
{
System.out.println("You cannot divide the " + x + " to 0. ");// display th econditional staement
}
String a=(JOptionPane.showInputDialog("Do you want to continue?");//ask user to continue the program
ans=a.chartAt(0);//get the input
}
}//end main method
}//end class UserFriendlyDivision
Programmer: Danver G. Palmiano
Date Started: March 6,2009
Date Ended: March 6, 2009
*/
import java.util.InputMismatchException;
import javax.swing.*;
public class UserFriendlyDivision{
public static void main(String args[]){
char ans='y';
while(ans=='y'){
double x=Integer.parseInt(JOptionPane.showInputDialog("Enter numerator:"));//ask user input
double y=Integer.parseInt(JOptionPane.showInputDialog("Enter divisor:"));//ask user second input
double answer=x/y; //formula to get the answer
System.out.println(+ x + " / " + ); //display the input ask
System.out.println("The quotient is:"+answer); //display the answer
if(y==0)//conditional method
{
System.out.println("You cannot divide the " + x + " to 0. ");// display th econditional staement
}
String a=(JOptionPane.showInputDialog("Do you want to continue?");//ask user to continue the program
ans=a.chartAt(0);//get the input
}
}//end main method
}//end class UserFriendlyDivision
Monday, March 2, 2009
Array List and Iterator Exercise
/*
Programmer: Danver G. Palmiano
Date Started: March 2,2009
Date Ended: MArch 3, 2009
Purpose: To learn about Iterators.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Iteratorsmain{
public static void main(String args[]) {//start main method
ArrayList dan = new ArrayList();
dan.add("D");
dan.add("O");
dan.add("G");//output ask in arraylist
dan.add("C");
dan.add("A");
dan.add("T");
System.out.print("Original contents of al: ");
Iterator Iter = al.iterator();
while (Iter.hasNext()) {
String element = Iter.next();
System.out.print(element + " ");//print the original output in sequence
}
System.out.println();
ListIterator Litre= al.listIterator();
while (Litre.hasNext()) {
String element = Litre.next();
Litre.set(element + "+");// setting the element
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (Litre.hasPrevious()) {
String element = Litre.previous();
System.out.print(element + " ");//print the elements in backwards form
}
}// end main method
}//end class Iteratorsmain
*Output:
Original contents of al: D O G C A T
Modified list backwards: T+ A+ C+ G+ O+ D+
NOTE: This code were copied at the net. Since we are trying to learn how to create an Array List Iterators...
SOURCE:http://www.java2s.com/Code/JavaAPI/java.util/
ArrayListiterator.htm
What I learned in this exercise:
* To add iteration support to your own classes, we must implement the iter method.
* The for-in statement uses iterators to control the loop, and we can use iterators in many other contexts.
* Iterators are very usefull in making program even using it in our thesis...
Programmer: Danver G. Palmiano
Date Started: March 2,2009
Date Ended: MArch 3, 2009
Purpose: To learn about Iterators.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Iteratorsmain{
public static void main(String args[]) {//start main method
ArrayList
dan.add("D");
dan.add("O");
dan.add("G");//output ask in arraylist
dan.add("C");
dan.add("A");
dan.add("T");
System.out.print("Original contents of al: ");
Iterator
while (Iter.hasNext()) {
String element = Iter.next();
System.out.print(element + " ");//print the original output in sequence
}
System.out.println();
ListIterator
while (Litre.hasNext()) {
String element = Litre.next();
Litre.set(element + "+");// setting the element
}
// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (Litre.hasPrevious()) {
String element = Litre.previous();
System.out.print(element + " ");//print the elements in backwards form
}
}// end main method
}//end class Iteratorsmain
*Output:
Original contents of al: D O G C A T
Modified list backwards: T+ A+ C+ G+ O+ D+
NOTE: This code were copied at the net. Since we are trying to learn how to create an Array List Iterators...
SOURCE:http://www.java2s.com/Code/JavaAPI/java.util/
ArrayListiterator.htm
What I learned in this exercise:
* To add iteration support to your own classes, we must implement the iter method.
* The for-in statement uses iterators to control the loop, and we can use iterators in many other contexts.
* Iterators are very usefull in making program even using it in our thesis...
Subscribe to:
Posts (Atom)