Program Name: User-Friendly Division
Purpose: To apply knowledge that i have learned in OOP.
Date: March 8, 2009
Subect: 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 y = ' ';
//determine if the user wants to continue or quit
while( ( y != 'q') || ( y!= 'Q' ))
{
try
{
snum = input.next();
y= snum.charAt(0);
if( (y == 'q') || ( y == 'Q') )
System.exit(-1);
number1 = Integer.parseInt(snum);
System.out.print("Enter the value of the divisor: ");
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("You can't divide "+number1+" by "+number2);
}
}
}
}
No comments:
Post a Comment