Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!

# Operations Class

By: a guest on Apr 8th, 2013  |  syntax: Java  |  size: 2.30 KB  |  views: 14  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. package com.ruminay.math;
2. import java.util.Scanner;
3. import java.lang.Math;
4.
5. public class Operations {
6.
7.
9.
10.                 Scanner scanObj = new Scanner(System.in);
11.                 double a = 0; //ax^2
12.                 double b = 0; //bx
13.                 double c = 0; //c
14.                 double dis = 0; //The discriminant (b^2-4ac)
15.                 double x1 = 0; //first, sometimes only, root
16.                 double x2 = 0; //second root. Only used when there are two roots.
17.
18.                 System.out.println("------------------------------------------------"); //Signify new section.
19.                 System.out.println("aX^2+bX+c=0");
20.                 System.out.println("Please input a, b, and c. Doubles are allowed.");
21.                 System.out.print("a?:"); a=scanObj.nextDouble();
22.                 System.out.print("b?:"); b=scanObj.nextDouble();
23.                 System.out.print("c?:"); c=scanObj.nextDouble();
24.                 System.out.println();
25.
26.                 dis=((b*b)-(4*a*c));
27.                 if(dis==0) //first case
28.                 {
29.                         x1=(-b)/(2*a);
30.                         System.out.println("\tDiscriminant = 0. One real root.");
31.                         System.out.println("\tX = "+x1);
32.                 }
33.                 if(dis>0) //second case
34.                 {
35.                         x1=((-b)+(Math.sqrt((b*b)-(4*a*c))))/(2*a);
36.                         x2=((-b)-(Math.sqrt((b*b)-(4*a*c))))/(2*a);
37.                         System.out.println("\tDiscriminant > 0. Two real roots.");
38.                         System.out.println("\tX1 = "+x1);
39.                         System.out.println("\tX2 = "+x2);
40.                 }
41.                 if(dis<0) //third case
42.                 {
43.                         System.out.println("\tDiscriminant < 0. No real roots.");
44.                 }
45.
46.
47.         }
48.         public void linear(){
49.
50.                 Scanner scanObj = new Scanner(System.in);
51.                 double x = 0;
52.                 double y = 0;
53.
54.                 double a,b,c,d,e,f = 0;
55.
56.                 System.out.println("------------------------------------------------"); //Signify new section.
57.                 System.out.println("aX+bY=c"); //Tell the user the equations being used.
58.                 System.out.println("dX+eY=f");
59.                 System.out.println("Please input a, b, c, d, e, and f. Doubles are allowed.");
60.                 System.out.print("a?:"); a=scanObj.nextDouble(); //Get all the variables. No \n's are necessary because the scanner makes one when taking input.
61.                 System.out.print("b?:"); b=scanObj.nextDouble();
62.                 System.out.print("c?:"); c=scanObj.nextDouble();
63.                 System.out.print("d?:"); d=scanObj.nextDouble();
64.                 System.out.print("e?:"); e=scanObj.nextDouble();
65.                 System.out.print("f?:"); f=scanObj.nextDouble();
66.
67.                 x=((e*c-b*f)/(e*a-b*d));
68.                 y=((c-a*x)/b);
69.
70.                 System.out.println("\tX = "+x);
71.                 System.out.println("\tY = "+y);
72.
73.         }
74. }
clone this paste RAW Paste Data
Top