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

No comments:

Post a Comment