Wednesday, March 18, 2009

Exercise 1- Reverse Word

//ProgrammerName: Joan N. Dominguez
//ProgramName: ReverseSent
//Purpose: Programming exercise which display the original line/words and reversed the words/line that have been entered by the user 
//Date: March 17, 2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon

import javax.swing.*;


public class ReverseSent {

public static void main(String[] args) 
{
String ReverserWord = JOptionPane.showInputDialog("Enter a line to be reverse : ");//input an original words or phrase
String ReverserWord2 = ReverserWord.substring(ReverserWord.indexOf(" "),ReverserWord.length());
String reverseWord = new StringBuffer(ReverserWord2).reverseWord().toString();
String ReverserWord3 = ReverserWord.substring(0,ReverserWord.indexOf(" "));
String reverseWord2 = new StringBuffer(ReverserWord3).reverseWord3().toString();
    System.out.println("\nOriginal input: " + ReverserWord);// Print the original string
    System.out.println("Reversed input: " + reverseWord2 + " "+ reverseWord);//Print the string in reversed order
}
    }

        }

-----

//A sample output of this program must be....






Original input: Have a good summer vacation!

Reversed input: evaH !noitacav remmus doog a 


note: this program is modified from the web.



Tuesday, March 17, 2009

Exercise 4 - Name Echo

//ProgrammerName: Joan N. Dominguez

//ProgramName: NameEcho
//Purpose: Programming exercise
//Date: March 17,2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon

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/Fname    
 String FN = name.substring(name.indexOf(" "),name.length()).toUpperCase();
 
 //second input/Secondname    
 String SN = name.substring(0,name.indexOf(" "));


 // Print the Input names
 System.out.print(SN);
 System.out.println(FN);

 }

}
------

//output of this program

Enter your name:

Joan Dominguez

Joan DOMINGUEZ


Exercise 5 - Greeting

//ProgrammerName: Joan N. Dominguez
//ProgramName: HelloObject
//Purpose: Programming exercise with the application of the previous lesson in java programming
//Date: March 17,2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon


import java.util.Scanner;

public class HelloObject {

  public static void main(String[]args) {
      Scanner a = new Scanner(System.in);
      String greeting;
          System.out.print("Enter your Greeting: "); //it is where the greeting that is printed by the object given by the user.
          greeting = a.nextLine();
          System.out.println("\n");
          System.out.print(greeting);
          System.out.println();
  }
   
   

}

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

//Output of this program:

Enter Greeting: 

Hello Sir!

Hello Sir!


Friday, March 6, 2009

User-Friendly Division by: Joan N. Dominguez

//ProgrammerName: Joan N. Dominguez
//ProgramName: DivisionPractice
//Purpose: Programming exercise with the application of the previous lesson in java programming
//Date: March 06,2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon

import java.util.Scanner;

public class DivisionPractice {

  private static int quotient(int numerator, int denominator) {
  //throws Arithmetic Exception
  if (denominator == 0)
  throw new ArithmeticException();
  return(numerator / denominator);
  }
  public static void main(String args[]){
      
      Scanner input = new Scanner(System.in);
      
  int number1=0, number2=0, result=0;
  String snum;
  String sdiv;
  char x = ' ';
  //determine if the user wants to continue or quit
  while( ( x != 'q') || ( x != 'Q' )) {
  try {
         System.out.print("\nEnter the numerator: ");
  snum = input.next();
  x = snum.charAt(0);
  if( (x == 'q') || ( x == 'Q') )
      System.exit(-1);
  number1 = Integer.parseInt(snum);
  System.out.print("Enter the divisor: ");
  sdiv = input.next();
  number2 = Integer.parseInt(sdiv);
  result = quotient(number1,number2);
  System.out.print(number1 + " / " + number2+" is "+result);
  }
  //cathes the Arithmetic Exception it detects
  catch (Exception e) {
  System.out.println(e.toString());
  System.out.println("You can't divide "+number1+" by "+number2);
  }
  }
}
   
}

Monday, March 2, 2009

Iteration_ArrayList Exer

//ProgrammerName: Joan N. Dominguez
//ProgramName: Iterator_arraylist
//Purpose: To show how to iterate through the elements of java ArrayList object using Iterator.
//Date: March 02,2009
//Subject: Computer Programming 3
//Instructor: Dony Dongiapon


 
import java.util.ArrayList;
import java.util.Iterator;
 
public class Iterator_arraylist {
 
  public static void main(String[] args) {
 
  //create an ArrayList object
  ArrayList arrayList = new ArrayList();
 
  //Add elements to Arraylist
  arrayList.add("1");
  arrayList.add("2");
  arrayList.add("3");
  arrayList.add("4");
  arrayList.add("5");
 
  //get an Iterator object for ArrayList using iterator() method.
  Iterator itr = arrayList.iterator();
 
  //use hasNext() and next() methods of Iterator to iterate through the elements
  System.out.println("Iterating through ArrayList elements...");
  while(itr.hasNext())
  System.out.println(itr.next());
 
  }
}
 

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


//The  output would be


Iterating through ArrayList elements...
1
2
3
4