Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ruminay.math;
- import java.util.Scanner;
- import java.lang.Math;
- public class Operations {
- public void quadratic(){
- Scanner scanObj = new Scanner(System.in);
- double a = 0; //ax^2
- double b = 0; //bx
- double c = 0; //c
- double dis = 0; //The discriminant (b^2-4ac)
- double x1 = 0; //first, sometimes only, root
- double x2 = 0; //second root. Only used when there are two roots.
- System.out.println("------------------------------------------------"); //Signify new section.
- System.out.println("aX^2+bX+c=0");
- System.out.println("Please input a, b, and c. Doubles are allowed.");
- System.out.print("a?:"); a=scanObj.nextDouble();
- System.out.print("b?:"); b=scanObj.nextDouble();
- System.out.print("c?:"); c=scanObj.nextDouble();
- System.out.println();
- dis=((b*b)-(4*a*c));
- if(dis==0) //first case
- {
- x1=(-b)/(2*a);
- System.out.println("\tDiscriminant = 0. One real root.");
- System.out.println("\tX = "+x1);
- }
- if(dis>0) //second case
- {
- x1=((-b)+(Math.sqrt((b*b)-(4*a*c))))/(2*a);
- x2=((-b)-(Math.sqrt((b*b)-(4*a*c))))/(2*a);
- System.out.println("\tDiscriminant > 0. Two real roots.");
- System.out.println("\tX1 = "+x1);
- System.out.println("\tX2 = "+x2);
- }
- if(dis<0) //third case
- {
- System.out.println("\tDiscriminant < 0. No real roots.");
- }
- }
- public void linear(){
- Scanner scanObj = new Scanner(System.in);
- double x = 0;
- double y = 0;
- double a,b,c,d,e,f = 0;
- System.out.println("------------------------------------------------"); //Signify new section.
- System.out.println("aX+bY=c"); //Tell the user the equations being used.
- System.out.println("dX+eY=f");
- System.out.println("Please input a, b, c, d, e, and f. Doubles are allowed.");
- System.out.print("a?:"); a=scanObj.nextDouble(); //Get all the variables. No \n's are necessary because the scanner makes one when taking input.
- System.out.print("b?:"); b=scanObj.nextDouble();
- System.out.print("c?:"); c=scanObj.nextDouble();
- System.out.print("d?:"); d=scanObj.nextDouble();
- System.out.print("e?:"); e=scanObj.nextDouble();
- System.out.print("f?:"); f=scanObj.nextDouble();
- x=((e*c-b*f)/(e*a-b*d));
- y=((c-a*x)/b);
- System.out.println("\tX = "+x);
- System.out.println("\tY = "+y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement