Prerequisites: JDK, maven, git and Intellij installation and : For all projects
ID: 3742396 • Letter: P
Question
Prerequisites:
JDK, maven, git and Intellij installation and :
For all projects in this course you will need a set of tools as follow:
Download and install JDK 10 on your system
Download Mavan and click here for installation guide
a. Here is a good resource on getting started with maven
Download and install gitbash
Apply for intellij Academic license , then download the ultimate version and install
Here is some tutorial on using Intellij
https://www.youtube.com/watch?v=GSKERVTMWqs
https://www.jetbrains.com/idea/documentation/
You may use Eclipse or Netbeans, but Intellij is highly recommended.
For additional help, visit
https://sp18.datastructur.es/materials/lab/lab1setup/lab1setup https://sp18.datastructur.es/materials/lab/lab1/lab1
Project Description:
In this project you are going to build a command line application which takes a quadratic equation in the form "ax2 + bx + c = 0" of as command line argument and solves the equation by using the following formula
In order to support power a proper notation you can take the ax2 + bx + c = 0 as
ax^2 + bx + c and then solve the equation. A useful data structure for parsing the command line is stack. However, if you find other ways to make it this work, would be OK. Keep in mind
that you cannot ask for a,b,c from user separately.
To simplify the problem for this project, you can assume that a,b,c are not zero. If you cover other cases, you will be rewarded with bonus points.
Make sure to follow the instruction and use proper naming as it is provided to you. Don’t change Class names, method names or variables.Remember, that all names in java are case sensitive.
Requirements:
In addition, your program should support “README” option which prints help/usage of the program and all supported options.
If the user writes:
ax^2+bx+cx A quadratic equation where ^ is for power operation.
Options are:
READM will print this help
s will print only the root answers
a will print all details, detected a,b,c and roots
As it is noted above, your program will need to support s and a options as well for short/simple output and full details output respectively.
Notes:
Before diving into coding, use a scratch paper and plan out your design. Come up with as many unique cases as possible which you need to consider. Start with simple cases and expand your work to cover more complex cases. For instance, there are different quadratic equations such
as 2x^2+1, x^260x+100, x^24, etc... These cases include single digit or multiple digit multipliers for x, or b,c being 0.
A good strategy might be to start with simple cases and then expanding the scope your application to cover other cases.
Project Repository:
For most projects you will be using github source control. Each project may come with a starter code that you need to download. Use the link below to register and create a github account and start your work.
https://classroom.github.com/a/UkPT_yc
Explanation / Answer
//There is a bit of correction required in above solution,please follow the solution given below.
package PCk;
import java.io.*;
import java.util.*;
public class QuadraticEquation {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine(); // Reading a quadratic equation
char[] k = s.toCharArray(); // Converting the input string into char array;
StringBuilder sb = new StringBuilder();
int a = 0, b = 0, c = 0;
for (int i = 0; i < k.length; i++) { // Converting all minus sign into -+ as for example 2x2-9x==2x2+-9x
if (k[i] == '-' && i != 0) {
sb.append("+");
sb.append("-");
} else
sb.append(k[i]);
}
String snew = new String(sb);
String[] s1 = snew.split("\+"); // spliting the read equation on the basis of + seperater
int countx = 0; // to findout value of a,b and c counting the number of x in the equation.
for (int i = 0; i < k.length; i++) {
if (k[i] == 'x')
countx++;
}
StringBuilder aa = new StringBuilder();// used to store the coefficient of x2
StringBuilder bb = new StringBuilder();// used to store the coefficient of x
StringBuilder cc = new StringBuilder();// used to store c in quadratic equation
if (countx == 2) { //equation will have a,b and may be c(x2+4x+7,x2+4x)
for (int i = 0; i < s1[0].length(); i++) {
if (s1[0].charAt(i) != 'x')
aa.append(s1[0].charAt(i));
else
break;
}
for (int i = 0; i < s1[1].length(); i++) {
if (s1[1].charAt(i) != 'x')
bb.append(s1[1].charAt(i));
else
break;
}
if (s1.length == 3)
cc.append(s1[2]);
} else if (countx == 1) {//equation will have a and c,but no b(x2-4)
for (int i = 0; i < s1[0].length(); i++) {
if (s1[0].charAt(i) != 'x')
aa.append(s1[0].charAt(i));
else
break;
}
if (s1.length == 2)
cc.append(s1[1]);
}
if (countx == 2) { //parsing the value of a,b and c received in string buffer and converting them to int,if two x are present in equation
String aq = new String(aa);
if (aq.length() == 0)
a = 1;
else if (aq.charAt(0) == '-'&&aq.length()==1)
a = -1;
else
a = Integer.parseInt(aq);
String bq = new String(bb);
if (bq.length() == 0)
b = 1;
else if (bq.charAt(0) == '-'&&bq.length()==1)
b = -1;
else
b = Integer.parseInt(bq);
if(s1.length==3) {
String cq = new String(cc);
c = Integer.parseInt(cq);}
}
if (countx == 1) { //parsing the value of a,b and c received in string buffer and converting them to int,if only one x is present in equation
String aq = new String(aa);
if (aq.length() == 0)
a = 1;
else if (aq.charAt(0) == '-'&&aq.length()==1)
a = -1;
else
a = Integer.parseInt(aq);
b = 0;
if (s1.length == 2) {
String cq = new String(cc);
c = Integer.parseInt(cq);
}
}
//System.out.println(a + " " + b + " " + c);
int value;
double root1 = 0; // variable root1,root2
double root2 = 0;
boolean flag = false;
value = b * b - 4 * a * c;
if (value > 0) {//two roots exist
root1 = (-b + Math.sqrt(value)) / (2 * a); //finding root1
root2 = (-b - Math.sqrt(value)) / (2 * a); //finding root2
} else if (value == 0) { //only one roots exist which is equal to second root
root1 = (-b + Math.sqrt(value)) / (2 * a);
root2 = root1;
} else {
flag = true; //imaginary roots
}
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String s4 = br2.readLine(); // ReadMEoption
if (s4.equalsIgnoreCase("README")) {
String s3 = br2.readLine();
if (s3.equalsIgnoreCase("a") && flag == false) { //output for real roots and input as a
System.out.print("root1=" + root1 + " root2=" + root2 + " a=" + a + " b=" + b + " c=" + c);
System.out.println();
}
else if (s3.equalsIgnoreCase("a") && flag == true) { //output for imaginary roots and input as a
System.out.print(" a=" + a + " b=" + b + " c=" + c + " and Roots are imaginary");
System.out.println();
}
else if (s3.equalsIgnoreCase("s") && flag == false) {// input as s
System.out.print("root1=" + root1 + " root2=" + root2);
System.out.println();
} else if (s3.equalsIgnoreCase("s") && flag == true) { // imaginary roots
System.out.print("Roots are imaginary");
System.out.println();
}
}
}
}
}