Wednesday, March 18, 2009

Exercises

Exer5—Greeting—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: This program inputs user’s greetings and prints out the greeting.*/


import java.util.Scanner;
public class Greetings {

public static void main(String[] args) {


Scanner a= new Scanner(System.in);
String mygreet;
System.out.println("Enter your Greetings:");// prompts user to enter greetings
mygreet= a. nextLine();

System.out.println(" ");
System.out.println(" "+mygreet);// prints ouy the greetings


}
}
Exer2—Color Cycle—
/*Programmer: Ana Mae B. Respecia
Date started: March 13, 2009
Date ended: March 18, 2009
Description: Program that when you click 1 button the color will interchange to green, blue, gray, red */

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 Button extends JPanel implements ActionListener {private static boolean USE_CROSS_PLATFORM_UI = false;int buttonLabelIndex = 0;String buttonLabels[] = { "Green", "blue", "Gray", "Red" };Color buttonColors[] = { Color.GREEN, Color.BLUE, Color.GRAY, Color.RED,};JButton button;public Button() {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(20,20,20,20));}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("Button Demo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JComponent contentPane = new Button();contentPane.setOpaque(true);frame.setContentPane(contentPane);frame.pack();frame.setVisible(true);}public static void main(String[] args) {run();}}Exer3 – Combination Lock—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: this program is labeled 0-9 and if the user enters the combination 7-3-5 the program will exit otherwise it will turn to red.*/

public class combi extends JFrame{  String[] password={"8","4","9"};  int z=0,a=0;
  JButton buttons[];
  JPanel buttonPanel;
  Container container = getContentPane();

  public combi(){
  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));


public CombinationLock () {

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


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


}
}



Exer4—Name Echo—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: This program inputs user’s name and will turn the second name into Uppercase letter .*/

import java.util.*;import java.io.*;

public class Upper{
 public static void main(String[] args)throws StringIndexOutOfBoundsException{
  try{

public String wafa;

  Scanner ana = new Scanner(System.in);
  //ask the user to input some text
  System.out.println("Enter your name:");
  //get the data unput by the uset and store it in variable ana.
  String ana= wafa.nextLine();
  //get the " " through indexOf method
  int c=name.indexOf(" ");
  int a=c+1;
  //store the first name entered by the user to index output
  String output = name.substring(0,c);
  //store the second name entered to index 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.");
  }
 }
}



Exer 1—Word Reverser--

/*Programmer: Ana Mae B. Respecia
Date started: March 13, 2009
Date ended: March 18, 2009
Description: Program that reads sentences from the user and prints out each word reversed.*/
import java.util.*;

public class WordReverser{

public static void main(String args[]){

String reverse;
String ana;
Scanner scan=new Scanner(System.in);
System.out.println("Input:");
reverse=scan.nextLine();// input user to enter the desired word
StringTokenizer st=new StringTokenizer(ana) ;
while (st.hasMoreTokens()) {
ana=st.nextToken();// reverse and read each word
ana=new StringBuffer(ana).reverse().toString();// pass to so that the word will be reverse
System.out.print(ana+" ");// prints out the reversed output

}

}
}

Saturday, March 7, 2009

User Friendly Division

/*Programmer:Ana Mae B. Respecia
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask denominator and numerator and will result to quotient. Then if the user enter 'Q'/'q' the program will quit.*/

import java.util.*; //all  utilities of java 

public class DivisionPractice
{

//initialize a numerator and divisor to compute for the users interaction
public static double qoutient(double numerator,double divisor)
throws ArithmeticException
{
return numerator/divisor;
}

public static void main(String[] args){
Scanner scanner=new Scanner(System.in);

String ana = "ana";
char x=ana.charAt(0);

// get of character ana and stores in the x
while(x!='q'||x!='Q'){

// in this condition if the user enters the keyword q or Q the program will automatically exit 

System.out.print("Please enter an integer numerator:");

// this prompt user to enter the numerator
String num=scanner.next();


try{
if(num.charAt(0)=='q'||num.charAt(0)=='Q'){
 System.exit(1);

// the program will exit if the users enter q or Q if not the program will continue until the user enters the keywords
}else{

// stores the integer into the variable num and convers into the integer
double numerator=Integer.parseInt(num);

// prompt user to enter the divisor
System.out.print("Enter the divisor:");
double divisor=scanner.nextInt();
// will compute and prints out the result of the given numerator and denominator
double result=qoutient(numerator,divisor);
System.out.printf("\n Result: "+numerator+"/"+divisor+" is "+result+"\n");
}
}

// this catch block will prompt to the user if the user enters  the value 0
catch(InputMismatchException inputMistmatchException)
{
double numerator=Integer.parseInt(num);
System.err.println("You can't divide "+numerator+" by 0");
System.err.printf("\n Exception: \n");
System.out.println("You must enter integers.Please try again\n");
}

// this will prompt the user that the user enters Alpha values for ex abcdef and so on
catch(NumberFormatException inputMistmatchException)
{
System.err.printf("\n Exception: ");
scanner.nextLine();
System.err.println("Alphanumeric is an invalid denominator.Please try again\n");

System.out.println("You must enter integers.Please try again\n");
}

catch(ArithmeticException arithmeticException)
{
System.err.printf("\nException:%s\n",arithmeticException);
scanner.nextLine();
System.err.println("Zero is an invalid denominator.Please try again\n");
System.out.println("You must enter integers.Please try again\n");
}

catch(IllegalFormatConversionException illegal){
System.out.println(" ");
}
}
}
}



