Wednesday, March 18, 2009

Java exercises

Exer 1 

//programmer: junnaliza Arreglado 
//programme name: hello object program 
// date started: march 17, 2009 
// date ended: march 17, 2009 
//purpose: manipulate String.. 

import javax.swing.*;

public class wordReverse {
  public static void main(String[] args) {

  String input; // Used for the input string.
  String reversed; // Reversed form or the input string.

  input = JOptionPane.showInputDialog(null, "Enter a string"); // get the string from the user
  reversed = "";
  for (int i=0; i  reversed = input.substring(i, i+1) + reversed;
  }
  JOptionPane.showMessageDialog(null, "Reversed:\n" + reversed); //the output of the reveresed string from the user
  }
}

sample output: 

enter a String:

Go to the main menu

unem niam eht ot oG


__________________________________________________________________

exer4


//programmer: junnaliza Arreglado 
//programme name: hello object program 
// date started: march 17, 2009 
// date ended: march 17, 2009 
//purpose: manipulate String. 

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

public class nameEcho {  
 public static void main(String[] args) throws IOException  
 {  
 String yourName; 
 String firstName; 
 String lastname; 
 System.out.println("Enter your name:");  
 Scanner input = new Scanner(System.in);  
yourName = input.nextLine();  
firstName = yourName.substring(0,yourName.indexOf(" "));  
lastname= yourName.substring(yourName.indexOf(" "),yourName.length()).toUpperCase();  
 System.out.println();  
 System.out.print(firstName); 
 System.out.print(lastname); 
  System.out.println();  
 }  




sample output: 

Enter your name: 
junnaliza arreglado 

junnaliza ARREGLADO 

Process completed. 




__________________________________________________________________ 
Exer 5 


//programmer: junnaliza Arreglado 
//programme name: hello object program 
// date started: march 17, 2009 
// date ended: march 17, 2009 
//purpose: manipulate inputs.. 

import java.util.Scanner; 
public class helloObject  

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



sample output:

Enter Greeting: 
hello mars! 

hello mars! 

Process completed

Saturday, March 7, 2009

User-Friendly Division by:Junnaliza Arreglado

//Programmer: Junnaliza Arreglado
//Date Started: March 6,2009
//Date Finished:March 7,2009
//Programme Name: User-Friendly Division
//Purpose: To use Exception handling in the Programme


import java.util.*; //importing the java util package that holds the InputMismatch and the Scanner class

public class jhong
{

public static int quo(int num, int div)throws ArithmeticException
{
return num/div; //holds the aithmetic exeption of the try block
}

public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num=0;
int div=0;
String quit="exit";
char x=quit.charAt(0); //String class initialization
while(x!='q' || x!='Q') //to initialize the quit of the program
{
if(x!='q' || x != 'Q') //to initialize the quit of the program
{
try //starting the try block
{
System.out.println("Enter the numerator:");
if((input.next().charAt(0)=='q')||(input.next().charAt(0)=='Q'))
{
x=input.next().charAt(0);
}
else
{
num=input.nextInt();
System.out.println("Enter the divisor:");
div=input.nextInt();
int finalQ=quo(num,div);
System.out.println(num+"/"+div+" is "+finalQ);
}
} //end of the try block


catch(ArithmeticException wrongDiv) //wrong denominator-the 0
{
System.err.println("You can't divide "+num+" by "+div); //error message
}

catch(InputMismatchException stringNum) //catch for wrong input of the numerator
{
System.err.println("You entered bad data.\nPlease try again."); //error message
}


}
else
{
x='q'; //quiting
}
}

}
}

Monday, March 2, 2009

ArrayList and Iterators

// programmer name: Arreglado, Junnaliza
// date started: march 2, 2009
// date finished: march2, 2009
// prgramme name: making array ang traversing through the use of iterators


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


public class Ar_iter
{
public static void main(String args[])throws Exception
{
ArrayList list = new ArrayList(); //importing an arraylist class within the util package and naming an object list

list.add("jhong");
list.add("mhing");
list.add("yhang"); //initialization of the arraylist
list.add("jhen");
list.add("lhyn");
list.add("phau");

System.out.print("the contents of the arraylist list are: ");
Iterator iterator1 = list.iterator(); //the class iterator making an object iterator1
while (iterator1.hasNext())
{ //iterator method hasNext()-used to traverse next values or elements
String values = iterator1.next();
System.out.print(values + " "); //used to print out the values of the array list using an iterator
}
System.out.println();

ListIterator listanditerate = list.ListIterator(); //making an object from the listiterator class of java naming it as listanditerate
while (listanditerate.hasNext())
{
String values = listanditerate.next(); //traversing while the list has still an element-next element
}


System.out.print("making a backward list of the values within the arraylist: ");
while (listanditerate.hasPrevious())
{
String values = listanditerate.previous(); //traversing the vales while it has a previous element
System.out.print(values " "); //printing the values of the list while it has a previous element
}
}
}

note: The code is just modified. It came from the web.
Date: March 2, 2009

Things that i learned:
  • To create an list of array, we should import the UTIL package of the java technology
  • The Arraylist package has several useful method
  • One class that is used to manipulate the arraylist is the iterators.
  • To use the iterators we need to import also the iterrator class of the util package.
  • Iterators also has useful methods (some are used in the code)
  • arraylist and iterators is very useful in making codes or even in the real world application.