Thursday, March 19, 2009

Java exercises

EXER 1
/*Programmer: Gerlie O. Lidon
Program Name: Word Reverser
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which the words that you enter will reverse with the words and 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

***********************************************************************************

EXER 2

/*Programmer: Gerlie O. Lidon
Program Name: Color Cycle
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which there is only one button in the frame 
*/

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(250,205);
setLocation(100,100);
setTitle(getClass().getName());

addWindowListener(wt);

green = new JButton("Red");
blue = new JButton("Green");
gray = new JButton("Blue");
red = new JButton("Gray");
green.addActionListener(this);
blue.addActionListener(this);
gray.addActionListener(this);
red.addActionListener(this);
cp = getContentPane();
cp.setBackground(Color.black);
cp.setLayout(new FlowLayout());
cp.add(green);
cp.add(blue);
cp.add(gray);
cp.add(red);
show();
}
public void actionPerformed(ActionEvent e) {
JButton s = (JButton) e.getSource(); //get the source of the event

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 == red) cp.setBackground ( Color.red);

cp.repaint();
}

public static void main (String[] args) {

JFrame f = new ColorCycle();
}

}

class WindowTerminator extends WindowAdapter {

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

***********************************************************************************
EXER 3
/*Programmer: Gerlie O. Lidon
Program Name: Combination Lock
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which in a frame with ten buttons, labeled 0 through 9. If the user exit the program, he/she must click on the correct three buttons in order. 
*/
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("a"));
  cp.add(new JButton("b"));
  cp.add(new JButton("c"));
  cp.add(new JButton("d"));
  cp.add(new JButton("e"));
  cp.add(new JButton("f"));
  cp.add(new JButton("g"));
  cp.add(new JButton("h"));
  cp.add(new JButton("i"));
  cp.add(new JButton("j"));
  cp.setBackground(Color.black);
  pack();
}
}

***********************************************************************************
EXER 4
/*Programmer: Gerlie O. Lidon
Program Name: Name Echo
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which it will ask the user's name and the name that is being enter will writes back with the first name as entered and the second name all in capital
*/

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

***********************************************************************************
EXER 5 
/*Programmer: Gerlie O. Lidon
Program Name: Hello
Date Started: March 16, 2009
Date Finished: March 19, 2009
Program Purpose: To be able to make a program in which a version of the HelloObject program the greeting that is being printed by the object is given by the use.
*/

import java.util.Scanner;
public class Hello
{
public static void main(String[] args)
{
String greetings; // Used for the input string.
Scanner input=new Scanner(System.in);
System.out.println("Enter Greeting:");
greetings=input.nextLine();
System.out.println();
System.out.println(greetings);
}
}

Friday, March 13, 2009

Exception Handling( User-Friendly Division)

Programmer: Gerlie O. Lidon 
Program Name: User-Friendly Division
Time Started: March 6,2009 
Time Finished: March 13, 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;  
  }  
  public static void main( String args[] ) 
  { 
  Scanner scanner = new Scanner( System.in );  
  boolean continueLoop = true; // determines if more input is needed 
  {  
  {  
  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;  
  }  
  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”);  
  }  
  catch ( ArithmeticException arithmeticException )  
  {  
  System.err.printf( "\nException: %s\n", arithmeticException ); 
  System.out.println( "Zero is an invalid denominator. Please try again.\n" ); 
  }  
  } while ( continueLoop );  
  } 
  }


Thursday, March 5, 2009

ArrayListIterator

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 have learned

Arraylist is an enhanced version of array.

Arraylist does not specifiy index.

Arraylist is more flexible than array.

Iterators is use to manipulater arraylist.

Monday, February 9, 2009

Direct Clothing Case Study Solution

/*Programmer: Gerlie O. Lidon
Date started: Feb 7, 2009
Date ended: Feb 8, 2009
Program name:catalog
Program purpose: To create a solution for direct clothing
*/

public class Catalog
{

//variable declaration

private int ShirtID;
private double Price;
private String Color;
private String Description;
private int Quantityinstock;

//costructor declaration

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 declaration

public void addashirt(int newShirtID, double newPrice, String newColor,String newDescription, int newQuantityinstock)
{

ShirtID=newShirtID;
Price=newPrice;
Color=newColor;
Description=newDescription;
Quantityinstock=newQuantityinstock;

System.out.println("Shirt number: "+ShirtID+ "\nPrice: "+Price+"\nDescription: "+Description+"\nColor: "+Color+"\nQuantity: "+Quantityinstock);
System.out.println("Thank you it was succesfully added");
}

public void removeaShirt(int newShirtID)
{
ShirtID=newShirtID;
System.out.println("The Shirt number"+ShirtID+"is succesfully remove ");
}

}

******************************************************************************
/*Programmer: Gerlie O. Lidon
Date started: Feb 7, 2009
Date ended: Feb 8, 2009
Program name: Customer
Program purpose: To create a solution for direct clothing
*/

public class Customer
{

//customer variable declaration

private int CustomerID;
private String Name;
private String Address;
private int Phonenumber;
private String Emailaddress;


//customer constructor declaration

public Customer()
{

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

{

CustomerID=c;
Name=n;
Address=a;
Phonenumber=p;
Emailaddress=e;

}

//customer method declarations

public void customergreetings(String newname)
{
Name=newname;
System.out.println("Welcome" + Name + " To DIRECT CLOTHING Shop");
}

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 Informations");
System.out.println( "\nCustomerID: "+CustomerID+" \nName: "+Name+" \nAddress: "+Address+" \nPhonenumber: "+Phonenumber+" \nEmailaddress: "+Emailaddress);
}

}
***************************************************************************
/*Programmer: Gerlie O. Lidon
Date started: Feb 7, 2009
Date ended: Feb 8, 2009
Program name: Order
Program purpose: To create a solution for direct clothing
*/

public class Order
{

//variable declaration

private int OrderID;
private double Totalprice;
private String Status;

//constuctor declaration

public Order()
{

}

public Order(int o, double tp, String s)
{

OrderID=o;
Totalprice=tp;
Status=s;
}

//method declaration..

public void placeorder(int newo, double newtp, String news)
{

OrderID=newo;
Totalprice=newtp;
Status=news;
}

public void removeorder(int o)

{

OrderID=o;
System.out.println("Order number: "+OrderID+" is succesfully cancelled...Thank you!");
}

public void submitOrder()
{
System.out.println("Order number is succesfully submitted the "+OrderID);
}

public void putorderonhold()
{
System.out.println("The order is still to be proccess.\n Wait for a while for the reply. Thank you!");
}

}
******************************************************************************
/*Programmer: Gerlie O. Lidon
Date started: Feb 7, 2009
Date ended: Feb 8, 2009
Program name: Shirt
Program purpose: To create a solution for direct clothing
*/


public class Shirt
{
//variable declaration

private int ShirtID;
private double Price;
private String Description;
private String Color;
private int Quantityinstock;

//constructor declaration

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 declaration

public void addshirttoorder(int newShirtID)
{

ShirtID=newShirtID;
System.out.println("You have added Shirt number "+ShirtID+" to your orders");
}

public void removeshirttoorder(int newShirtID)
{
ShirtID=newShirtID;
System.out.println("Shirt number " +ShirtID+ "is successfully remove from the orders");
}

public void displayshirtinformation()
{
System.out.println("Shirt number: "+ShirtID+" \nPrice: "+Price+" \nDescription: "+Description+" \nColor: "+Color+" \nQuantity in Stock: "+Quantityinstock);
}

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("Your information has been succesfully added");
}

public void removestock(int newShirtID)
{

ShirtID=newShirtID;
System.out.println("The Shirt number "+ShirtID+" is succesfully remove from the stock");
}

}
******************************************************************************
/*Programmer: Gerlie O. Lidon
Date started: Feb 7, 2009
Date ended: Feb 8, 2009
Program name: Form of Payment
Program purpose: To create a solution for direct clothing
*/

public class FormOfPayment
{

//variable declaration...

private int Checknumber;
private int Creditcardnumber;
private String Expirationdate;

// constructor declaration..

public FormOfPayment()
{

}

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

{

Checknumber=c;
Creditcardnumber=cc;
Expirationdate=e;
}

public void verifyCreditcard()
{
System.out.println("Your credit card is OK ");
System.out.println("We will less your the amount from your credit card");
}

public void verifyCheckpayment()
{
System.out.println("You must send the a Check in our company before we are going to Send your orders! Thank you!");
}

}

*******************************Thank You!!!**********************************

Wednesday, February 4, 2009

IT134A_DDS

/*Programmer: Gerlie O. Lidon
Date started: 02-04-2009
Date ended: 02-04-2009
Program name: Cube
Program purpose: To create classes using Visibility Modifiers.*/

public class Cube{
private double length;
private double width;
private double height;
private double volume;
private double area;
public Cube(double l, double w, double h)
{
l=length;
w=width;
h=height; }
public Cube()
{
}

private double volume()
{
return (length*width*height);
}
private double area()
{
return (length*width);
}
public void setDimension(double newLength, double newwidth, double newheight)
{
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: Gerlie O. Lidon
Date started: 02-04-2009
Date ended: 02-04-2009
Program name: CubeTester1
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();
}
}