# Object Oriented and C#: Quadratic Roots Program

The following program is designed to accept three doubles as inputs and prints the roots of a quadratic, whether complex or real. If a non-double is inputted into the program, the program should display “Bad Input”. The program contains two files: a “program” file to run several of the main methods and a “complex” file which creates the class for handling complex numbers and overrides the built in “ToString” method.

The first goal is to initialize the part of the program that handles real roots. The easiest portion is to create a method that reads doubles. It is important that the method contains a nullable type because the method should return null if a non-double such as a string is put into the method. This provides an easy way to use a conditional statement upon using the “Tryparse” method. The “Tryparse” method returns a boolean value of true or false. The “if” statement checks if the return is true and if so, returns the result. If not, null is returned. Next, the “getquadraticstring” method is implemented to format the printed result in the form “AX^2+BX+C”. This is also done within the “program” file. Format specifiers are put within the placeholders to set the printed values to two decimal places if neccessary. The “getrealroots” method produces the roots of the quadratic given that they are purely real. First the discriminant (the part in the quadratic formula under the square root symbol) is calculated. Several if statements are provided to check how many real roots there are and returns that quantity as an integer. For example, if the discriminant is negative, there will be no real roots returned. This means both of the “out” variables should be set to null and the function should return a 0. For a discriminant = 0, the quadratic formula reduces to -B/2A and the second root should be null. The return value is again the number of roots (1). It is important to note that an “if-else” statement must only end in “else” rather than “else if”. The “else” statement must cover all other possibilities. Within the “main” function, three numbers are taken from the console using the getDouble method. An integer value is obtained from the getRealroots method which states the number of roots. This will be used for the conditional statements. For ease of reading code, a string variable is created to store the return from the “getQuadraticString” method.

Next, an “if” statement is used to print a bad input if any of the a, b, c variables are null. A return statement is included within the “if” statement so that an else does not have to be provided. This will exit the statement after it has completed. Now the logic for the imaginary numbers must be implemented. The default constructor is shown with default inputs of zero. It doesn’t need any code within it because it inherits the Complex constructor. The “ToString()” method must be overridden because the formatting must be changed to adhere to complex numbers. In addition, logic must be implemented for the “getImaginaryRoots()” method. The discriminant is calculated the same way as before, however the absolute value is taken. The real part must be calculated separately and the denominator is split for this reason. For clarification, this is the real part of a complex root. The two roots are the same, but complex conjugates. The “main” function must be updated to reflect the imaginary roots. The “getQuadraticString()” method is updated as shown. Three pieces of string must be created with several conditions imposed. They begin as empty strings and are filled in. Separating them into parts lets the logic be implemented for when each coefficient is 1 or -1. When C is zero, an empty string will be printed. 