/*Programmer:Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/
/*Programmer:Miexie D. Tulid
Excersice no. 1
Date started: March 06, 2009
Date ended: March 07, 2009
Description: A program that will ask numerator and denominator, generate the quotient. Then
if the user enters 'q' or 'Q' the program will quit.*/
import java.util.InputMismatchException;
import java.util.Scanner;
public class DivisionPractice{
public static float quotient(float numerator, float denominator)throws ArithmeticException
{
return numerator/denominator;}
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
String exitchar="mix";
char x=exitchar.charAt(0);
float numerator=0;
float denominator=0;
String ask="mix";
while(x!='q'){
if(x != 'Q'){
try{
System.out.println("Enter the numerator:");
if((scan.next().charAt(0)=='q')||(scan.next().charAt(0)=='Q')){
x=scan.next().charAt(0);
}else{
numerator=scan.nextFloat();
System.out.println("Enter the divisor:");
denominator=scan.nextFloat();
float result=quotient(numerator,denominator);
System.out.println(numerator+"/"+denominator+" is "+result);
}
}
catch(ArithmeticException arerr)
{
System.err.println("You can't divide "+numerator+" by "+denominator);
}
catch(InputMismatchException inputerr)
{
System.err.println("You enter bad data.\nPlease try again.");
}
catch(IndexOutOfBoundsException index)
{
System.err.println("");
}
}else{
x='q';
}
}
}
}
Friday, March 6, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment