Roger Sanchez

Greenhorn

Posts: 16

posted 14 years ago

I am studying for a exam and am trying to work through this sample question (in case it actually shows up on the real thing). Can anyone help?

Thanks,

Roger

The Quadratic applet--see the sketch of the opening screen below--solves equations in the form a * x * x + b * x + c = 0.0 where the numbers (called coefficients) a, b, and c are supplied by the user--in thethree editable text fields (instances of class TextField in Version2, and of class JTextField in Version 3) of the top row of the applet.

Whenever the user clicks on the SOLVE button, the applet responds by displaying a message in the TextField in the second row of the applet. The message is one of four possible types:

"Not all three coefficients are valid" This string is displayed if Java cannot convert the contents of one or more text fields in the top row into a double type number. HINT: Use Double.parseDouble(s) to convert a String s into primitive double type--this call will throw a NumberFormatException if s does not represent a valid double value.

"There is no (real) solution to this problem"

This string is displayed if b*b-4*a*c is negative. HINT: Given a quadratic equation a*x*x+b*x+c=0, define its "discriminant" d as d=b*b-4*a*c. The equation has two solutions--if any at all--and these are given by the formulas

x1 = ( -b + Math.sqrt(d) ) / ( 2 * a )

x2 = ( -b - Math.sqrt(d) ) / ( 2 * a )

A negative number, however, does not have a (real) square root, so if d is negative, then the equation does not have a (real) solution at all.

X = " + x1 + " or X = " + x2

This String expression is displayed if d is positive. (d, x1, and x2 are defined in 2) > above.

" X = " + x1

This String expression is displayed when d is zero. (According to the formulas in 2) above, d=0 implies that x1=x2=(-b)/(2*a), that is, solutions x1 and x2 are one and the same, so we have a single solution.) Based on the information above and the code below, complete the code of the Quadratic applet. In particular, write the code missing from the body of the actionPerformed method.

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]

[ July 25, 2003: Message edited by: Dirk Schreckmann ]

Thanks,

Roger

The Quadratic applet--see the sketch of the opening screen below--solves equations in the form a * x * x + b * x + c = 0.0 where the numbers (called coefficients) a, b, and c are supplied by the user--in thethree editable text fields (instances of class TextField in Version2, and of class JTextField in Version 3) of the top row of the applet.

Whenever the user clicks on the SOLVE button, the applet responds by displaying a message in the TextField in the second row of the applet. The message is one of four possible types:

"Not all three coefficients are valid" This string is displayed if Java cannot convert the contents of one or more text fields in the top row into a double type number. HINT: Use Double.parseDouble(s) to convert a String s into primitive double type--this call will throw a NumberFormatException if s does not represent a valid double value.

"There is no (real) solution to this problem"

This string is displayed if b*b-4*a*c is negative. HINT: Given a quadratic equation a*x*x+b*x+c=0, define its "discriminant" d as d=b*b-4*a*c. The equation has two solutions--if any at all--and these are given by the formulas

x1 = ( -b + Math.sqrt(d) ) / ( 2 * a )

x2 = ( -b - Math.sqrt(d) ) / ( 2 * a )

A negative number, however, does not have a (real) square root, so if d is negative, then the equation does not have a (real) solution at all.

X = " + x1 + " or X = " + x2

This String expression is displayed if d is positive. (d, x1, and x2 are defined in 2) > above.

" X = " + x1

This String expression is displayed when d is zero. (According to the formulas in 2) above, d=0 implies that x1=x2=(-b)/(2*a), that is, solutions x1 and x2 are one and the same, so we have a single solution.) Based on the information above and the code below, complete the code of the Quadratic applet. In particular, write the code missing from the body of the actionPerformed method.

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]

[ July 25, 2003: Message edited by: Dirk Schreckmann ]

Consider Paul's rocket mass heater. |