Friday, March 6, 2009

User Friendly Division by: Reuben D. Matiga

User-Friendly Division by: Reuben D. Matiga
//ProgrammerName: Reuben D. matiga
//ProgramName: DivisionPractice
//Purpose: To Learned more in java
//Date: March 06,2009
//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 num;
  String div;
  char x = ' ';
  //determine if the user wants to continue or quit
  while( ( x != 'q') || ( x != 'Q' )) 
{
  try {
  System.out.print ("\nEnter the numerator: ");
  num = input.next();
  x = snum.charAt(0);
  if( (x == 'q') || ( x == 'Q') )
  System.exit(-1);
  number1 = Integer.parseInt(num);
  System.out.print("Enter the divisor: ");
 div = input.next();
  number2 = Integer.parseInt(div);
  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);
  }
  }
}
   
}


Explanation of this code is, when you enter a value in numerator (any kind of value) and the value of you divisor is 0, the result is unknown because you can’t divide nay number in 0.

============================================================== 

No comments:

Post a Comment