Thursday, March 19, 2009

IT134 EXERCISES

/*Programmer: Cristine A. Salac
Program Name: Word Reverser(exer 1)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program in a sentence from the user and prints it out with each word reversed, but with the words punctuation in the original order.
*/
import javax.swing.*;
import java.util.Scanner;
import java.io.*;


public class ReverseSent {

public static void main(String[] args) throws IOException
{

String a=JOptionPane.showInputDialog("Enter Name: ");//ask input from the user
System.out.print("Word:"+a);

String b=a.substring(a.indexOf(" "),a.length());
String reverse= new StringBuffer(b).reverse().toString();//reverse the first word entered
String c=a.substring(0,a.indexOf(" "));
String reverse2=new StringBuffer(c).reverse().toString();//reverse the second word entered


System.out.println("\nWord in Normal Form : " + a);// Print the normal string

System.out.println("Reverse word: " +reverse2+ " "+ reverse );// Print the string in reversed form
}//end of main method


}//end class Reverse

--------------------------

/*Programmer: Cristine A. Salac
Program Name: Color Cycle(exer 2)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose:to create a program that has only one button in the frame. Clicking on the cycles through the colors,on color change per click.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
public class MainClass 

{ public static void main(String args[]) 

{  
 JFrame f = new JFrame("JColorChooser Sample");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  final JButton button = new JButton("Pick to Change Background");

  ActionListener actionListener = new ActionListener()

 { public void actionPerformed(ActionEvent actionEvent) 

{  
 Color initialBackground = button.getBackground();
  Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground);

  if (background != null)

 { button.setBackground(background);

  }
  }

  };

  button.addActionListener(actionListener);

  f.add(button, BorderLayout.CENTER);
  f.setSize(300, 200);
  f.setVisible(true);

  }
}

------------------------------------------------------
/*Programmer: Cristine A. Salac
Program Name: Combination Lock(exer 3)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a frame with ten buttons, labeled 0-9.If the user wants to exit the program it should have three correct buttons in order and if the wrong combination is used, the frame turns 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.red);
pack();


}
}//end of class CombinationLock

-------------------------------------
/*Programmer: Cristine A. Salac
Program Name: Name Echo(exer 4)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program that 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.Scanner;
import java.io.*;

public class NameEcho {
public static void main(String[] args) throws IOException
{
System.out.print("\nEnter your name:");//Input a name
System.out.println("");
Scanner in = new Scanner(System.in);
String name = in.nextLine();

//first input/First name
String Firstname = name.substring(name.indexOf(" "),name.length()).toUpperCase();

//second input/Second name
String Secondname = name.substring(0,name.indexOf(" "));


// Print the Input names
System.out.print(Secondname);
System.out.println(Firstname);

}//end of main method

}//end of class NameEcho

-------------------
/*Programmer: Cristine A. Salac
Program Name: Hello Object(exer 5)
Date Started: 03/16/09
Date Finished: 03/19/09
Program Purpose: to create a program where the greeting that is printed by the object is given by the user.
*/
import javax.swing.*;

public class Hello
{

public static void main(String args[]){

String i=JOptionPane.showInputDialog("Enter Greeting:");
System.out.println("Enter Greeting:"+i);
System.out.println("\n"+i);
}//end of main method
} //end of class Hello

Wednesday, March 11, 2009

USER-FRIENDLY DIVISION

Programmer: Cristine A. Salac
Program Name: User-Friendly Division
Time Started: March 6,2009
Time Finished: March 11, 2009
Program Purpose:To create a program about Exception Handling

import java.util.InputMismatchException;

 import java.util.Scanner;
  
  public class DivisionPractice
  {

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

  } // end method for quotient
 
  public static void main( String args[] )
  {
  Scanner scanner = new Scanner( System.in );  
  boolean continueLoop = true; // determines if more input is needed
   
  //the try block
  do  

  {  
  Try // read two numbers which are the numerator 
  and denominator and calculate quotient  
  {  
  System.out.print( "Enter your numerator: " );
  int numerator = scanner.nextInt();  
  System.out.print( "Enter your denominator: " );
  int denominator = scanner.nextInt();  
   
  int result = quotient( numerator, denominator );  
  System.out.printf( "\nResult: %d / %d = %d\n", numerator,  
  denominator, result );  
  continueLoop = false;  
  } // end try  
  catch ( InputMismatchException inputMismatchException )  
  {  
  System.err.printf( "\nException: %s\n", inputMismatchException ); // discard input so user can try again  
  scanner.nextLine();  
  System.out.println( "You must enter integers. Please try again.\n" );  
  } // end catch  
  catch ( ArithmeticException arithmeticException )  
  {  
  System.err.printf( "\nException: %s\n", arithmeticException );
  System.out.println( "Zero is an invalid denominator. Please try again.\n" );
  } // end catch  
  } while ( continueLoop ); // end do...while  
  } // end main
  
  } // end class 

