Saturday, March 28, 2009

Exercise 2: Change:Color

/*Programmer: Rosedil Mae Mobreros
*Date started: March 15, 2009
*Date ended: March 28, 2009
*Purpose: To create a program that used button and once it clicked, the background changed with corresponds to its button name.*/


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



public class ChangeColor extends JFrame
implements ActionListener
{

private WindowTerminator wt= new Terminate();
private JButton gray, red, green,blue // JButtons will cause the background colors to change
private Container rox; // this is the contentpane

public ChangeColor ( )
{
setSize(200,200);
setLocation(50,50);
setTitle(getClass().getName());


addWindowListener(winL);

gray = new JButton("Gray");
red = new JButton("Red");
green = new JButton("Green");
blue = new JButton("Blue");
gray.addActionListener(this);
red.addActionListener(this);
green.addActionListener(this);
blue.addActionListener(this);

rox = getContentPane();
rox.setLayout(new FlowLayout());
rox.setBackground(Color.yellow);
rox.add(gray);
rox.add(red);
rox.add(green);
rox.add(blue);

show();
}


public void actionPerform(ActionEvent ae)
{

JButton j = (JButton) ae.getSource();

if ( j == gray)rox.setBackground(Color.gray);
else if ( j == green)rox.setBackground(Color.red);
else if ( j == blue)rox.setBackground(Color.green);
else if ( j == gray)rox.setBackground(Color.blue);


rox.repaint();
}


public static void main (String[] args)
{

JFrame fm = new Color();
}

}

class WindowTerminator extends WindowAdapter
{

public void windowClosing (WindowEvent eve)
{
System.exit(0);
}
}

Thursday, March 19, 2009

Exercise1- Word Reverser

/*Programmer: Rosedil Mae P. Mobreros
*Date started: March 16, 2009
*Date ended: March 19, 2009
*Purpose: To create a program that access sentence from the user and prints it out with each word in reversed form.*/

import java.util.*;
public class WordReverse{
public static void main(String args[])
{

String roxy;
String key;

Scanner scan=new Scanner(System.in);
System.out.println("Key in desired word:");
input=scan.nextLine();
StringTokenizer k=new StringTokenizer(key);
while (k.hasMoreTokens())
{
roxy=k.nextToken();
roxy=new StringBuffer(k).reverse().toString();
System.out.print(k+" ");

}

}
}

Exercise5- Acknowledgement

Exercise5- Acknowledgement

/*Programmer: Rosedil Mae P. Mobreros
*Date started: March 15, 2009
*Date ended: March 19, 2009
*Purpose: To create a program that can access greetings from user and print it out. */

import java.util.Scanner;
public class Greetings
{
public static void main(String args[])
{
String acknowledge;
Scanner scan=new Scanner(System.in);
System.out.println("Please enter your acknowledgement:");
acknowledge =scan.nextLine();
Ciao c=new Ciao(acknowledge);
System.out.println(c.echo());
}
}

Wednesday, March 18, 2009

Exercise 3: Lock Combination

Exercise 3: Lock Combination
/*Programmer:Rosedil Mae P. Mobreros
*Date started: March 14, 2009
*Date ended: March 18, 2009
*Purpose: To create a program that uses number buttons from 0-9, when the user clicked three correct unlocked combination, the program will exit, hence its background will turn to red.*/


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

public class LockCombination extends JFrame
{

public static void main (String [] args)
{
new LockCombination ().setVisible(true);
}

public LockCombination ()
{

Container roxy = getContentPane();
roxy.setLayout(new FlowLayout());


roxy.add(new JButton("0"));
roxy.add(new JButton("1"));
roxy.add(new JButton("2"));
roxy.add(new JButton("3"));
roxy.add(new JButton("4"));
roxy.add(new JButton("5"));
roxy.add(new JButton("6"));
roxy.add(new JButton("7"));
roxy.add(new JButton("8"));
roxy.add(new JButton("9"));
roxy.setBackground(Color.white);
pack();


}
}

Exercise4: Name

Exercise4: Name
/*Programmer:Rosedil Mae P. Mobreros
*Date started: March 15, 2009
*Date ended: March 18, 2009
*Purpose: To create a program that will read and print the full name of the user. The first name will remain the same as the way the user had entered, but the last name should be changed into upper case.*/

import java.util.Scanner;
public class Name
{

public static void main(String args[])
{

Scanner scan=new Scanner(System.in);
String fullname;
String firstname;
String lastname;

System.out.println("Please enter your full name:");
name=scan.nextLine();
int roxy=name.indexOf(" ");
firstname=name.substring(0,roxy);
lastname=name.substring(roxy);
System.out.printf("\n%s %s\n",firstname,lastname.toUpperCase());

}
}

Wednesday, March 11, 2009

DivisionTest

/*Programmer:Rosedil Mae P. Mobreros
Date started: March 09, 2009
Date ended: March 11, 2009
Description: A program that will generate the quotient, by asking first the numerator and denominator.

import java.util.Scanner;
import java.util.InputMismatchException;

public class DivisionTest{

public static float quotient(float numerator, float denominator)throws ArithmeticException
{
return numerator/denominator;
}

public static void main(String args[]){
Scanner scan=new Scanner(System.in);

String endchar="kjl";
char x=endchar.charAt(0);
float numerator=0;
float denominator=0;
String ask="kjl";



while(y!='s')
{
if(y != 'S')
{

try
{
System.out.println("Enter numerator:");
if((scan.next().charAt(0)=='s')||(scan.next().charAt(0)=='S'))
{
y=scan.next().charAt(0);
}
else
{
numerator=scan.nextFloat();
System.out.println("Enter divisor:");
denominator=scan.nextFloat();
float result=quotient(numerator,denominator);
System.out.println(numerator+"/"+denominator+" is "+answer);
}
}

catch(ArithmeticException err)
{
System.err.println("Dividing "+numerator+" by "+denominator);
}

catch(InputMismatchException inputerr)
{
System.err.println("The number you entered is not valid.\nPlease enter a valid number.");
}
catch(IndexOutOfBoundsException index)
{
System.err.println("");
}

}
else
{
y='s';
}
}

}
}

Sunday, March 1, 2009

ArrayList Iterator Implementation

/*Programmer: Rosedil Mae Mobreros
Program Name: ArraylistIteratorTest
Date:March 2, 2009
Purpose: To create a program using array list and iterators.*/


import java.util.Iterator;
import java.util.ArrayList;
import java.util.Scanner;


public class ArrayListIteratorTest{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String beauty="a";



ArrayList list=new ArrayList();

list.add("flower");
list.add("bag");
list.add("dress");
list.add("phone");



System.out.println("My Favorite things : ");
Iterator it = list.iterator();
while (it.hasNext()) {
String flowers = it.next();


System.out.println(flowers);
}


}
}


Things I've learned in this exercise:
. I've learned that an array list is an array based implementation wherein the elements of the List can be easily accessed.

. Array List and Iterators allows duplication of data.

. Using these classes allows easy storage and retrieval of elements.