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.

Monday, February 9, 2009

Program Solution of Direct Clothing Case Study by Lacno, Maribelle

/* Programmer:     Maribelle Lacno
  * Program name: Direct Clothing Case study
  * Subject:     IT134_Computer Programming 3
  * Instructor:     Mr. Dony Dongiapon
  * Date Started: 02/09/09
  * Date Finished: 02/09/09
  * Purpose:     To create an online direct clothing case study.
*/

catalog

public class catalog{

 private String clothes;
 
 private void addShirt(){
  System.out.println("\nShirt jacket is in catalog");
 }

 private void removeShirt(){
  System.out.println("\nShirt jacket is not in catalog");
 }

 public catalog(String newShirt){
  clothes = newShirt;
  this.addShirt();
 }
 public catalog(){
  this.removeShirt();
 }

}

shirt

public class shirt{

 private int shirtID;
 private double price;
 private String color;
 private String description;
 private int quantity;

 private void addStock(){
  customer me = new customer(20060413,"Maribelle","Calapagan",629355,"tulipslady@gmail.com");
 }

 private void removeStock(){
  catalog clothes = new catalog();
  System.out.println("\nJacket has been removed!");
  customer me = new customer();  
 }

 public void displayShirtInfo(){
  catalog clothes = new catalog("jacket");
  System.out.print("\nShirt Information: \nShirtID = "+shirtID+"\nPrice: "+price);
  System.out.print("\nColor: "+color+"\nDescription: "+description+"\nQuantity: "+quantity);
 }

 public shirt(int newID, double newPrice, String newColor, String newDescription, int newQuantity){
  shirtID = newID;
  price = newPrice;
  color = newColor;
  description = newDescription;
  quantity = newQuantity;
  
  this.addStock();
 }
 
 public shirt(){
  this.removeStock();
 }

}

customer

public class customer{

 private int customerID;
 private String name;
 private String address;
 private int phoneNumber;
 private String emailAddress;

 private void placeOrder(){
  System.out.print("\nCustomer "+name+" with an id of "+customerID+"\nwho lived in "+address+", and has a phone number "+phoneNumber);
  System.out.print("\nand email address of "+emailAddress+" wants to make an order.");
  order myOrder = new order(01,600.00,"available");
 }

 private void cancelOrder(){
  System.out.print("\nOrder has been cancelled!");
  order myOrder = new order();
 }

 public customer(int cusID, String cusName, String cusAddress, int cusPhoneNumber, String cusEmailAddress){
  customerID = cusID;
  name = cusName;
  address = cusAddress;
  phoneNumber = cusPhoneNumber;
  emailAddress = cusEmailAddress;
  this.placeOrder();
 }
  public customer(){
  this.cancelOrder();
  }

}

order

import java.util.Scanner;

public class order{
 Scanner scan = new Scanner(System.in);
 private int orderID;
 private double totalPrice;
 private String status;

 private void addShirtToOrder(){
  System.out.print("\nYour shirt has been added to order!");
 }

 private void removeShirtFromOrder(){
  System.out.print("\nYour shirt has been removed from order!");
 }

 public void submitOrder(){
  System.out.print("\nWhat form of payment you have? choose 1(check) or 0(credit card): ");
  int choice = scan.nextInt();
  if(choice==1){
  formOfPayment balance = new formOfPayment();
  }else{
  formOfPayment balance = new formOfPayment(7895,"Feb.14,2009");
  }
  this.addShirtToOrder();
 }

 public void putOrderOnHold(){
  this.removeShirtFromOrder();
 }

 public order(int newOrderID, double totalAmount, String availability){
  orderID = newOrderID;
  totalPrice = totalAmount;
  status = availability;
  this.submitOrder();
 }
 public order(){
  this.putOrderOnHold();
 }

}

formOfPayment

import java.util.Scanner;

