ITI0011-2015:harjutus 01

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti

Harjutuse tähtaeg on 3. praktikum, mis toimub 10.-11. veebruaril.

Üldine info harjutuse esitamise kohta: ITI0011:harjutused.

Kirjeldus

Kirjutada funktsioon, mis leiab ruutvõrrandi lahendi. Ruutvõrrand on kujul a * x^2 + b * x + c = 0. Sellel võib olla mitu lahendit.

Kui lahendeid on kaks, siis tagastada nende kahe argumendi korrutis. Kui lahendeid on üks, tagastada see lahend. Kui lahendeid pole, tagastada Double.NaN (see on üks konstant).

Ülesande esitamine

Ülesanne tuleb laadida git repositooriumisse kausta "EX01".

Testimine

Ülesanne on testitav alates esmaspäevast (ehk siis soovitatav on laadida oma lahendus esmaspäeval üles, et saada tagasisidet).

Ülesande hindamine

Ülesande hindamine toimub järgmises praktikumis. Eeldus on see, et lahendus on git'i üles laetud korrektselt.

Mall

Etteantud mall, mida peaksite kasutama selle ülesande jaoks:

<source lang="java"> /**

* Home assignment 01.
*/

public class Task01 {

   /**
    * Function that solves an quadratic equation (a*x^2 + b*x + c = 0).
    * @param a A squared variable held constant (a*x^2).
    * @param b A variable held constant (b*x).
    * @param c An absolute term (c).
    * @return If equation has 2 roots (x1 and x2), returns the product of them (x1 * x2);
    *         If equation has 1 root (x), returns it (x);
    *         If equation has no roots, returns Double.NaN.
    */
   public static double solveQuadraticEquation(double a, double b, double c) {
       //Calculate the discriminant and initialize d variable.
       double d = -1.0;
       //TODO: add your code here.
       if (d > 0.0) {
           //Calculate the roots and initialize x1 and x2.
           double x1 = 0, x2 = 0;
           //TODO: add your code here.
           return x1 * x2;
       } else if (d == 0.0) {
           //Calculate the root and initialize x.
           double x = 0;
           //TODO: add your code here.
           return x;
       } else {
           //Initialize variable x with Double.NaN.
           double x = 0;
           //TODO: add your code here.
           return x;
       }
   }
   
   /**
    * Entry-point of the program.
    * This is here so you can test out your code
    * with running this program.
    * @param args Arguments from command-line.
    */
   public static void main(String[] args) {
   	System.out.println("a = 1, b = -5, c = 6:" 
   			+ solveQuadraticEquation(1.0, -5.0, 6.0));
   	// should be 6
   	// add some more tests here if needed.
   }

} </source>