Advertisement
ivan_yosifov

Basic Exception Handling

Jun 9th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. package demo;
  2.  // Basic exception handling
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class HelloJava {
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.         int choice = 0;
  10.        
  11.         do{
  12.             printMenu();
  13.             choice = input.nextInt();
  14.            
  15.             if(choice == 1){
  16.                 handleReverse();               
  17.             }else if(choice == 2){
  18.                 handleAverage();
  19.             }else if(choice == 3){
  20.                 handleLinearEquation();
  21.             }else if(choice != 4){
  22.                 System.out.println("Invalid choice");
  23.             }
  24.         }while(choice != 4);
  25.        
  26.     }
  27.     public static void handleLinearEquation(){
  28.         Scanner input = new Scanner(System.in);
  29.         System.out.print("Enter a, b and c");
  30.        
  31.         try{
  32.             int a = input.nextInt();
  33.             int b = input.nextInt();
  34.             int c = input.nextInt();
  35.            
  36.             double[] roots = getRoots(a, b, c);
  37.             if(roots[0] == roots[1] && roots[0] != 0){
  38.                 System.out.println(roots[0]);
  39.             }else if(roots[0] == 0 && roots[1] == 0){
  40.                 System.out.println("No solution");
  41.             }else{
  42.                 System.out.println(roots[0] + ", " + roots[1]);
  43.             }
  44.         }catch(Exception e){
  45.             System.out.println("a can not be zero");
  46.         }
  47.     }
  48.    
  49.     public static double[] getRoots(int a, int b, int c) throws Exception{
  50.         if(a == 0){
  51.             throw new Exception();
  52.         }
  53.         double[] roots = {0, 0};
  54.        
  55.         double D = b * b - 4 * a * c;
  56.         if(D > 0){
  57.             roots[0] = (-b + Math.sqrt(D)) / (2 * a);
  58.             roots[1] = (-b - Math.sqrt(D)) / (2 * a);
  59.         }else if(D == 0){
  60.             roots[0] = -b / (2 * a);
  61.             roots[1] = roots[0];
  62.         }
  63.            
  64.         return roots;
  65.     }
  66.     public static void handleAverage(){
  67.         Scanner input = new Scanner(System.in);
  68.         System.out.print("Enter numbers separated by space: ");
  69.        
  70.         try{
  71.             String line = input.nextLine();
  72.             String[] numbers = line.split(" ");
  73.            
  74.             double average = getAverage(numbers);
  75.             System.out.println("Average: " + average);
  76.         }catch(Exception e){
  77.             System.out.println("You must enter at least one number");
  78.         }
  79.     }
  80.    
  81.     public static double getAverage(String[] numbers) throws Exception {
  82.         if(numbers.length <= 0){
  83.             throw new Exception();
  84.         }
  85.        
  86.         double sum = 0;
  87.         double average = 0;
  88.        
  89.         for(int i = 0; i < numbers.length; i++){
  90.             sum += Double.parseDouble(numbers[i]);
  91.         }
  92.        
  93.         average = sum / numbers.length;
  94.        
  95.         return average;
  96.     }
  97.     public static void handleReverse(){
  98.         Scanner input = new Scanner(System.in);
  99.         System.out.print("Enter number: ");
  100.        
  101.         try{
  102.             int number = input.nextInt();
  103.             int reversed = reverse(number);
  104.            
  105.             System.out.println("Reversed: " + reversed);
  106.         }catch (Exception e){
  107.             System.out.println("Number must be positive");
  108.         }
  109.     }
  110.    
  111.     public static void printMenu(){
  112.         System.out.println();
  113.         System.out.println("1. Reverse number");
  114.         System.out.println("2. Get average from sequence");
  115.         System.out.println("3. Solve linear equation");
  116.         System.out.println("4. Exit");
  117.     }
  118.     public static int reverse(int n) throws Exception{
  119.         if(n < 0){
  120.             throw new Exception();
  121.         }
  122.        
  123.         int rev = 0;
  124.         int length = String.valueOf(n).length();
  125.        
  126.         if(length == 1){
  127.             rev = n;
  128.         }else{
  129.             while(n != 0){
  130.                 length--;
  131.                 int lastDigit = n % 10;
  132.                
  133.                 rev += lastDigit * Math.pow(10, length);
  134.                 n /= 10;
  135.             }          
  136.         }          
  137.        
  138.         return rev;
  139.     }
  140.            
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement