Wednesday, March 18, 2009

Exercise3- Combination Lock

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that used number buttons from 0-9, and once the user clicked three correct
 *unlocked combination, the program will exit, otherwise its background will turn to red.*/

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

public class CombinationLock extends JFrame {

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

public CombinationLock () {

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

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

  }
}

Exercise2- Color Cycle

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that used button and once it clicked, the background

  changed with corresponds to its button name.*/

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that used button and once it clicked, the background
  changed with corresponds to its button name.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ColorCycle extends JFrame 
    implements ActionListener {

  private WindowTerminator wt = new WindowTerminator(); // Code below
  private JButton red,blue, green, gray; // the JButtons which will cause the background colors to change
  private Container cp; // our contentpane

  public ColorCycle( ) {
  setSize(300,300);
  setLocation(100,100);
  setTitle(getClass().getName());


  addWindowListener(wt);

  red = new JButton("Red");
  green = new JButton("Green");
  blue = new JButton("Blue");
  gray = new JButton("Gray");
  red.addActionListener(this);
  green.addActionListener(this);
  blue.addActionListener(this);
  gray.addActionListener(this);
   
  cp = getContentPane();
  cp.setBackground(Color.white);
  cp.setLayout(new FlowLayout());
  cp.add(red);
  cp.add(green);
  cp.add(blue);
  cp.add(gray);

  show();
  }


  public void actionPerformed(ActionEvent e) {

  JButton s = (JButton) e.getSource(); //get the source of the event

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


  cp.repaint();
  }


  public static void main (String[] args) {

  JFrame f = new ColorCycle();
  }

}

class WindowTerminator extends WindowAdapter {

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

Exercise5- Hello

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that reads a greeting from the user then prints it out. */

import java.util.Scanner;
public class HelloTester{
public static void main(String args[]){
String greet;
Scanner scan=new Scanner(System.in);
System.out.println("Enter Greeting:");
greet=scan.nextLine();
Hello h=new Hello(greet);
System.out.println(h.echo());
}
}

Exercise4- Name Echo

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that reads a full name the user and prints it out w/ the same word in firstname

 but upper case in last name.*/


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

Scanner scan=new Scanner(System.in);
String name;
String fname;
String lname;

System.out.println("Enter your name:");
name=scan.nextLine();
int x=name.indexOf(" ");
fname=name.substring(0,x);
lname=name.substring(x);
System.out.printf("\n%s %s\n",fname,lname.toUpperCase());

}
}

Exercise1- Word Reverser

/*Programmer: Miexie D. Tulid
 *Date started: March 13, 2009
 *Date ended: March 18, 2009
 *Description: A program that reads a sentence from the user and prints it out w/ each word
 *reversed.*/


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

String input;
String r;
 Scanner scan=new Scanner(System.in);
 System.out.println("Input:");
 input=scan.nextLine();
 StringTokenizer st=new StringTokenizer(input) ;
  while (st.hasMoreTokens()) {
  r=st.nextToken();
  r=new StringBuffer(r).reverse().toString();
  System.out.print(r+" ");
   
  }
   
}
}

Friday, March 6, 2009

Friendly Division by: Miexie D. Tulid

/*Programmer:Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/

/*Programmer:Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/

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

public class DivisionPractice{

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 exitchar="mix";
char x=exitchar.charAt(0);
float numerator=0;
float denominator=0;
String ask="mix";



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{
numerator=scan.nextFloat();
System.out.println("Enter the divisor:");
denominator=scan.nextFloat();
float result=quotient(numerator,denominator);
System.out.println(numerator+"/"+denominator+" is "+result);
}
}

catch(ArithmeticException arerr)
{
System.err.println("You can't divide "+numerator+" by "+denominator);
}

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

}else{
x='q';
}
}

}
}

Sunday, March 1, 2009

/*Programmer: Miexie D. Tulid *Date started: March 02, 2009
 *Date ended: March 02, 2009
 *Description: A program that uses ArrayList and Iterator classes.*/

import java.util.Iterator;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.*;

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


ArrayList list=new ArrayList();



while(!x.equals("q")){
    x=JOptionPane.showInputDialog("Enter your favorite phrase. (Enter q to quit..)");
list.add(x);
}

System.out.println("Array List contents : ");
Iterator it = list.iterator();
while (it.hasNext()) {
String element = it.next();
if(!element.equals("q")){

 System.out.println(element);}
}  


}
}


Date: March 02, 2009

Things I've learned in this exercise.

  • ArrayList cannot hold premitive data types such as int.
  • In declaring an ArrayList index may not be specify.
  • Iterator class is the class that is used to manipulate ArrayList. 

Monday, February 9, 2009

/*Programmer:Miexie D. Tulid
*Title:ClothingTester class
*Status: it really work.! Try mo....*/


import javax.swing.JOptionPane;

public class ClothingTester
{
public static void main(String ars[]){
Shirt shirt1=new Shirt(0,20,"dark","men's wear",2);
Shirt shirt2=new Shirt(1,6,"red","ladies' wear",7);
Shirt shirt3=new Shirt(2,17,"rainbow","children's wear",5);


System.out.println(shirt1.displayInformation()+"\n\n");
System.out.println(shirt2.displayInformation()+"\n\n");
System.out.println(shirt3.displayInformation()+"\n\n");




String ans="mix";

while(ans!="Yes"||ans!="No"){
ans=JOptionPane.showInputDialog("Would you like to place an order?\n(Yes/No)");
if(ans.equals("Yes")){
JOptionPane.showMessageDialog(null,"Please fill in the following form.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);
String id=JOptionPane.showInputDialog("Enter your id");
String name=JOptionPane.showInputDialog("Enter your name");
String address=JOptionPane.showInputDialog("Enter your address");
String phone=JOptionPane.showInputDialog("Enter your phone number");
String email=JOptionPane.showInputDialog("Enter your e-mail address");
JOptionPane.showMessageDialog(null,"Thank you!\nNow please fill in the order form.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);
String shirtord=JOptionPane.showInputDialog("Enter the shirt id you want to purchase");
String quanord=JOptionPane.showInputDialog("Enter the quantity");
int cid=Integer.parseInt(id);
int shirtno=Integer.parseInt(shirtord);
int shirtquan=Integer.parseInt(quanord);
Customer customer=new Customer(cid,name,address,phone,email,shirtno,shirtquan);
System.out.println(customer.placeorder());
JOptionPane.showMessageDialog(null,"Look at your profile on the screen!","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null,"Thank you!\nNow fill in the payment form","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);
String fpayment="mix";
while(fpayment!="Credit card"||fpayment!="Check"){
fpayment=JOptionPane.showInputDialog("Enter your form of payment\n(Credit card/Check)");
if(fpayment.equals("Credit card")){
String screditno=JOptionPane.showInputDialog("Enter your credit card no");
String date=JOptionPane.showInputDialog("Enter its expiration date");
int creditno=Integer.parseInt(screditno);
FormofPayment payment=new FormofPayment(fpayment,creditno,date);
JOptionPane.showMessageDialog(null,"Submit?","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

String submit="mix";
while(submit!="Ok"){
String svcreditno=JOptionPane.showInputDialog("Verify your credit card no");
int vcreditno=Integer.parseInt(svcreditno);
submit=payment.verifyCardno(vcreditno);
if(submit.equals("Ok")){
JOptionPane.showMessageDialog(null,"Your credit card has been verified!","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}
else{
JOptionPane.showMessageDialog(null,"Your credit card has been denied!\nPlease try again.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}
}//end submit while

}//end if credit
else if(fpayment.equals("Check")){
String screditno=JOptionPane.showInputDialog("Enter your check no");
String date=JOptionPane.showInputDialog("Enter its expiration date");
int creditno=Integer.parseInt(screditno);
FormofPayment payment=new FormofPayment(creditno,date);
JOptionPane.showMessageDialog(null,"Submit?","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

String submit="mix";
while(submit!="Ok"){
String svcreditno=JOptionPane.showInputDialog("Verify your check no");
int vcreditno=Integer.parseInt(svcreditno);
submit=payment.verifyChekPayment(vcreditno);
if(submit=="Ok"){
JOptionPane.showMessageDialog(null,"Your check has been verified!","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}
else{
JOptionPane.showMessageDialog(null,"Your check has been denied!\nPlease try again.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}
}//end submit while

}//end elseif credit

else{
JOptionPane.showMessageDialog(null,"Invalid input!\nPlease try again.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}




}

}
else if(ans.equals("No")){
JOptionPane.showMessageDialog(null,"Goodbye!","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}

else {
JOptionPane.showMessageDialog(null,"Invalid input!\nPlease try again.","Clothing Order Interface",JOptionPane.PLAIN_MESSAGE);

}





}
}
}
/*Programmer:Miexie D. Tulid
*Title:Shirt class*/


public class Shirt
{
public int shirtID;
private double price;
private String color;
private String description;
private int quantity;

public Shirt(int newshirtID, double newprice, String newcolor, String newdescription, int newquantity)
{
shirtID=newshirtID;
price=newprice;
color=newcolor;
description=newdescription;
quantity=newquantity;
}


public int addStock( int stock)
{
quantity=quantity+stock;
return quantity;
}


public int removeStock( int stock)
{
quantity=quantity-stock;
return quantity;
}


public String displayInformation()
{
return String.format("ShirtID:"+shirtID+"nPrice:"+price+"\nColor:"+color+"\nDescription:"+description+"\nQuantity:"+quantity);

}
}
/*Programmer:Miexie D. Tulid
*Title:Catalog class*/


public class Catalog
{
int x=0;
Shirt shirtobjects[]=new Shirt[3];

public Catalog(int id,double p,String c, String d, int q){
shirtobjects[id]=new Shirt(id,p,c,d,q);
}



public void addShirt(int id,double p,String c, String d, int q){

for(int y=0;y
if(shirtobjects[y].shirtID==id)
{q=shirtobjects[y].addStock(q);
shirtobjects[y]=new Shirt(id,p,c,d,q);
}
else{
shirtobjects[id]=new Shirt(id,p,c,d,q);

}}

}

public void removeShirt(int id,double p,String c, String d, int q){

for(int y=0;y if(shirtobjects[y].shirtID==id)
{q=shirtobjects[y].removeStock(q);
if(q==0){

shirtobjects[y]=new Shirt(0,0,"none","none",0);
}
else{
shirtobjects[y]=new Shirt(id,p,c,d,q);

}
}

}
}
public String generateString(int z){
return String.format(shirtobjects[z].displayInformation());
}


}
/*Programmer:Miexie D. Tulid
*Title:Form of Payment class*/


public class FormofPayment
{

private int checkno;
private int cardno;
private String expiredate;
private String fpay;

public FormofPayment(String fpayment,int creditno,String xdate)
{
fpay=fpayment;
cardno=creditno;
expiredate= xdate;
}
public FormofPayment(int chek,String xdate)
{
checkno=chek;
expiredate= xdate;
}


public String verifyCardno(int newcardno)
{
if(cardno==newcardno){
return String.format("Ok");}

else{

return String.format("Not");}
}

public String verifyChekPayment(int newcheckno)
{
if(checkno==newcheckno){

return String.format("Ok");}

else{

return String.format("Not");}
}
}
/*Programmer:Miexie D. Tulid
*Title:Form of Payment class*/


public class FormofPayment
{

private int checkno;
private int cardno;
private String expiredate;
private String fpay;

public FormofPayment(String fpayment,int creditno,String xdate)
{
fpay=fpayment;
cardno=creditno;
expiredate= xdate;
}
public FormofPayment(int chek,String xdate)
{
checkno=chek;
expiredate= xdate;
}


public String verifyCardno(int newcardno)
{
if(cardno==newcardno){
return String.format("Ok");}

else{

return String.format("Not");}
}

public String verifyChekPayment(int newcheckno)
{
if(checkno==newcheckno){

return String.format("Ok");}

else{

return String.format("Not");}
}
}
/*Programmer:Miexie D. Tulid
*Title:Customer class*/


public class Customer{
private int cid;
private String cname;
private String caddress;
private String cphone;
private String cemail;
private int cshirtno;
private int cshirtquan;

public Customer(int id,String name,String address,String phone,String email,int shirtno,int shirtquan){
cid=id;
cname=name;
caddress=address;
cphone=phone;
cemail=email;
cshirtno=shirtno;
cshirtquan=shirtquan;

}

public String placeorder(){
return String.format("CustomerID:"+cid+"\nCustomer name"+cname+"\nAddress:"+caddress+"\nPhone number:"+cphone+
"\nEmail address:"+cemail+"\nShirt Id:"+cshirtno+"\nQuantity:"+cshirtquan);

}
}
/*Programmer:Miexie D. Tulid
*Title:Order class
*Status:unfinished*/


public class Order{

private int orderid;
private double totalprice;
private String status;
}
//Program Name: CubeTester.java
//Programmer: Miexie D. Tulid
//Description: A class program that already tests the class Cube.java
//Date: February 04, 2009


import javax.swing.JOptionPane;
public class CubeTester{
public static void main(String args[]){
Cube Cube1=new Cube(2,2,2);
Cube Cube2=new Cube();




JOptionPane.showMessageDialog(null,"The volume and area of constructor having a parameter is: "+Cube1.displayCube(),"Volume and Area of a Cube",JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog(null,"Now lets go to the constructor without a parameter!");
String w=JOptionPane.showInputDialog("Enter your width: ");
String h=JOptionPane.showInputDialog("Enter your height: ");
String l=JOptionPane.showInputDialog("Enter your lenght: ");

double a=Integer.parseInt(w);
double b=Integer.parseInt(h);
double c=Integer.parseInt(l);

Cube2.setDimension(a,b,c);
JOptionPane.showMessageDialog(null,"The volume and area of constructor having no a parameter is: "+Cube2.displayCube(),"Volume and Area of a Cube",JOptionPane.PLAIN_MESSAGE);

}
}
//Program Name: Cube.java
//Programmer: Miexie D. Tulid
//Description: A class program that allows to manipulate the area and volume of a cube using a constructor with and without a parameter.
//Date: February 04, 2009


public class Cube{

private double cwidth;
private double clength;
private double cheight;





public Cube(double width, double height, double length)
{
this.cwidth=width;
this.cheight=height;
this.clength=length;
}

public Cube()
{

}

private double volume()
{

return (cwidth*clength*cheight);

}


private double area()
{

return (cwidth*clength);

}


public void setDimension(double newwidth, double newheight, double newlength)
{
this.cwidth=newwidth;
this.cheight=newheight;
this.clength=newlength;
}

public String displayCube()
{
return String.format(volume()+" and "+area());

}
}