public class formOfPayment{
 Scanner scan = new Scanner(System.in);
 private int checkNumber;
 private int creditCardNumber;
 private String expirationDate;

 private void verifyCreditCardNumber(){
  System.out.print("\nCredit Card number accepted!");
 }

 private void verifyCheckPayment(){
  System.out.print("\nCheck number accepted!");
 }

 public formOfPayment(int newCreditCardNumber, String dateExpiration){
  creditCardNumber = newCreditCardNumber;
  expirationDate = dateExpiration;
  
  this.verifyCreditCardNumber();
 }
 public formOfPayment(){
  this.verifyCheckPayment();
 }
}

directClothingTester

import java.util.Scanner;

public class directClothingTester{
 public static void main(String[] args){
  Scanner scan = new Scanner(System.in);
  System.out.print("\n*Welcome to Online Direct Clothing*\nAn official website in which you can order different clothes.");
  System.out.print("\nDo you want to make an order? choose 1(yes) or 0(no): ");
  int choice = scan.nextInt();
  if(choice==1){
  shirt jacket = new shirt(9863,300.00,"green","small",8);
  jacket.displayShirtInfo();
  }else{
  System.out.print("\nDo you want to removed an existing order? choose 1(yes) or 0(no): ");
  int answer = scan.nextInt();
  if(answer==1){
  shirt jacket = new shirt();
  }else{
  System.out.println("\nThanks for ordering!");
  }
 }
}
}

Wednesday, February 4, 2009

cube solution

/* Programmer:       Maribelle Lacno

 * Program name:    cube

 * Subject:               IT134_Computer Programming 3

 * Instructor:            Mr. Dony Dongiapon

 * Date Started:        02/04/09

 * Date Finished:       02/04/09

 * Purpose:              To create a class that will calculate the volume

 *                                 and area of a  cube inside a private methods.

*/

import javax.swing.JOptionPane;

public class cube{

  private double width;
  private double height;
  private double length;
  private double totalVolume;
  private double totalArea;
    
  //declare a constructor cube with a parameters.
  public cube(double newWidth, double newHeight, double newLength){
   width = newWidth;
   length = newLength;
   height = newHeight;
      
   this.volume();
   this.area();
  }
   
  //declare a constructor cube without a parameters.
  public cube(){
   double newWidth = Integer.parseInt(JOptionPane.showInputDialog("Enter a new width:"));
   double newHeight = Integer.parseInt(JOptionPane.showInputDialog("Enter a new height:"));
   double newLength = Integer.parseInt(JOptionPane.showInputDialog("Enter a new length:"));
        
   this.setDimension(newWidth, newHeight,newLength);
   this.volume();
   this.area();
  }
   
  //create a private method that will calculate and return the volume of a cube.
  private double volume(){
   return totalVolume = length*width*height;
  }
    
  //create a private method that will calculate and return the area of a cube.
  private double area(){
   return totalArea = length*width;
  }
    
  //set a new dimension of your cube
  public void setDimension(double newWidth, double newHeight, double newLength){
   width = newWidth;
   height = newHeight;
   length = newLength;
  }
    
  //display the volume and area of your cube
  public void displayCube(){
   JOptionPane.showMessageDialog(null,"The volume of your cube is "+totalVolume+"\n                  and the area of your cube is "+totalArea);
  }  
   
}

cubeTester

/* Programmer:     Maribelle Lacno
  *Program name:  cubeTester
  *Subject:               IT134_Computer Programming 3
  *Instructor:          Mr. Dony Dongiapon
  *Date Started:      02/04/09
  *Date Finished:    02/04/09
  *Purpose:              To create a cubeTester class that will access                                                             *                               the private methods of the cube class.
*/

public class cubeTester{

 public static void main(String[] args){
      
  cube myCube = new cube(5.0,5.0,5.0);
  myCube.displayCube();
        
  cube myNewCube = new cube();
  myNewCube.displayCube();
 }
   
}