Saturday, March 21, 2009

Exercise5-Hello

/* Programmer: Norfe R. Gregorio
* Program name:Hello
* Subject: IT134_Computer Programming 3
* Instructor: Mr. Dony Dongiapon* Date Started: 03/14/09
* Date Finished: 03/19/09* Purpose: Will print the greeting that is given by the user*/
import java.*;
public class HelloTester{public static void main(String args[]){
String greet;Scanner scan=new Scanner(System.in);
System.out.println("Enter Your Greeting:");
greet=scan.nextLine();Hello h=new Hello(greet);System.out.println(h.echo());}}

Exercise4-NameEcho

/* Programmer: Norfe R. Gregorio * Program name: NameEcho * Subject: IT134_Computer Programming 3 * Instructor: Mr. Dony Dongiapon * Date Started: 03/16/09 * Date Finished: 03/19/09 * Purpose: To write a program that will 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.*;
public class NameEcho{ public static void main(String[] args)throws StringIndexOutOfBoundsException{ try{ Scanner scan = new Scanner(System.in); System.out.println("Enter your name:"); String name = scan.nextLine(); int Big=name.indexOf(" "); int gib=Big++; String output = name.substring(0,Big); String name2=name.substring(gib,name.length()); System.out.print(output+" "+name2.toUpperCase()); } catch(StringIndexOutOfBoundsException index){ System.err.printf("No more data."); } }}

Exercise3-CombinationLock

/*Programmer: Norfe R Gregorio *Date started: March 13, 2009 *Date ended: March 19, 2009 *Title:CombinationLock *Description:a program tht will result other frame color if the user will click three buttons that the program have*;
public class CombinationLock extends JFrame {
public static void main (String [] args) { new CombinationLock().setVisible(true); }
public CombinationLock () {
Container ComLock = getContentPane(); cp.setLayout(new FlowLayout());
ComLock.add(new JButton("0")); ComLock.add(new JButton("1")); ComLock.add(new JButton("2")); ComLock.add(new JButton("3")); ComLock.add(new JButton("4")); ComLock.add(new JButton("5")); ComLock.add(new JButton("6")); ComLock.add(new JButton("7")); ComLock.add(new JButton("8")); ComLock.add(new JButton("9")); ComLock.setBackground(Color.white); pack();
}}

Exercise2-ColorCycle

/* Programmer: Norfe R. Gregorio* Program name: Color Cycle* Subject: IT134_Computer Programming 3* Instructor: Mr. Dony Dongiapon* Date Started: 03/14/09 * Date Finished: 03/19/09* Purpose:A program that will change the color of the background in Frame when you click the botton*/
import java.*;
public class ColorCycle extends JFrame implements ActionListener {
private WindowTerminator wincolor = new WindowTerminator(); private JButton red, Green, Blue, Gray, Violet; private Container color;
public ColorCycle( ) { setSize(200,200); setLocation(100,100); setTitle(getClass().getName());
addWindowListener(wincolor);
red = new JButton("Red"); green = new JButton("Green"); blue = new JButton("Blue"); gray = new JButton("Gray"); Violet = new JButton("Violet"); red.addActionListener(this); green.addActionListener(this); blue.addActionListener(this); gray.addActionListener(this); Violet.addActionListener(this); color = getContentPane(); color.setBackground(Color.white); color.setLayout(new FlowLayout()); color.add(red); color.add(Green); color.add(Blue); color.add(Gray); color.add(Violet); show(); }
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource();
if ( s == Red) cp.setBackground(Color.Red); else if ( s == Green) cp.setBackground ( Color.Green); else if ( s == Blue) cp.setBackground ( Color.Blue); else if ( s == Gray) cp.setBackground ( Color.Gray); else if ( s == Violet) cp.setBackground ( Color.Violet);

color.repaint(); }
public static void main (String[] args) {
JFrame f = new ColorCycle(); }
}
class WindowTerminator extends WindowAdapter {
public void windowClosing (WindowEvent e) { System.exit(0); }}

Thursday, March 19, 2009

Exercise1-Reverse

/* Programmer: Norfe R. Gregorio* Program name: Reverse* Subject: IT134_Computer Programming 3* Instructor: Mr. Dony Dongiapon* Date Started: 03/14/09 * Date Finished: 03/19/09* Purpose: The purpose of this proggram is to read the entered string of the user in reverse form*/
import java.util.*;public class reverse{ public static void main(String[] args){sring val;string value; Scanner scan = new Scanner(System.in); System.out.print("Please enter the word to be rverse: "); String value = scan.nextLine(); StringTokenizer val = new StringTokenizer(value); while(val.hasMoreTokens())
{ String element = tokens.nextToken(); StringBuffer buffer = new StringBuffer(word); element = buffer.reverse().toString(); System.out.print(word+" "); } }}

Monday, March 16, 2009

User-Friendly Division

import java.util.*;

public class UserFriendlyDivision{
public static double quotient(double num, double den)
{
return num/den;}
public static void main(String args[]){
Scanner scan=new Scanner(System.in);

String exit="quit";
char x=exit.charAt(0);
double num=0;
double den=0;
String ask="quit";

while(x!='q'){
if(x != 'Q'){

try{
System.out.println("Enter the numerator:");
if((scan.next().charAt(0)=='q')||(scan.next().charAt(0)=='Q')){
x=scan.next().charAt(0);
}else{
num=scan.nextFloat();
System.out.println("Enter the denomenator:");
den=scan.nextFloat();
double result=quotient(num,den);
System.out.println(num+"/"+den+" is "+result);
}
}
catch(ArithmeticException arerr)
{
System.err.println("You can't divide "+num+" by "+den);
}

catch(InputMismatchException inputerr)
{
System.err.println("You enter bad data.\nPlease try again.");
}
catch(IndexOutOfBoundsException index)
{
System.err.println("");
}

}else{
x='q';
}
}

}
}

Saturday, March 7, 2009

arraylist iterator

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

public class MainClass {
public static void main(String args[]) {
ArrayList al = new ArrayList();

al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");

System.out.print("Original contents of al: ");
Iterator itr = al.iterator();
while (itr.hasNext()) {
String element = itr.next();
System.out.print(element + " ");
}
System.out.println();

ListIterator litr = al.listIterator();
while (litr.hasNext()) {
String element = litr.next();
litr.set(element + "+");
}

// Now, display the list backwards.
System.out.print("Modified list backwards: ");
while (litr.hasPrevious()) {
String element = litr.previous();
System.out.print(element + " ");
}
}

}

/*things I've learned in this exercise:

Iterator takes the place of Enumeration in the Java collections framework.

  • Iterators allow the user to remove elements from the underlying collection during the iteration.
  • Method names have been improved in this Interface.

*/