Friday, March 20, 2009

Exercise 1 --- Word Reverser

/* Programmer: Maribelle Lacno
  * Program name: reverseSent
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/09 
  * Purpose: To create a program that reads in a sentence 
  *     from the user and prints it with each word reversed. 
*/

import java.util.*;
public class reverseSent {
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    //ask the user to input data.
    System.out.print("\nInput: ");
    //get the data and store it in variable data.
    String data = scan.nextLine();
    //use StringTokenizer to separate each word.
    StringTokenizer tokens = new StringTokenizer(data);
    while(tokens.hasMoreTokens()){
      String element = tokens.nextToken();
      StringBuffer buffer = new StringBuffer(element);
      //method reverse reverses the contents of the StringBuffer.
      element = buffer.reverse().toString();
      //print the reverse word of data input by user.
      System.out.print(element+" ");
    }
  }
}

Exercise 2 --- Color Cycle

/* Programmer: Maribelle Lacno
  * Program name: colorCycle
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/09 
  * Purpose: To create a program that has only one button 
  *     in the frame but in clicking this button will change the
  *     background color. 
*/

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

public class colorCycle extends JFrame{
 int x=0;
 boolean click = false;
 JPanel buttonPanel;
 JButton buttons;
 Container container = getContentPane();
 String colors[] = {"red","blue","green","yellow"};
 Color buttonColors[] = {Color.red,Color.blue,Color.green,Color.yellow};
 
 public colorCycle(){
  super("Color Cycle");
  changeColor action = new changeColor();
  container.setLayout(new FlowLayout());
  buttons = new JButton(colors[x]);
  if(click){
  buttons.setBackground(buttonColors[x]);
  }else{
  buttons.setForeground(buttonColors[x]);
  }
  buttonPanel = new JPanel();
  buttonPanel.setLayout(new GridLayout());
  buttons.addActionListener(action);
  buttonPanel.add(buttons);
  container.add(buttonPanel,BorderLayout.CENTER);
  setSize(400,150);
  setVisible(true);
 }
 
 private class changeColor implements ActionListener{
  public void actionPerformed(ActionEvent event){
  x=++x  buttons.setText(colors[x]);
  container.setBackground(buttonColors[x]);
  buttons.setBackground(buttonColors[x]);
  }
 }

 public static void main(String[] args){
  colorCycle color = new colorCycle();
  color.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

Exercise 3 --- Combination Lock

/* Programmer: Maribelle Lacno
  * Program name: combinationLock
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/09 
  * Purpose: To create a program that will create a frame
  *     with ten buttons from 0 to 9 and will exit if the user
  *     click on the correct three buttons in order but if the
  *     wrong combination is used, the frame will turns red. 
*/

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

public class combinationLock extends JFrame{
  String[] password={"8","4","9"};
  int x=0,y=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 x=0;x<10;x++){
      if(event.getActionCommand().equals(password[x])){
      y+=1;
      if(y==3){
        System.exit(0);
      }
    }else{
      y+=1;
      if(y==3){
        container.setBackground(Color.red);
        x=-1;
        y=-1;
      }
    }
    x++;
   }
  }
 }
}

Exercise 4 --- Name Echo

/* Programmer: Maribelle Lacno
  * Program name: NameEcho 
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/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.util.*;
import java.io.*;

public class NameEcho{
 public static void main(String[] args)throws StringIndexOutOfBoundsException{
  try{
  Scanner scan = new Scanner(System.in);
  //ask the user to input data.
  System.out.println("Enter your name:");
  //get the data and store it in variable name.
  String name = scan.nextLine();
  //get the index of " " through indexOf method
  int c=name.indexOf(" ");
  int a=c+1;
  //store the first name entered to variable output
  String output = name.substring(0,c);
  //store the second name entered to variable name2
  String name2=name.substring(a,name.length());
  //print the first name and the second name in capital letters
  System.out.print(output+" "+name2.toUpperCase());
  }
  catch(StringIndexOutOfBoundsException index){
  System.err.printf("No more data.");
  }
 }
}

Thursday, March 19, 2009

Exercise 5 --- HelloObject

/* Programmer: Maribelle Lacno
  * Program name: Hello
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/09 
  * Purpose: To write a version of the HelloObject program 
  *     where the greeting that is printed by the object is given 
  *     by the user. 
*/

import java.util.*;

public class Hello{
Scanner scan = new Scanner(System.in);
public Hello(){
System.out.println("Enter greeting:");
String greeting = scan.nextLine();
System.out.println();
System.out.print(greeting);
}
public static void main(String[] args){
Hello greeting = new Hello();
}
}

