Ec - 21 - 18 Coe Assignment 3
Ec - 21 - 18 Coe Assignment 3
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.*;
/**
*/
int count=1;
do{
//opens a do loop
a=DataIn.readLine();
a1=Double.parseDouble(a);
b=DataIn.readLine();
b1=Double.parseDouble(b);
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));
else if(d>0){
x1=(-b1+Math.sqrt(d))/(2*a1);
x2=(-b1-Math.sqrt(d))/(2*a1);
else{
d=(Math.sqrt(Math.abs((b1*b1)-(4*a1*c1))));
realx1=(-b1/(2*a1));
cmplxrt=d/(2*a1);
while(count<=100);
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;
/**
*/
int i;
for(i=1;i<=50;i++){
b=Double.parseDouble(B);
c=Double.parseDouble(C);
if(d<0){
double imaginary=(Math.sqrt(-d))/(2*a);
double real=(-b/(2*a));
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);
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.*;
/**
*/
int counter=1;
while(counter<=50) {
a=input.nextDouble();
b=input.nextDouble();
c=input.nextDouble();
d=((b*b)-(4*a*c));
if(d==0) {
x=(-b/(2*a));
}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);
}counter++;