Friday, March 6, 2009

User-Friendly Division

 
/* Programmer : Dandy A. Macaubos
  Program name : User Friendly Division
  Date Started : March 6,2009
  Date Finished : March 7,2009
  Purpose : to make a program friendly to the user 
  Instructor : Dony Dongiapon
  Subject : IT134
*/

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 y = ' ';

//determine if the user wants to continue or quit


  while( ( y != 'q') || ( y!= 'Q' ))

  {

  try 

  {

System.out.print(" WELCOME  TO MY USER FRIENDLY DIVISION " ) 

// asks the user to input a value of numerator

System.out.print("\nEnter the  numerator value : ");

  snum = input.next();
  y= snum.charAt(0);
  if( (y == 'q') || ( y == 'Q') )
  System.exit(-1);
  number1 = Integer.parseInt(snum);

// asks the user to input a value of divisor

System.out.print("Enter the  divisor value : ");

  sdiv = input.next();
  number2 = Integer.parseInt(sdiv);
  result = quotient(number1,number2);
  System.out.print(number1 + " / " + number2+" is "+result);
  }

//catches 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