Feedback for reg. no. 012345678
CE000: Programming in Java — Roots of a Quadratic
Presentation: 25/25
The program has been presented beautifully. All lines in the program are ≤ 80 characters as required, and
there is good use of whitespace, both to separate regions of code and within lines. Commenting is generally
very clear, and the block of comments at the top of the program (lines 1–49) makes an excellent job of
describing the purpose of the program and the algorithms used. Especially good is the reference to a book
in which the numerical issues involved are described in more detail.
Algorithms: 25/25
You have implemented both of the required algorithms correctly. The code is easy to read and understand,
and has nothing in it that ties it to a particular Java VM or operating system.
Coding style: 25/25
Program structure is excellent. As you have remarked, there is little to be gained here from using several
classes; and you have explained why all your procedures are declared as static. You have split off the
one common factor in both algorithms, the calculation of the discriminant (lines 121–136), into a separate
routine, though I am doubtful that this would find re-use in another program. Finally, your program is frugal
in its use of resources. Well Done!
.
Results: 25/25
Your program is invoked as per the specification. The example invocations given in the block of comments
at the top of the program are good in that they illustrate cases in which the conventional and numericallystable formulations yield similar and different results. It is especially good to see the output produced by
your program in these two cases
Overall, you have made an exceptional job of writing and presenting this program.
Overall mark: 100%
Your program listing is on the following pages. Any line numbers mentioned above appear in the left-hand margin of your listing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // q u a d r o o t s . j a v a
-solution of a x ^2 + b x + c = 0
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // PURPOSE
// This program calculates the solution of the quadratic equation
//
//
a x ^2 + b x + c = 0
//
// using two methods . The first method is the one we all learn at school :
//
//
-b +/ - sqrt ( b ^2 - 4 ac )
//
x = ---------------------- .
//
2a
//
// However , this is numerically unstable when b ^2 >> 4 ac as then b ^2 - 4 ac is
// approximately equal to b ^2 , and one of the roots then comes out to be
// ( - b + b ) / (2 a ) , which is zero . This is implemented in the routine called
// " q u a d _ s o l v e _u n s t a b l e ".
//
// The second method is described in " Numerical Recipes in C " by W . H . Press
// et al ( second edition , Cambridge University Press , 1992 , p183ff ) and is
// implemented in the routine " qua d_s ol ve_ sta ble ".
//
// USAGE
//
java quadroots a b c
// where the three command - line arguments are the coefficients of the quadratic
// equation . Two good examples are :
//
//
quadroots 1 0 -4
//
which should yield roots of -2 and +2 and generates the output :
//
Roots of -4.0 + 0.0 x + 1.0 x ^2 = 0 are :
//
-2.0 and 2.0 using numerically unstable equation .
//
-2.0 and 2.0 using numerically stable equations .
//
// quadroots 1 1 e6 1
//
which should yield roots of roots of -1000000 and -1.0 E -6 and generates
//
the output :
//
Roots of 1.0 + 1000000.0 x + 1.0 x ^2 = 0 are :
//
-1000000.0 and 0.0 using numerically unstable equation .
//
-1000000.0 and -1.0 E -6 using numerically stable equations .
//
// RESTRICTIONS
// The program finds real roots only ; it generates an error message and exits
// if the coefficients provided would result in a complex root .
//
// AUTHOR
// Registration number 012345678
// I hereby certify that this program is entirely my own work .
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50
51
// Boilerplate .
52
53
import java . lang . Math ;
54
55
56
// The entire program resides in a single class . For a program of this length ,
// there is little point in producing a many - class solution .
57
58
59
60
61
//
//
//
//
As an aside , the constraints of the Java language mean that all the methods
in this class must be declared static : a static method ( main ) can call only
other static methods . Programs that make use of Java ’ s object - oriented
features by creating objects at run - time would not have this constraint .
62
63
class quadroots1 {
64
65
66
67
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // main -- handle command - line arguments , perform calculations , output result
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68
69
70
71
72
public static void main ( String args []) {
String me = " quadroots ";
String version = "1.0";
float a , b , c , roots [];
73
74
75
76
// We require precisely three arguments on the command line .
// If we don ’ t get them , produce an error message describing how
// to use the program .
77
2
if ( args . length != 3) {
System . err . println (" Usage : " + me + " a b c ");
System . err . println ("
to solve a x ^2 + b x + c ");
} else {
// The command line was sensible , so output a banner giving our
// name and version . Then convert the numbers from Strings to
// floats , invoke the relevant routines to calculate the roots ,
// and output the results . Note that an exception will occur in
// stof if it is fed an invalid number .
78
79
80
81
82
83
84
85
86
87
System . out . println ( me + " version " + version );
88
89
a = stof ( args [0]);
b = stof ( args [1]);
c = stof ( args [2]);
System . out . println (" Roots of " + c + " + " + b + " x + " +
a + " x ^2 = 0 are :");
90
91
92
93
94
95
roots = q u ad _ s o l v e _ u n s t a b le (c , b , a );
System . out . println ("
" + roots [0] + " and " + roots [1] +
" using numerically unstable equation .");
96
97
98
99
roots = qu ad_ sol ve _st abl e (c , b , a );
System . out . println ("
" + roots [0] + " and " + roots [1] +
" using numerically stable equations .");
100
101
102
}
103
104
}
105
106
107
108
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // stof -- convert a String argument into a float and return it
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109
110
111
static float stof ( String s ) {
float val ;
112
val = ( float ) Float . parseFloat ( s );
return val ;
113
114
115
}
116
117
118
119
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // discrim -- calculate and return the discriminant
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120
121
122
static float discrim ( float c , float b , float a ) {
float d ;
123
//
//
//
//
124
125
126
127
Calculate the square of the discriminant and ensure that is
non - negative : if it were , Math . sqrt would throw an exception ; so
generate an error message and terminate the program .
If the value is non - negative , take the square root and return it .
128
d = b * b - 4.0 f * a * c ;
if ( d < 0.0 f ) {
System . err . println (" Alas , those values yield complex roots , " +
" which I cannot handle .");
System . exit (1);
}
return ( float ) Math . sqrt ( d );
129
130
131
132
133
134
135
136
}
137
138
139
140
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // q u a d _ s o lv e _ u n s t a b l e -- solve quadratic using " conventional " method
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141
142
143
144
static float [] q u a d _ s o l v e _u n s t a b l e ( float c , float b , float a ) {
float d ;
float roots [] = new float [2];
145
// Calculate the discriminant and , from that , work out the roots using
// the equation given in the comment at the top of the program .
146
147
148
d = discrim (c , b , a );
roots [0] = ( - b - d ) / 2.0 f / a ;
roots [1] = ( - b + d ) / 2.0 f / a ;
return roots ;
149
150
151
152
153
}
154
3
155
156
157
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // q uad _so lve _s tab le -- solve quadratic using numerically - stable method
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
158
static float [] qua d_s olv e_s tab le ( float c , float b , float a ) {
float q ;
float roots [] = new float [2];
159
160
161
162
//
//
//
//
163
164
165
166
The numerically - stable method we use is given in Press et al ( see
top of program for reference ). This involves calculating the " q "
value , which incorporates the discriminant , and using that to work
out the two roots .
167
q = ( b + sgn ( b ) * discrim (c , b , a )) / -2.0 f ;
roots [0] = q / a ;
roots [1] = c / q ;
return roots ;
168
169
170
171
}
172
173
174
175
176
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // sgn -- return -1 if argument is negative , otherwise return +1
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177
static float sgn ( float f ) {
return ( f < 0.0 f ) ? -1.0 f : 1.0 f ;
}
178
179
180
181
}
182
183
184
185
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // E n d
o f
q u a d r o o t s . j a v a
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4
© Copyright 2026 Paperzz