Thursday, March 5, 2009

User-Friendly Division by: Maribelle P. Lacno

/* Programmer: Maribelle Lacno
  * Program name: DivisionPractice
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/06/09 
  * Date Finished: 03/09/09 
  * Purpose: To create a program that will reinforce 
  * my learnings and knowledge in Exception handling. 
*/


import java .util.*;
import java.io.*;


public class DivisionPractice {
 //create the quotient() method that will compute the numerator and divisor
 public static double quotient(double numerator, double divisor)throws
 ArithmeticException{
  return numerator/divisor;
 }

 public static void main(String[] args){
  //declare a scanner that will get the value of the user's input
  Scanner scan = new Scanner(System.in);
  double num, div, ans;
   //initialize variable c to be equal to 'm'
  char c='m';
  do{
   //ask the user to input the numerator
   System.out.print("\nEnter numerator: ");
   //get and store the numerator to variable n as a String
   String n=scan.next();
   //get the first character of n and store it in variable x
   char x=n.charAt(0);
   try{
   //if x is equal to 'q' or 'Q', the program will exit
   if(x=='q'||x=='Q'){
   System.exit(0);
   }else{
    //convert the String value of n to integer value and store it in variable num
    num=Integer.parseInt(n);
    //ask the user to input the divisor
     System.out.print("\nEnter divisor: ");
    //get and store the divisor to variable d as a String
    String d=scan.next();
    //get the first character of d and store it in variable y
    char y=d.charAt(0);
    //if y is equal to 'q' or 'Q', the program will exit
    if(y=='q'||y=='Q'){
     System.exit(0);
    }else{
     //convert the String value of d to integer value and store it in variable div
     div = Integer.parseInt(d);
     //call the method quotient() to compute the variable ans
     ans=quotient(num,div);
     //the program will print the result
      System.out.println();
      System.out.print(num+"/"+div+" is "+ans);
      System.out.println();
    }
   }
  }
  catch(NumberFormatException numberFormat){
    //print the error message and tell the user to input again
    System.err.printf("\nYou entered bad data.\nPlease try again.");
  }
  catch(ArithmeticException arithmetic){
    //convert the String value of n to integer value and store it in variable num
    num=Integer.parseInt(n);
    //print the error message
    System.err.printf("\nYou can't divide "+num+" by 0");
   }
  }
  while(c!='q');
  }
}

ArrayList & Iterator

/* Programmer: Maribelle Lacno
* Program name: myList
* Subject: IT134_Computer Programming 3
* Instructor: Mr. Dony Dongiapon
* Date Started: 03/03/09
* Date Finished: 03/04/09
* Purpose: To create a program that will understand the concept of ArrayList and iterator.
*/

import java.util.*;

public class myList{

public static void main(String[] args){
boolean quit = true;
System.out.print("\na. add new data to the list.");
System.out.print("\nb.remove data from the list.");
System.out.print("\nc. view the content of the list.");

Scanner scan=new Scanner(System.in);
ArrayList list= new ArrayList();
do{
System.out.print("\nWhat do you want to do? ");
String choice=scan.next();
char c=choice.charAt(0);
if(c=='a'){
System.out.print("\nYou choose to add a new data.");
System.out.print("\nEnter the data: ");
String data = scan.nextLine();
list.add(data);
System.out.print("\nSize of list after adding data: " + list.size());
}else if(c=='b'){
System.out.print("\nYou choose to remove a data.");
System.out.print("\nEnter the data: ");
String index = scan.nextLine();
list.remove(index);
System.out.print("\nSize of list after deleting data: " + list.size());
}else if(c=='c'){
System.out.print("\nYou choose to view the list.");
System.out.print("\nMy List of classmates: ");
Iterator classmate= list.iterator();
while (classmate.hasNext()){
String content= classmate.next();
System.out.print("\n"+content);
}
ListIterator listClassmate= list.listIterator();
while (listClassmate.hasNext()){
String content= listClassmate.next();
listClassmate.set("\n"+content);
}
}else if(c=='q'||c=='Q'){
System.exit(0);
}else{
System.out.print("\nInvalid choice.\nPlease try again.");
}
}while(quit);
}
}


Things I learned in this exercise:

  • ArrayList implementation is like the concept of Linkedlist.
  • the iterator is used to access and manipulate the operation of the ArrayList.
  • ArrayList class has many methods like add(), remove(), set(), and size().
  • to use the ArrayList and iterator or ListIterator, we must import the java.util package.