Sunday, March 1, 2009

Programmer: Ana Mae B. Respecia

Title of Code: ArrayListNumbers

Description: Enables Array  List and this program executes the hasPrevious that enables letters to form backwards...


CODE:

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


public class ArrayListABCD{
  public static void main(String args[]) {
  ArrayList al = new ArrayList();

  al.add("1");
  al.add("2");
  al.add("3");
  al.add("4");
  al.add("5");
  al.add("6");

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


OUPUT OF THE CODE



Date: March 02, 2009

What i have learned:

  • ArrayList and Iterator are classes of java.util.
  • ArrayList cannot hold int.
  • Iterator is a class that can manipulate ArrayList
  • ArrayList is flexible, no need to specify an index.


Sunday, February 8, 2009

Direct Clothing Case Study Feb 8,2009

/*** PRogrammer: Ana Mae Respecia
Name of Programm: Catalog Class (Case study of direct Clothing)
Date Strated : January 6,2009
Date Ended: January 8,2009
Purpose: To run a solution for the direct clothing order ***/

public class Catalog
{

/** declaring my variables

private int shirtid;
private String color;
private double price;
private int quantityinstock;
private String description;

//costructor declaration

public Catalog()
{
}

public Catalog(int s, double p, String d,String c, int q)
{
shirtid=s;
color=c;
price=p;
description=d;
quantityinstock=q;
}


public void addshirtStock(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+"\nColor: "+color+"\nDescription: "+description+"\nQuantity in Stock: "+quantityinstock);

System.out.println("Successful adding");



}
}

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


/*** PRogrammer: Ana Mae Respecia
Name of Programm: Shirt Class (Case study of direct Clothing)
Date Strated : January 6,2009
Date Ended: January 8,2009
Purpose: To run a solution for the direct clothing order ***/


public class Shirt
{ // declaring variables
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 void addorder(int newshirtid) // constrcutor for adding order
{
shirtid=newshirtid;
System.out.println("You added Shirt number "+shirtid+" to your order");
}

public void cancelorder(int newshirtid)
{
shirtid=newshirtid;
System.out.println("Shirt number " +shirtid+ "is cancelled from your orders");
}


/*** PRogrammer: Ana Mae Respecia
Name of Programm: Class for Format of Payements (Case study of direct Clothing)
Date Strated : January 6,2009
Date Ended: January 8,2009
Purpose: To run a solution for the direct clothing order ***/


public class FormofPayment
{

private int checknum;
private int cardnum;
private String expiredate;

public FormOfPayment(); // constructor
{
}

public FormofPayment(int newcnum,int newcdnum,String newexdate)
{
checknum=newcnumo;
cardnum=newcdnum;
expiredate=newexdate;
}

public String verifyCardno(int newcardno)
{
if(cardno==newcdnum){
cardum=newcdnum;
return String.format("Your credit card number is verified!");}

{
else{
Sting.format("Invalid CardNumber pls check")
return String.Format()
}

public String verifyChekPayment(int newcheckno)
{
if(checknum==newcnum){
checknum=newcnum;
return String.format("Your check number is verified!");}
{
else{
Sting.format("Check number not verified system error pls check")
return String.Format()
}

}


/*** PRogrammer: Ana Mae Respecia
Name of Programm: Class for Order(Case study of direct Clothing)
Date Strated : January 6,2009
Date Ended: January 8,2009
Purpose: To run a solution for the direct clothing order ***/

public class Order{

// declaring variables
private int orderid;
private double totalprice;
private String status;


// constructor

public order ();
{
}


public Order(int neword, double newTp, String newstat)
{
orderid=ord;
totalprice=tp;
status=stat;
}
//Method
{

public void cancelorder()
{
orderid=ord;
System.out.print("Order is cancled"+ord);
}

public void addorder();
{
System.out.println("The order is added"+ord0;
}
}