Monday, March 9, 2009

User-Friendly Division By: Wendel B. Bocong

/*
Programmer: Wendel B. Bocong
Date Started: March 6,2009
Date Ended: March 9, 2009
Description:This program will calculate the quotient, then if you enter any character it will return an error message.
If the user will press q it will exit.
Purpose: To know more about different Exceptions.
*/
import java.util.*;
import java.io.*;

public class DivisionPractice{
public static double quotient(double numerator,double divisor)throws ArithmeticException{
return numerator/divisor;
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double numerator,divisor,answer;
String end = "tech";

char wen=end.charAt(0);

do{
try{
System.out.print("\n Numerator: "); //entering numerator from the user
String del=scan.next(); //this will get the value of numerator as String
char boc=del.charAt(0); //using the method charAt()

if(boc=='q'||boc=='Q'){
System.exit(0);
}else{
numerator=Integer.parseInt(del);
System.out.print("\nDivisor: ");//entering divisor from the user
String cong=scan.next();
divisor=Integer.parseInt(cong);
answer=quotient(numerator,divisor);//calculating the quotient
System.out.print(numerator+"/"+divisor+" is "+answer);//displaying the result
System.out.println();
}
}
catch(ArithmeticException arithmetic){//this exception will catch all error input by the user
System.err.print("\nNothing to Divide because it is 0");//message that will display if the user try to divide the numerator into 0
}
catch(NumberFormatException inputMismatch){//this exception will catch all String input whether it is from numerator or divisor and display error message
System.err.print("\nUndesirable type of input.\nPlease try again.");
}
}
while(wen!='q'||wen=='Q');
}
}

No comments:

Post a Comment