Tuesday, March 3, 2009

Iterators/Arraylist

/*Programmer: Cristine A. Salac 
  Program Name: Iterate through elements Java ArrayList using Iterator 

  Date Started : March 3,2009  
  Date Finished: March 3,2009 
  Purpose: To create java code using iterator and arraylist 
*/ 

import java.util.Iterator;import java.util.ArrayList;
 
public class IterateThroughArrayListUsingIteratorExample {
 
  public static void main(String[] args) {
 
   
  ArrayList tin = new ArrayList();  
 
  tin.add("math"); 
  tin.add("science");
  tin.add("english");
  tin.add("filipino");
  tin.add("sibika");

Iterator itr = tin.iterator();
 

  System.out.println("Iterating through ArrayList elements:");
  while(itr.hasNext())
  System.out.println(itr.next());
  {
      System.out.print("\n");
System.out.println("Size of ArrayList before removing elements : " 
  + tin.size());
  }

tin.clear();

System.out.print("\n");

  System.out.println("Size of ArrayList after removing elements : " 
  + tin.size());
 
  }

}


Things I have learned:

Arraylist is an enhanced version of an array manipulation since it has methods that manipulate the array to stores the elements. Array has a specific index size unlike arraylist it grows automatically as we add elements.

(I got this code from the web,i just modified it)

Monday, February 9, 2009

Direct Clothing Case Study by: Salac, Cristine

/*Programmer: Cristine A. Salac

Program Name:Catalog class (Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

public class Catalog

{

//variable declaration

private int shirtID;

private double price;

private String color;

private String description;

private int quantityinstock;

//constructor Definition

public Catalog()

{

}

public Catalog(int s, double p, String c, String d, int q)

{

shirtID=s;

price=p;

color=c;

description=d;

quantityinstock=q;

}

//method

public void addshirt(int newshirtID, double newprice, String newcolor, String newdescription, int newquantityinstock)

{

shirtID=newshirtID;

price=newprice;

color=newcolor;

description=newdescription;

quantityinstock=newquantityinstock;

System.out.println("Here are the info of our catalog:);

System.out.println("Shirt number: "+shirtID+ "\nPrice: "+price+"\nColor: "+color+"\nDescription: "+description+"\nQuantity in Stock: "+quantityinstock);

}

public void removeshirt(int newshirtID)

{

shirtID=newshirtID;

System.out.println("The Shirt number"+shirtid+"has been remove from the order");

}

}

/*Programmer: Cristine A. Salac

Program Name:Customer class (Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

public class Customer

{

// variable declaration

private int CustomerID;

private String Name;

private String Address;

private int Phonenumber;

private String Emailaddress;

//constructor Definition

public Customer()

{

}

public Customer(int c, String n, String a, int p, String e)

{

CustomerID=c;

Name=n;

Address=a;

Phonenumber=p;

Emailaddress=e;

}

//method

public void customerprofile(String newname, int newid, String newaddress, int newphonenumber, String newemailaddress)

{

CustomerID=newid;

Name=newname;

Address=newaddress;

Phonenumber=newphonenumber;

Emailaddress=newemailaddress;

system.out.println("Customer's Information:");

system.out.println( "\nCustomerID: "+CustomerID+" \nName: "+Name+" \nAddress: "+Address+" \nPhonenumber: "+Phonenumber+" \nEmailaddress: "+Emailaddress);

}

}

/*Programmer: Cristine A. Salac

Program Name:Order class (Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

public class Order

{

//variable declaration

private int OrderID;

private double Totalprice;

private String Status;

//constructor Definition

public Order()

{

}

public Order(int o, double t, String s)

{

OrderID=o;

Totalprice=t;

Status=s;

}

//method declaration

public void addorder(int neworder, double newtotalprice, String newstatus)

{

OrderID=neworder;

Totalprice=newtotalprice;

Status=newstatus;

}

public void removeorder(int order)

{

OrderID=order;

System.out.println("Order number: "+OrderID+" has been remove");

}

public void submitOrder()

{

OrderID=order;

System.out.println("Order number has been submitted "+OrderID);

}

public void putorderonhold()

{

System.out.println("The order is still on proccess");

}

}

/*Programmer: Cristine A. Salac

Program Name:Shirt class (Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

public class Shirt

{

//variable declaration

private int ShirtID;

private double price;

private String description;

private String color;

private int quantityinstock;

//constructor Definiton

public Shirt()

{

}

public Shirt(int s, double p, String d, String c, int q)

{

ShirtID=s;

price=p;

description=d;

color=c;

quantityinstock=q;

}

//method

public void addstock(int newShirtID, double newprice, String newcolor, String newdescription, int newquantityinstock)

{

ShirtID=newShirtID;

Price=newprice;

Description=newdescription;

Color=newcolor;

Quantityinstock=newquantityinstock;

System.out.println("Shirt number: "+ShirtID+ "\nPrice: "+price+"\nDescription: "+description+"\nColor:"+color+"\nQuantity in Stock: "+quantityinstock);

System.out.println("Information has been added");

}

public void removestock(int newShirtID)

{

ShirtID=newShirtID;

System.out.println("The Shirt number "+ShirtID+" has been remove from the stock");

}

public void displayshirtinformation()

{

System.out.println("Shirt number: "+ShirtID+" \nPrice: "+price+" \nDescription: "+description+" \nColor: "+color+" \nQuantity in Stock: "+quantityinstock);

}

}

/*Programmer: Cristine A. Salac

Program Name:FormOfPayment class (Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

public class FormOfPayment

{

//variable declaration

private int Checknumber;

private int Creditcardnumber;

private String Expirationdate;

// constructor Definiton

public FormOfPayment()

{

}

public FormOfPayment(int c, int ccn, String e)

{

Checknumber=c;

Creditcardnumber=ccn;

Expirationdate=e;

}

public void verifyCreditcard()

{

System.out.println("Your credit card is OK ");

}

public void verifyCheckpayment()

{

System.out.println("You must send the Check first to our company so that we can send your orders");

}

}

/*Programmer: Cristine A. Salac

Program Name:Clothing Tester(Direct Clothing Case Study)

Date Started: 02/08/09

Date Finished: 02/09/09

Program Purpose:To create a code for the purpose of our midterm exam.

*/

import java.util.Scanner;

public class Clothing Tester

{

public static void main(String args[])

{

int Y;

System.out.println("Welcome To Direct Clothing Case Study");

{

Scanner input=new scanner(System.in);

Shirt myShirt=new Shirt();

System.out.println("Welcome to Direct cothing");

System.out.println("Here's our Catalog:");

myShirt.displayCatalog();

Scanner input=newScanner(System.in);

System.out.println("Do you want to add a stock?");

if(input=='Y')

{

system.out.println("Enter ShirtID:");

String ID=input.nextLine();

myShirt.setShirtID(ID);

System.out.println("\n");

System.outprintln("Enter Price:");

double price-input.nextDouble();

myShirt.setPrice(price);

System.out.println("\n");

System.out.println("Enter Color:");

String color=input.nextLine();

myShirt.setColor(color);

System.out.println(\n");

System.out.println("Enter Quantity:");

int quantity-input.nextInt();

myShirt.setQuantity(quanity);

System.out.println("\n");

System.out.println("Enter shirt Description:");

String description=input.nextLine();

myShirt.setDescription(description);

System.out.println("\n");

System.out.println("Done?");

if(input=='Y')

{

System.out.println("The shirt has been added.");

}

else

{

System.out.println("Ok");

}

}

}

}

Wednesday, February 4, 2009

IT134A_DDS

/*Programmer:Cristine A. Salac
Date Started:02/04/09
Date Finished: 02/04/09
Program Name: Cube
Program Purpose: To create classes using Visibility Modifiers.
*/

public class Cube{

// variable declarations

private double width;
private double length;
private double height;
private double volume;
private double area;

// constructor declaration

public Cube(double w, double h, double l) {

width=w;
height=h;
length=l;
}

public Cube() {
}
// method declarations
private double volume() {


return (width*length*height);
}
private double area() {

return (width*length);
}
public void setDimension(double newLength, double newHeight, double newWidth) {


length=newLength; width=newWidth; height=newHeight;
}
public void displayCube() {


System.out.println("Cube Dimensions:");
System.out.println("The VOLUME of the Cube is" +volume());
System.out.println("The AREA of the Cube is" +area()); }
}
***********************************************************************
/*Programmer:Cristine A. Salac
Date Started:02/04/09
Date Finished: 02/04/09
Program Name: CubeTester
Program Purpose: To create classes using Visibility Modifiers.
*/

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

double l;
double w;
double h;

System.out.println("\n\n");
System.out.println("The Cube object with a parameter");
Cube firstCube=new Cube(2,2,2);

firstCube.displayCube();

System.out.println("The Cube object without a parameter");
System.out.println("\n\n");

Scanner a=new Scanner(System.in);
System.out.println("enter the value of the length:");
l=a.nextDouble();

Scanner b=new Scanner(System.in);
System.out.println("enter the value of the height:");
w=b.nextDouble();

Scanner c=new Scanner(System.in);
System.out.println("enter the value of the width:");
h=c.nextDouble();

System.out.println("The Cube object without a parameter");

System.out.println("\n\n");
Cube secondCube=new Cube();
secondCube.setDimension(l,w,h);
secondCube.displayCube(); }
}