Wednesday, March 18, 2009

JAVA Exercises

Exer 1

/*programmer: Bocong, Wendel B.
programme name: Reverse Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Reversing String*/
import java.util.Scanner;

public class Reverse {

public static void main(String args[]){
Scanner a=new Scanner(System.in);
String wendel;
System.out.printf("%s", "Enter the String: ");
wendel=a.nextLine();
wendel = new StringBuffer(wendel).reverse().toString();
System.out.println("Reversed String : " + wendel);
}
}

Exer 2

/* Exer 2
Programmer: Bocong, Wendel B.
Programme name: Color Cycle
Date Started: March 17, 2009
Date Ended: March 25, 2009
Purpose: To have a java code that manipulate the color backgroud of the Frame.
*/
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;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class ColorCycle extends JPanel implements ActionListener {

private static boolean USE_CROSS_PLATFORM_UI = false;

int buttonLabelIndex = 0;
String buttonLabels[] = { "black", "blue", "orange", "yellow" };
Color buttonColors[] = { Color.BLACK, Color.BLUE, Color.ORANGE, Color.YELLOW,};

JButton button;
public ColorCycle() {
super(new BorderLayout());
button = new JButton(buttonLabels[buttonLabelIndex]);
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(25,25,25,25));
}
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("Color Cycle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent contentPane = new ColorCycle1();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
run();
}
}


Exer 3


/* Exer 3
Programmer: Bocong, Wendel B.
Programme name: Combination Lock
Date Started: March 17, 2009
Date Ended: March 25, 2009
Purpose: To have a java code that will let the user press the button and if the user click the the wrong one in three
consecutive answer the color background will turn into red.
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class combinationLock extends JFrame{
String[] password={"6","1","4"};
int wen=0,del=0;
JButton buttons[];
JPanel buttonPanel;
Container container = getContentPane();

public combinationLock(){
super("Combination Lock");

ClickEvent action = new ClickEvent();
container.setLayout(new FlowLayout());
buttons = new JButton[10];
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0,buttons.length));

for(int wen=0;wen<10;wen++){
if(event.getActionCommand().equals(password[x])){
del+=1;
if(del==3){
System.exit(0);
}
}else{
y+=1;
if(y==3){
container.setBackground(Color.red);
wen=-1;
del=-1;
}
}
x++;
}
}
}
}


Exer 4

/*programmer: Bocong, Wendel B.
programme name: UpperCase Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Converting the input of the User and convert it to Upperase*/
import java.util.*;
import java.io.*;
public class UpperCase {

public static void main(String[] args) throws IOException
{
Scanner a=new Scanner(System.in);
String wen;
String del;
String boc;
System.out.printf("%s", "Enter the Name: ");
wen=a.nextLine();
del = wen.substring(0,wen.indexOf(" "));
boc= wen.substring(wen.indexOf(" "),wen.length()).toUpperCase();
System.out.println(" "+del+ " " +boc);
}
}


Exer 5

/*programmer: Bocong, Wendel B.
programme name: Hello Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Hello Program */
import java.util.Scanner;
public class Hello{

public static void main (String args[]){
Scanner wendel=new Scanner(System.in);
String greeting;
System.out.printf("%s", "Enter your Greeting: ");
greeting=wendel.nextLine();
System.out.printf("\n");
System.out.printf("%s",greeting);
}
}

No comments:

Post a Comment