Personally I don't feel that java would be a good choice for this type of application, unless you're doing a web page. Pascal is ideal for this type of coding, as you can code and test as you go. Instant feedback. Whenever I need a small program to do something, I write it in pascal, makes very fast work of small problems.
You should be aware of several things: get a copy of the swag code: Main SWAG (Sourceware Archive Group) HTML Index contains lots of useful pascal code snippets.
You also should consider having a copy of the Free Pascal compiler, its free and available for most CPUs.
Now, back to the problem. It's interesting, I haven't done the math for years so I checked wikipedia (don't right now, its wrong!) and wrote this: (let me know what you think, I working on a version that will allow you to change the amount of decimals in the answer on the fly!)
program Quadratics;
uses
crt;
const
reada = 'Please enter the value of a ';
readb = 'Please enter the value of b ';
readc = 'Please enter the value of c ';
var
e: integer;
a2,
x,
x1,
x2,
ix,
a,
b,
c,
d,
z: real;
ch: char;
done: boolean;
function getReal (s: string): real;
var
r: real;
rs: string;
begin
repeat
write (s);
readln (rs);
val (rs, r, e);
if (e <> 0) then
writeln ('Input must be a real number!');
until (e = 0);
getReal := r;
end;
function noZero (r: real; n: integer): string;
var
s: string;
begin
str (r:0:n, s);
while (s[length (s)] = '0') do
delete (s, length (s), 1);
if (s[length (s)] = '.') then
delete (s, length (s), 1);
noZero := s;
end;
begin
textColor (yellow);
done := false;
clrscr;
Writeln ('Hi, I am the Quadratic Calculator!');
writeln ('Press enter to get started');
ch := readKey;
clrscr;
while not done do begin
writeln;
textColor (lightGreen);
writeln ('Input the values for the Quadratic equation!');
textColor (yellow);
writeln;
Writeln ('Please use this as a guide! ax^2 + bx + c');
writeln;
a := getReal (reada);
b := getReal (readb);
c := getReal (readc);
d := (b * b) - (4 * a * c); { find the discriminant }
a2 := 2 * a;
writeln;
if (d = 0) then begin { discriminant is zero, one real root }
x1 := -b / a2;
writeln ('x = ', noZero (x1, 3));
end
else
if (d > 0) then begin { discriminant is positive, there are 2 roots }
x1 := (-b + sqrt (d)) / a2;
x2 := (-b - sqrt (d)) / a2;
writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3));
end
else begin { discriminant is negative, roots are complex }
x1 := -b / a2;
ix := sqrt (abs ((b * b) - 4 * a * c)) / a2;
writeln ('x = ', noZero (x1, 3), ' + i', noZero (ix, 3),
', ', noZero (x1, 3), ' - i', noZero (ix, 3));
end;
writeln;
{
x := (-b) + Sqrt ((b * b) - (4 * a * c));
x := x / 2 * a;
x2 := (-b) - Sqrt (((b * b) - (4 * a * c)));
x2 := x2 / 2 * a;
clrscr;
writeln ('WOW THAT WAS EASY!');
writeln;
write ('x= ');
write (x:2:2);
write (' or ');
writeln (x2:2:2);
writeln;
writeln ('Type any number and press enter to exit!');
readln (z); }
writeln ('Esc to quit, any other key to go again');
ch := readkey;
if (ch = #27) then
done := true;
end;
end.
You should be aware of several things: get a copy of the swag code: Main SWAG (Sourceware Archive Group) HTML Index contains lots of useful pascal code snippets.
You also should consider having a copy of the Free Pascal compiler, its free and available for most CPUs.
Now, back to the problem. It's interesting, I haven't done the math for years so I checked wikipedia (don't right now, its wrong!) and wrote this: (let me know what you think, I working on a version that will allow you to change the amount of decimals in the answer on the fly!)
program Quadratics;
uses
crt;
const
reada = 'Please enter the value of a ';
readb = 'Please enter the value of b ';
readc = 'Please enter the value of c ';
var
e: integer;
a2,
x,
x1,
x2,
ix,
a,
b,
c,
d,
z: real;
ch: char;
done: boolean;
function getReal (s: string): real;
var
r: real;
rs: string;
begin
repeat
write (s);
readln (rs);
val (rs, r, e);
if (e <> 0) then
writeln ('Input must be a real number!');
until (e = 0);
getReal := r;
end;
function noZero (r: real; n: integer): string;
var
s: string;
begin
str (r:0:n, s);
while (s[length (s)] = '0') do
delete (s, length (s), 1);
if (s[length (s)] = '.') then
delete (s, length (s), 1);
noZero := s;
end;
begin
textColor (yellow);
done := false;
clrscr;
Writeln ('Hi, I am the Quadratic Calculator!');
writeln ('Press enter to get started');
ch := readKey;
clrscr;
while not done do begin
writeln;
textColor (lightGreen);
writeln ('Input the values for the Quadratic equation!');
textColor (yellow);
writeln;
Writeln ('Please use this as a guide! ax^2 + bx + c');
writeln;
a := getReal (reada);
b := getReal (readb);
c := getReal (readc);
d := (b * b) - (4 * a * c); { find the discriminant }
a2 := 2 * a;
writeln;
if (d = 0) then begin { discriminant is zero, one real root }
x1 := -b / a2;
writeln ('x = ', noZero (x1, 3));
end
else
if (d > 0) then begin { discriminant is positive, there are 2 roots }
x1 := (-b + sqrt (d)) / a2;
x2 := (-b - sqrt (d)) / a2;
writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3));
end
else begin { discriminant is negative, roots are complex }
x1 := -b / a2;
ix := sqrt (abs ((b * b) - 4 * a * c)) / a2;
writeln ('x = ', noZero (x1, 3), ' + i', noZero (ix, 3),
', ', noZero (x1, 3), ' - i', noZero (ix, 3));
end;
writeln;
{
x := (-b) + Sqrt ((b * b) - (4 * a * c));
x := x / 2 * a;
x2 := (-b) - Sqrt (((b * b) - (4 * a * c)));
x2 := x2 / 2 * a;
clrscr;
writeln ('WOW THAT WAS EASY!');
writeln;
write ('x= ');
write (x:2:2);
write (' or ');
writeln (x2:2:2);
writeln;
writeln ('Type any number and press enter to exit!');
readln (z); }
writeln ('Esc to quit, any other key to go again');
ch := readkey;
if (ch = #27) then
done := true;
end;
end.