Wednesday, March 18, 2009

JAVA Exercises

Exer 1

/*programmer: Bocong, Wendel B.
programme name: Reverse Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Reversing String*/
import java.util.Scanner;

public class Reverse {

public static void main(String args[]){
Scanner a=new Scanner(System.in);
String wendel;
System.out.printf("%s", "Enter the String: ");
wendel=a.nextLine();
wendel = new StringBuffer(wendel).reverse().toString();
System.out.println("Reversed String : " + wendel);
}
}

Exer 2

/* Exer 2
Programmer: Bocong, Wendel B.
Programme name: Color Cycle
Date Started: March 17, 2009
Date Ended: March 25, 2009
Purpose: To have a java code that manipulate the color backgroud of the Frame.
*/
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class ColorCycle extends JPanel implements ActionListener {

private static boolean USE_CROSS_PLATFORM_UI = false;

int buttonLabelIndex = 0;
String buttonLabels[] = { "black", "blue", "orange", "yellow" };
Color buttonColors[] = { Color.BLACK, Color.BLUE, Color.ORANGE, Color.YELLOW,};

JButton button;
public ColorCycle() {
super(new BorderLayout());
button = new JButton(buttonLabels[buttonLabelIndex]);
if(USE_CROSS_PLATFORM_UI) {
button.setBackground(buttonColors[buttonLabelIndex]);
} else {
button.setForeground(buttonColors[buttonLabelIndex]);
}
button.addActionListener(this);
this.add(button, BorderLayout.CENTER);
this.setBorder(BorderFactory.createEmptyBorder(25,25,25,25));
}
public void actionPerformed(ActionEvent e) {
buttonLabelIndex = ++buttonLabelIndex < buttonLabels.length?buttonLabelIndex:0;
button.setText(buttonLabels[buttonLabelIndex]);
this.setBackground(buttonColors[buttonLabelIndex]);
button.setBackground(buttonColors[buttonLabelIndex]);
}
private static void run() {
if(USE_CROSS_PLATFORM_UI) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
JFrame frame = new JFrame("Color Cycle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent contentPane = new ColorCycle1();
contentPane.setOpaque(true);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
run();
}
}


Exer 3


/* Exer 3
Programmer: Bocong, Wendel B.
Programme name: Combination Lock
Date Started: March 17, 2009
Date Ended: March 25, 2009
Purpose: To have a java code that will let the user press the button and if the user click the the wrong one in three
consecutive answer the color background will turn into red.
*/

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

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


Exer 4

/*programmer: Bocong, Wendel B.
programme name: UpperCase Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Converting the input of the User and convert it to Upperase*/
import java.util.*;
import java.io.*;
public class UpperCase {

public static void main(String[] args) throws IOException
{
Scanner a=new Scanner(System.in);
String wen;
String del;
String boc;
System.out.printf("%s", "Enter the Name: ");
wen=a.nextLine();
del = wen.substring(0,wen.indexOf(" "));
boc= wen.substring(wen.indexOf(" "),wen.length()).toUpperCase();
System.out.println(" "+del+ " " +boc);
}
}


Exer 5

/*programmer: Bocong, Wendel B.
programme name: Hello Program
Date started: march 17, 2009
Date ended: march 19, 2009
Purpose: Hello Program */
import java.util.Scanner;
public class Hello{

public static void main (String args[]){
Scanner wendel=new Scanner(System.in);
String greeting;
System.out.printf("%s", "Enter your Greeting: ");
greeting=wendel.nextLine();
System.out.printf("\n");
System.out.printf("%s",greeting);
}
}

Monday, March 9, 2009

User-Friendly Division By: Wendel B. Bocong

/*
Programmer: Wendel B. Bocong
Date Started: March 6,2009
Date Ended: March 9, 2009
Description:This program will calculate the quotient, then if you enter any character it will return an error message.
If the user will press q it will exit.
Purpose: To know more about different Exceptions.
*/
import java.util.*;
import java.io.*;

public class DivisionPractice{
public static double quotient(double numerator,double divisor)throws ArithmeticException{
return numerator/divisor;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double numerator,divisor,answer;
String end = "tech";

char wen=end.charAt(0);

do{
try{
System.out.print("\n Numerator: "); //entering numerator from the user
String del=scan.next(); //this will get the value of numerator as String
char boc=del.charAt(0); //using the method charAt()

if(boc=='q'||boc=='Q'){
System.exit(0);
}else{
numerator=Integer.parseInt(del);
System.out.print("\nDivisor: ");//entering divisor from the user
String cong=scan.next();
divisor=Integer.parseInt(cong);
answer=quotient(numerator,divisor);//calculating the quotient
System.out.print(numerator+"/"+divisor+" is "+answer);//displaying the result
System.out.println();
}
}
catch(ArithmeticException arithmetic){//this exception will catch all error input by the user
System.err.print("\nNothing to Divide because it is 0");//message that will display if the user try to divide the numerator into 0
}
catch(NumberFormatException inputMismatch){//this exception will catch all String input whether it is from numerator or divisor and display error message
System.err.print("\nUndesirable type of input.\nPlease try again.");
}
}
while(wen!='q'||wen=='Q');
}
}

Sunday, March 1, 2009

ArrayList/Iterators

/*** Programmer: Bocong, Wendel B.
Name of Program: ArrayList with IteratorsDate Started : March 2,2009
Date Ended: March 2,2009
Purpose: To try implementations that deals with ArrayList and Iterators.***/

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class iterators {
public static void main(String args[]) {
ArrayList<String> wen = new ArrayList<String>();

wen.add("Jessel Ann Abellana");
wen.add("Nerissa Agapay");
wen.add("Mixie Tulid");
wen.add("Rosedil Mobreros");
wen.add("Ana Mae Respecia");
wen.add("Wendel Bocong");

System.out.print("Original contents of wen: ");
Iterator<String> boc = wen.iterator();
while (boc.hasNext())
{
String element = boc.next();
System.out.print(element + " ");
}

System.out.println();

ListIterator<String> del= wen.listIterator();
while (del.hasNext())
{
String element = del.next();
del.set(element + "+");
}

System.out.println("Size of wen after additions: " + wen.size());
wen.remove(1);

{
System.out.println("Size of wen after deletions: " + wen.size());
System.out.println("Contents of wen: " + wen );
}
}
}


Things I learned in this exercise:

  • the implementation of array list is more comprehensive compared to array.

Sunday, February 8, 2009

DirecClothing Case Study

/*** Programmer: Bocong, Wendel B.Name of Program: Catalog Class (Case study of direct Clothing)
Date Started : February 6,2009

Date Ended: February 9,2009

Purpose: Seeking for an implementation for CASE STUDY OF DIRECT CLOTHING***/


public class Order{


private int Orderid;
private double Totalprice;
private String Status;

public Order()
{
}

public Order(int newoid, double newtot, String newsta)
{
Orderid=newoid;
Totalprice=newtot;
Status=newsta;
}


public void Ordercancel(int newoid)
{
Orderid=newoid;
System.out.print("Order has been cancelled"+Orderid);
}

public void addorder()
{
System.out.println("The following shirt has been added to your order"+Orderid);
}
}




/*** Programmer: Bocong, Wendel B.
Name of Program: Catalog Class (Case study of direct Clothing)
Date Started : February 6,2009
Date Ended: February 9,2009

Purpose: Seeking for an implementation for cASE STUDY OF DIRECT CLOTHING***/


public class Shirt
{
public int Shirtid;
private double Price;
private String Color;
private String Description;
private int Quantity;

public Shirt(int newsid, double newpri, String newcol, String newdes, int newqua)
{
Shirtid=newsid;
Price=newpri;
Color=newcol;
Description=newdes;
Quantity=newqua;
}

public void addstock(int newsid)
{
Shirtid=newsid;
System.out.println("You just new Shirt " +Shirtid+ " to your stock.");
}

public void cancelorder(int newsid)
{
Shirtid=newsid;
System.out.println("You just remove " +Shirtid+ " from your stock. ");
}
}



/*** Programmer: Bocong, Wendel B.
Name of Program: Catalog Class (Case study of direct Clothing)
Date Started : February 6,2009
Date Ended: February 9,2009

Purpose: Seeking for an implementation for CASE STUDY OF DIRECT CLOTHING***/


public class Catalog
{

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

public Catalog( )

{

}

public Catalog(int sid, double pri, String des,String col, int qua)
{
Shirtid=sid;
Color=col;
Price=pri;
Description=des;
Quantityinstock=qua;
}


public void addshirt(int newsid, double newpri, String newcol, String newdes, int newqua)
{
Shirtid=newsid;
Price=newpri;
Color=newcol;
Description=newdes;
Quantityinstock=newqua;

System.out.println("Shirt number: "+Shirtid);
System.out.println("\nPrice: "+Price);
System.out.println("\nColor: "+Color);
System.out.println("\nDescription: "+Description);
System.out.println("\nQuantity in Stock: "+Quantityinstock);

System.out.println("Add of Product is successful!");

}


public void removeashirt(int newShirtid)
{
Shirtid=newShirtid;
System.out.println("The Shirt number"+Shirtid+"is succesfully removed from the order");
}
}


/*** Programmer: Bocong, Wendel B.
Name of Program: Catalog Class (Case study of direct Clothing)
Date Started : February 6,2009
Date Ended: February 9,2009
Purpose: Seeking for an implementation for CASE STUDY OF DIRECT CLOTHING***/


public class Formofpayment
{

private int Checknumber;
private int Cardnumber;
private String Expiredate;

public Formofpayment( )
{
}

public Formofpayment(int newche,int newcar,String newexp)
{
Checknumber=newche;
Cardnumber=newcar;
Expiredate=newexp;
}

public void verifyCardnumber(int newche)
{
if(Cardnumber==newche){
Cardnumber=newche;
System.out.println("Card Number has been verified!");}

else{
System.out.println("Please Check because your card number is Invalid");
}
}
public void verifyCheckPayment(int newche)
{
if(Checknumber==newche){
Checknumber=newche;
System.out.println("Check Number has been Verified!");}

else{
System.out.println("Please Check because your Check Number is Invalid");

}
}
}


/*** Programmer: Bocong, Wendel B.
Name of Program: Catalog Class (Case study of direct Clothing)
Date Started : February 6,2009
Date Ended: February 9,2009
Purpose: Seeking for an implementation for CASE STUDY OF DIRECT CLOTHING***/


public class Customer {

  private int Customerid;
  private String Name;
  private String Address;
  private int PhoneNumber;
  private String emailadd;
   
 
 private void placeOrder(){
  System.out.print(Name+"\n, from "+Address);
  System.out.print("has a phone number "+PhoneNumber);
  System.out.print("\n and email add "+emailadd+" have an order.");
  Order bench = new Order(2,780.00,"available and in stock");
 }

 private void cancelOrder(){
  System.out.print("\nCancelled order!");
  Order bench = new Order();
 }

 public Customer(int cid, String cname, String cadd, int cnum, String cemail){
  Customerid = cid;
  Name = cname;
  Address = cadd;
  PhoneNumber = cnum;
  emailadd = cemail;
  this.placeOrder();
 }
  public Customer(){
  this.cancelOrder();
  }

}


Wednesday, February 4, 2009

// programmer:Wendel B. Bocong
// date started: February 4, 2009
// date ended: February 4, 2009
// Programme name: Cube Exercise
// programme purpose: Implementing Class Through Visibility Modifier in JAVA

import java.util.Scanner;
public class CubeTester{

  public static void main(String[] args) {
      
    double w;
    double e;
    double n;

      
      Cube myCube1 = new Cube(8,10,5);
      myCube1.displayCube();
    
    
    Scanner a=new Scanner(System.in);
    System.out.println("length:");
    w=a.nextDouble();

    Scanner b=new Scanner(System.in);
    System.out.println("height:");
    e=b.nextDouble();
    
    Scanner c=new Scanner(System.in);
    System.out.println("width:");
    n=c.nextDouble();

        Cube myCube2=new Cube();    
        myCube2.SetDimension(w,e,n);
        myCube2.displayCube();     
  }
   
}



// programmer:Wendel B. Bocong
// date started: February 4, 2009
// date ended: February 4, 2009
// Programme name: Cube Exercise
// programme purpose: Implementing Class Through Visibility Modifier in JAVA


public class Cube{

    private double length;
    private double width;
    private double heigth;
    private double volume;
    private double area;



    public Cube(double w, double e, double n)
    {
    length=w;
    heigth=e;
    width=n;
    }

    public Cube( )
    {

    }



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




  private double volume()
  {
    return(length*width*heigth);
  }


    public void SetDimension (double newlen, double newhei, double newwid)
    {
     length=newlen;
     width=newwid;
     heigth=newhei;
    }


  public void displayCube()
  {
      System.out.println("the area of the cube is " +area());
      System.out.println("the volume of the cube is " +volume());
  }
}