0% found this document useful (0 votes)
4 views9 pages

Ec - 21 - 18 Coe Assignment 3

The document contains three Java programs designed to solve quadratic equations using different methods: BufferedReader, GUI with JOptionPane, and Scanner. Each program prompts the user for coefficients a, b, and c, calculates the discriminant, and determines the roots based on its value. The implementation includes handling for real and complex roots, and the programs are structured to run multiple times based on user input.

Uploaded by

Harry Supa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Ec - 21 - 18 Coe Assignment 3

The document contains three Java programs designed to solve quadratic equations using different methods: BufferedReader, GUI with JOptionPane, and Scanner. Each program prompts the user for coefficients a, b, and c, calculates the discriminant, and determines the roots based on its value. The implementation includes handling for real and complex roots, and the programs are structured to run multiple times based on user input.

Uploaded by

Harry Supa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

MOI UNIVERSITY

SCHOOL OF ENGINEERING
ELECTRICAL AND COMMUNICATION DEPARTMENT
OBJECT ORIENTED PROGRAMMING
COE 361
DR. OWOR
ASSIGNMENT 3
HEZEKIAH MWANGI MURIMI
EC/21/18
25/02/2020
Question 1: Using buffered reader to draft a program that solves for quadratic functions
import java.io.*;

import java.math.*;

/**

*@author HEZEKIAH MWANGI

*/

public class Buffered_Quadratic {

public static void main(String [] args)throws IOException{

//main method begins the execution of java application

String a,b,c; //defining string variables

Double a1,b1,c1,d,x1,x2,realx1,realx2,cmplxrt; //variable definition as double

BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in)); //prompt

int count=1;

do{

//opens a do loop

System.out.println("Enter the value of a");

a=DataIn.readLine();

a1=Double.parseDouble(a);

System.out.println("Enter the value of b");

b=DataIn.readLine();

b1=Double.parseDouble(b);

System.out.println("Enter the value of c");

c=DataIn.readLine();

c1=Double.parseDouble(c);
d=(b1*b1)-(4*a1*c1);//computes for the value of d

if(d==0){

x1=x2=(-b1/(2*a1));

System.out.println(" The two roots are equal x1=x2 " +x1);

//displays the value of real repeated roots if condition one is true

else if(d>0){

x1=(-b1+Math.sqrt(d))/(2*a1);

x2=(-b1-Math.sqrt(d))/(2*a1);

System.out.println("The first root is "+x1 +" And second root is "+x2);

//displays real roots

else{

d=(Math.sqrt(Math.abs((b1*b1)-(4*a1*c1))));

realx1=(-b1/(2*a1));

cmplxrt=d/(2*a1);

System.out.println("The first complex root is "+realx1 +cmplxrt+"i" +" Whereas


the second complex root is "+realx1 +cmplxrt+"i" );

//displays for complex and real roots

while(count<=100);

}//ends main function

} //closes the class Buffered_Quadratic

The program runs and produces the output similar to the one printed on the screen below
Question 2: Using GUI to draft a program that solves for quadratic functions
import javax.swing.JOptionPane;

/**

* @author HEZEKIAH MWANGI

*/

public class GUI_Quadratic {

public static void main(String[] args){

//main method begins the execution of java application

String A,B,C; //defining string variables

double a,b,c,d; //defining double variables

int i;

for(i=1;i<=50;i++){

// opening a for loop

A=JOptionPane.showInputDialog(" Enter a:"); //prompt


a=Double.parseDouble(A);

B=JOptionPane.showInputDialog(" Enter b:"); //prompt

b=Double.parseDouble(B);

C=JOptionPane.showInputDialog(" Enter c:"); //prompt

c=Double.parseDouble(C);

d=(Math.pow(b,2))-(4*a*c); //working out for the value of d

if(d<0){

double imaginary=(Math.sqrt(-d))/(2*a);

double real=(-b/(2*a));

JOptionPane.showMessageDialog(null,"root 1"+ "=" +real +


"+"+imaginary+"i"); //displays the imaginary roots

JOptionPane.showMessageDialog(null,"root 2"+ "=" +real+ "-


"+imaginary+"i"); //displays the imaginary roots

if(d>0 {

double real1=(-b+(Math.sqrt(d)))/(2*a);

double real2=(-b-(Math.sqrt(d)))/(2*a);

JOptionPane.showMessageDialog(null,"root1"+real1);

JOptionPane.showMessageDialog(null,"root2"+real2);

if(d==0) {

double equal=(-b/(2*a));

JOptionPane.showMessageDialog(null,"equal roots"+equal);

} //closes main function

} //ends class GUI_Quadratic

The program runs and produces the output similar to the one printed on the screen below
Question 3: Using Scanner approach to solve a quadratic equation
import java.util.Scanner;

import java.math.*;

/**

* @author HEZEKIAH MWANGI

*/

public class ScannerQuadratic {

public static void main(String[] args){

//main method begins the execution of java application

double a,b,c,d,x,x1,x2,Re,Cp; //defining double variables

int counter=1;

while(counter<=50) {

Scanner input=new Scanner(System.in); //prompt

System.out.println(" Enter the value of a:");

a=input.nextDouble();

System.out.println("Enter the value of b:");

b=input.nextDouble();

System.out.println("Enter the value of c:");

c=input.nextDouble();

d=((b*b)-(4*a*c));

if(d==0) {

x=(-b/(2*a));

System.out.println("The two roots are equal x1=x2 " +x);

}else if(d>0) {

x1=(-b+Math.sqrt(d))/(2*a);

x2=(-b-Math.sqrt(d))/(2*a);

System.out.println("The first root is "+1 +" While the second root is "+x2);
}else{

d=Math.abs(d);

Re=(-b/(2*a));

Cp=Math.sqrt(d)/(2*a);

System.out.println("The first complex root is "+Re +Cp+"i" +" The second


complex root is "+Re + Cp+"i");

}counter++;

} //ends function main

} //closes class Scanner_Quadratic

Which has the output as one printed on the screen:

You might also like