THINK FIRST·CODE LATER

← All labs

QuadraticEquation: an object that answers questions

Problem

Write a class QuadraticEquation for ax² + bx + c = 0 and a public class QuadraticApp.

The class has three private fields a, b, c set by the constructor (accessors only: an equation does not change), and the methods getDiscriminant() (b² − 4ac), getRoot1(), getRoot2() (each returns 0 when the discriminant is negative) and numberOfRoots() (2, 1 or 0).

main reads triples a b c until the end of the input and prints one line per equation. For the input

1 2 -3
1 2 1
1 2 3

the program prints

a = 1.0, b = 2.0, c = -3.0: discriminant 16.00, two roots 1.000 and -3.000
a = 1.0, b = 2.0, c = 1.0: discriminant 0.00, one root -1.000
a = 1.0, b = 2.0, c = 3.0: discriminant -8.00, no real roots

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l10-8-data-1.txt

1 2 -3
1 2 1
1 2 3
What your program must print for l10-8-data-1.txt
a = 1.0, b = 2.0, c = -3.0: discriminant 16.00, two roots 1.000 and -3.000
a = 1.0, b = 2.0, c = 1.0: discriminant 0.00, one root -1.000
a = 1.0, b = 2.0, c = 3.0: discriminant -8.00, no real roots

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l10-8-data-1.txt

Windows PowerShell does not understand <:

Get-Content l10-8-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.