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

Monday, March 9, 2009

User-Friendly Division By: Wendel B. Bocong

/*
Programmer: Wendel B. Bocong
Date Started: March 6,2009
Date Ended: March 9, 2009
Description:This program will calculate the quotient, then if you enter any character it will return an error message.
If the user will press q it will exit.
Purpose: To know more about different Exceptions.
*/
import java.util.*;
import java.io.*;

public class DivisionPractice{
public static double quotient(double numerator,double divisor)throws ArithmeticException{
return numerator/divisor;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double numerator,divisor,answer;
String end = "tech";

char wen=end.charAt(0);

do{
try{
System.out.print("\n Numerator: "); //entering numerator from the user
String del=scan.next(); //this will get the value of numerator as String
char boc=del.charAt(0); //using the method charAt()

if(boc=='q'||boc=='Q'){
System.exit(0);
}else{
numerator=Integer.parseInt(del);
System.out.print("\nDivisor: ");//entering divisor from the user
String cong=scan.next();
divisor=Integer.parseInt(cong);
answer=quotient(numerator,divisor);//calculating the quotient
System.out.print(numerator+"/"+divisor+" is "+answer);//displaying the result
System.out.println();
}
}
catch(ArithmeticException arithmetic){//this exception will catch all error input by the user
System.err.print("\nNothing to Divide because it is 0");//message that will display if the user try to divide the numerator into 0
}
catch(NumberFormatException inputMismatch){//this exception will catch all String input whether it is from numerator or divisor and display error message
System.err.print("\nUndesirable type of input.\nPlease try again.");
}
}
while(wen!='q'||wen=='Q');
}
}

Sunday, March 1, 2009

ArrayList/Iterators

/*** Programmer: Bocong, Wendel B.
Name of Program: ArrayList with IteratorsDate Started : March 2,2009
Date Ended: March 2,2009
Purpose: To try implementations that deals with ArrayList and Iterators.***/

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class iterators {
public static void main(String args[]) {
ArrayList<String> wen = new ArrayList<String>();

wen.add("Jessel Ann Abellana");
wen.add("Nerissa Agapay");
wen.add("Mixie Tulid");
wen.add("Rosedil Mobreros");
wen.add("Ana Mae Respecia");
wen.add("Wendel Bocong");

System.out.print("Original contents of wen: ");
Iterator<String> boc = wen.iterator();
while (boc.hasNext())
{
String element = boc.next();
System.out.print(element + " ");
}

System.out.println();

ListIterator<String> del= wen.listIterator();
while (del.hasNext())
{
String element = del.next();
del.set(element + "+");
}

System.out.println("Size of wen after additions: " + wen.size());
wen.remove(1);

{
System.out.println("Size of wen after deletions: " + wen.size());
System.out.println("Contents of wen: " + wen );
}
}
}


Things I learned in this exercise:

  • the implementation of array list is more comprehensive compared to array.