Vous êtes sur la page 1sur 8

QUADRATIC EQUATIONS

package quadraticequations; import java.util.Formatter; import javax.swing.JOptionPane; /** * * @author Sergie_Chingly */ public class QuadraticEquations { double a, b, c, d, e, f, g, h, i; boolean x; int r; public void solver1() { do { try { String aa = JOptionPane.showInputDialog("Enter value of A"); a = Double.parseDouble(aa); if (a == 0) { JOptionPane.showMessageDialog(null, "A is an invalid input. Try again another value.", "Error!", JOptionPane.ERROR_MESSAGE); QuadraticEquations s = new QuadraticEquations(); s.solver1(); } else { String bb = JOptionPane.showInputDialog("Enter the value of B"); b = Double.parseDouble(bb); String cc = JOptionPane.showInputDialog("Enter the value of C"); c = Double.parseDouble(cc); if(b == 0) {

if(c == 0) { JOptionPane.showMessageDialog(null , "The root is x = 0.", "Answer", JOptionPane.PLAIN_MESSAGE); } else if (c < 0) { d = -c/a; e = Math.sqrt(d); Formatter x = new Formatter(); x.format("%.6f", e); JOptionPane.showMessageDialog(null , "The root is x = " + x.toString() + "." , "Answer", JOptionPane.PLAIN_MESSAGE); } else if (c > 0) { d = c/a; e = Math.sqrt(d); Formatter z = new Formatter(); z.format("%.6f", e); JOptionPane.showMessageDialog(null , "The root is x = j" + z.toString() + ".", "Answer", JOptionPane.PLAIN_MESSAGE); } } if(c == 0) { if(b == 0) { JOptionPane.showMessageDialog(null , "The root is x = 0.", "Answer", JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null , "The root is x = " + -b/a + "." , "Answer", JOptionPane.PLAIN_MESSAGE); } } }

x = false; } catch(NumberFormatException e) { JOptionPane.showMessageDialog(null, "Invalid value. Try again another value.", "Error!", JOptionPane.ERROR_MESSAGE); QuadraticEquations s = new QuadraticEquations(); s.solver1(); } } while( x ); if (a == 0 || b == 0 || c == 0){} else { d = b*b - 4*a*c; if (d == 0) { e = -b/(2*a); Formatter i = new Formatter(); i.format("%.6f", e); JOptionPane.showMessageDialog(null , "The root is x = " + i.toString() + "." , "Answer", JOptionPane.PLAIN_MESSAGE); } else if (d > 0) { f = Math.sqrt(d); g = (-b + f)/(2*a); h = (-b - f)/(2*a); Formatter l = new Formatter(); l.format("%.6f", g); Formatter y = new Formatter(); y.format("%.6f", h); JOptionPane.showMessageDialog(null , "The roots are x1 = " + l.toString() + " and x2 = " + y.toString() +"." ,"Answer", JOptionPane.PLAIN_MESSAGE); } else if (d < 0) {

e = -d; f = Math.sqrt(e); g = -b/(2*a); h = f/(2*a); Formatter j = new Formatter(); j.format("%.6f", g); Formatter o = new Formatter(); o.format("%.6f", h); JOptionPane.showMessageDialog(null , "The roots are x1 = " + j.toString() + " + j" + o.toString() + " and x2 = " + j.toString() + " - j" + o.toString() + ".", "Answer", JOptionPane.PLAIN_MESSAGE); } } } /** * @param args the command line arguments */ public static void main(String[] args) { JOptionPane.showMessageDialog(null, "General form of a Quadratic Equation: Ax^2 + Bx + C = 0", "Hey!", JOptionPane.PLAIN_MESSAGE); QuadraticEquations s = new QuadraticEquations(); s.solver1();

ARRAY
package arrayjava; import java.util.Arrays; import java.util.Scanner;

/** * * @author Sergie_Chingly */ public class ArrayJava { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x; System.out.print("Number of Array/s:"); x = scanner.nextInt(); int[] array = new int[x]; for(int y = 0; y != x; y++) { System.out.print("array[" + y + "]: " ); array[y] = scanner.nextInt(); } { int total = 0; for(int z = 0; z != x; z++) { total += array[z]; } double average = total/array.length; System.out.printf("\nSummation of Arrays: %d", total); System.out.printf("\nAverage of Arrays: %f", average); } int lowest = array[0]; for (int element : array) { if (element < lowest) lowest = element; }

int highest = array[0]; for (int value : array) { if (value > highest) highest = value; } Arrays.sort(array); System.out.printf("\nLowest Value: %d\n", lowest);

System.out.printf("Highest Value: %d\n", highest); { System.out.print("\nArrays in Descending Order:" ); for(int i = array.length - 1; i >= 0; i--) { System.out.print( " " + array[i]); } System.out.print("\nArrays in Ascending Order:" ); for(int i = 0; i < array.length; i++) { System.out.print( " " + array[i]); } } } }

Mindanao State University College of Engineering General Santos City

ES 84 JAVA PROGRAM

Quadratic Equation and Array


Submitted in Partial Fulfillment of the Requirements in ES 84: Numerical Methods - EE
School Year: 2012-2013, 2nd Semester MTH 7:30 9:00 a.m.

Submitted by:

Andres, Chingly Sergie P.

20 February 2013

OUTPUT
Quadratic Equation

Array

Vous aimerez peut-être aussi