# The If-statement

• Goal of Lab

In this lab we will learn to use the if statement.

• Relevant Material: Chapter 6 in book

• Preparation

```   mkdir ~/cs170/lab7                     # make lab directory
cd /home/cs170001/share/labs/lab7      # Go to share folder with lab5
cp * ~/cs170/lab7                      # Make a copy to your folder
cd ~/cs170/lab7                        # Go back to your lab folder
```

• If you use BlueJ

• If you are using BlueJ for the lab, you must open lab7 as "Non BlueJ"

I believe that you have work enough with BlueJ to turn Non-BlueJ Java projects into a BlueJ one.

• Lab Assignment

Edit the program file ABC.java in your ~/cs170/lab7 directory.

Your task is to write the following program in the ABC.java file. The program first reads in 3 decimal numbers a, b and c, and then computes and prints the solution of the quadratic equation

```   ax2 + bx + c = 0
```

• More details...

According to High School Calculus, there are 2 real valued solutions if the discriminant b2 - 4ac is greater than zero:

```                --------------------
-b + \/ b * b - 4 * a * c
x1 =  ----------------------------
2 * a
```
and
```                --------------------
-b - \/ b * b - 4 * a * c
x2 =  ----------------------------
2 * a
```

There are 1 real valued solution if the discriminant b2 - 4ac is equal to zero:

```           -b
x1 =  --------
2 * a
```

There are 2 complex valued solutions if the discriminant b2 - 4ac is less than zero:

```                          --------------------
-b           \/ 4 * a * c - b * b
x1 =  --------- +   ------------------------  i
2 * a                2 * a
```
and
```                          --------------------
-b           \/ 4 * a * c - b * b
x2 =  --------- -   ------------------------  i
2 * a                2 * a
```
NOTE:

The way to print a complex root is:

• Print first number (the real part)
• Print the String "+" or the String "-"
• Print second number (the imaginary part)
• Print the String "i"

Write a program that reads in 3 decimal numbers a, b and c, and then computes and prints the solution of the quadratic equation.

In particular, if the discriminant is 0, the program must print ONE SINGLE value only.

E.g., if a = 1, b = 4 c = 4, the program must only print:
```	-2.0
```
(You program MUST NOT print -2.0 twice)

Furthermore, if the discriminant is less than 0, the program must print the complex number with the letter "i" appended to the imaginary part.

E.g., if a = 1, b = 1 c = 1, the program must print:
```	-0.5 + 0.8660254037844386 i
-0.5 - 0.8660254037844386 i
```

• Relevant material

• HINT:

If you can, avoid typing - cut and paste statements that you can use from old programs into your new program.

• Test

• Make sure you use values of a, b and c such that b2 - 4ac takes on every possible sign !

• Positive discriminant:

• a = 1, b = 4, c = 1
```	-0.2679491924311228
-3.732050807568877
```

• Zero discriminant:

• a = 1, b = 4, c = 4
```        -2.0
```

• Negative discriminant:

• a = 1, b = 1, c = 1
```	-0.5 + 0.8660254037844386 i
-0.5 - 0.8660254037844386 i
```

• NOTE !!!

• Java does not understand imaginary or complex numbers (double is a deciaml (real) number)

• So: do NOT use "i" as a number in the program.

• If you need to print the complex solution, you need to compute
``` the real part:      realpart = -b / (2*a)
the imaginary part: imgpart = Math.sqrt(4 * a * c - b*b)/ (2 * a)
```
separately.

To print the complex number, you use:

```    System.out.println( realpart + " + " + imgpart + " i");
```

• Turnin

• Turn in the lab before 6:00 PM with the following commands:
```   cd ~/cs170/lab7

/home/cs170001/turnin ABC.java  lab7

```