Advertisement
Guest User

Calculator

a guest
Oct 26th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. import static java.lang.System.*;
  3.  
  4. public class main
  5. {
  6.    public static void main(String[] args)
  7.    {   
  8.  
  9.         Scanner keyboard = new Scanner(System.in);
  10.  
  11.         int choice = 0;
  12.         double fAnswer;
  13.         String quadAns;
  14.         boolean choice1;
  15.  
  16.         while(choice != 4)
  17.         {  
  18.             choice1 = false;
  19.             out.println("\n+-------------------------------------+\n");
  20.             out.println("\t1) Quadratic Calculator");
  21.             out.println("\t2) Area Calculator");
  22.             out.println("\t3) Volume Calculator");
  23.             out.println("\t4) Exit");
  24.  
  25.             choice = keyboard.nextInt();
  26.  
  27.             if (choice == 1)
  28.             {
  29.                 double [] a;
  30.  
  31.                 a = quadCal();
  32.                 out.println("The answer is: " + a[1] + " " + a[2]);
  33.                 choice1 = true;
  34.             }
  35.  
  36.             else if (choice == 2)
  37.             {
  38.                
  39.                 fAnswer = areaCal();
  40.  
  41.             }
  42.  
  43.             else if(choice == 3)
  44.             {
  45.                 fAnswer = volCal();
  46.             }
  47.  
  48.             else if(choice == 4)
  49.             {
  50.                 System.exit(0);
  51.             }
  52.  
  53.             else
  54.             {
  55.                 out.println("Error");
  56.                 choice1 = true;
  57.             }
  58.  
  59.             if (choice1 == false) {
  60.             out.println("The answer is: " + fAnswer);  
  61.             }
  62.  
  63.             else
  64.             {
  65.             }
  66.            
  67.         }
  68.  
  69.    }
  70.  
  71.    public static double[] quadCal()
  72.    {
  73.  
  74.         Scanner keyboard = new Scanner(System.in);
  75.  
  76.         double[] quad;
  77.         quad = new double[2];
  78.         double a, b, c, n = 0;
  79.             out.print("Enter the value of a: ");
  80.             a = keyboard.nextDouble();
  81.             out.println();
  82.             out.print("Enter the value of b: ");
  83.             b = keyboard.nextDouble();
  84.             out.println();
  85.             out.print("Enter the value of c: ");
  86.             c = keyboard.nextDouble();  
  87.             out.println();         
  88.  
  89.             quad[0] = ((b*(1-2)) + Math.sqrt((b*b) - (4*a*c)))/(2*a);
  90.             quad[1] = ((b*(1-2)) - Math.sqrt((b*b) - (4*a*c)))/(2*a);
  91.  
  92.         return quad;
  93.    }
  94.  
  95.    public static double areaCal()
  96.    {
  97.  
  98.         Scanner keyboard = new Scanner(System.in);
  99.  
  100.         double a, b, area;
  101.  
  102.         out.print("Enter the value of height: ");
  103.         a = keyboard.nextDouble();
  104.         out.println();
  105.         out.print("Enter the value of length: ");
  106.         b = keyboard.nextDouble();
  107.         out.println();
  108.  
  109.         area = a * b;
  110.  
  111.         return area;
  112.    }
  113.  
  114.    public static double volCal()
  115.    {
  116.  
  117.         Scanner keyboard = new Scanner(System.in);
  118.  
  119.         double a, b, c, volume;
  120.  
  121.         out.print("Enter the value of height: ");
  122.         a = keyboard.nextDouble();
  123.         out.println();
  124.         out.print("Enter the value of length: ");
  125.         b = keyboard.nextDouble();
  126.         out.println();
  127.         out.print("Enter the value of depth: ");
  128.         c = keyboard.nextDouble();
  129.         out.println();
  130.  
  131.         volume = a * b * c;
  132.  
  133.         return volume;
  134.    }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement