583 words - 3 pages

Part a

Video is attached with narration

Part b

The purpose of my program is to help solve quadratic equations. I faced some difficulties along

the way as I pursued this purpose. For example, often times my solution methods were

returning “NaN”. I initially attempted to solve this by writing out the formula and making sure the

parentheses were grouped correctly. I ended up collaborating with one of my classmates to

determine why sometimes the solution was correct and other times it returned “NaN. I

determined that it returned “NaN” when the solutions were not real. After figuring out the

problem with the help of a partner, I independently learned about exceptions and how to “throw”

and “catch”. Using this new knowledge of exceptions, I was able to handle non-real solutions.

Initially, my problem was not user interactive. I learned about the Scanner class and saw the

opportunity to add a user interaction part to it to make it more friendly. I also saw many

opportunities to use abstraction such as when finding the solutions, I did not need to reprogram

the discriminant aspect of the quadratic formula. Instead, I just called the discriminant method of

my class.

Part c

public double getFirstSolution() {

double firstxsolution;

double discriminant = getDiscriminant();

if(discriminant<0) {

throw new IllegalArgumentException("The discriminant is negative. There

are no real solutions");

}

double root = Math.sqrt(discriminant);

firstxsolution = (-b + root)/(2*a);

return firstxsolution;

}

This is my algorithm to get the first solution of the quadratic equation. It works by using the

discriminant method of my class and using the Math class for some operations. First, I call the

getDiscriminant() method and store the value of b^2 - 4ac in a variable. This part of the

algorithm simplifies the rest of the calculations. It then checks if the discriminant is negative to

determine if the solutions are valid or not. If the solutions are invalid, it handles this by throwing

an exception. Otherwise, it calculates the square root of the discriminant using the Math class’

sqrt method. Then, it manipulates this square root value by adding it to negative b and dividing

the quantity by 2 * a, completing the quadratic formula. This algorithm is the primary algorithm

used in the program as the program’s intended purpose is to provide the solutions for a

quadratic equation. The mathematical concepts that it integrates are those of the square root

which is found using the Math.sqrt, unreal values which are accounted for in the if statement,

and the quadratic formula, which is the entire algorithm.

Part d

public double getDiscriminant()

{

double Discriminant = Math.pow(b, 2) - (4 * a * c);

return Discriminant;

}

Here is an example of an abstraction that I used in this program. I found that I often repeated

the procedure for finding a discriminant and that a user may even need to find only the

discriminant so I created a getDiscriminant() method in order to avoid repeating my code. This

method is not only called by both of the getSolution methods but also by the user if they need to

find the discriminant. It simplifies my code because it is easier to read when the getDiscriminant

method is called instead of the formula being written out. It also removes repetition. The method

works by using the Math class’s pow method to square the value of b. Then from that value, it

subtracts the quantity of 4ac which is the formula for the discriminant of a quadratic equation.

Other Popular Examples

Need help finding an example assignment?