Programming and Problem Solving with Java

(やまだぃちぅ) #1

206


to determine the slope of the line. If x1equals x2, the line is vertical and the
slope is undefined. The segment should display the slope with an appropriate la-
bel. If the slope is undefined, it should display the message “Slope undefined.”
11.Given the floatvariables a,b,c,root1,root2, and discriminant, write a code
segment to determine whether the roots of a quadratic polynomial are real or
complex (imaginary). If the roots are real, find them and assign them to root1
and root2. If they are complex, write the message “No real roots.”
The formula for the solution to the quadratic equation is

The means “plus or minus” and indicates that the equation has two solutions:
one in which the result of the square root is added to band one in which the
result is subtracted from b. The roots are real if the discriminant (the quantity
under the square root sign) is not negative.

Programming Problems
1.Design and write a Java application that takes as input a single letter and
displays the corresponding digit on the telephone. The letters and digits on a
telephone are grouped this way:
2 = ABC 4 = GHI 6 = MNO 8 = TUV
3 = DEF 5 = JKL 7 = PRS 9 = WXY
No digit corresponds to either Q or Z. For these two letters, your application
should display a message indicating that they are not used on a telephone.
The screen dialog might look like this:
Enter a single letter, and I will tell you what the corresponding
digit is on the telephone.
R
The digit 7 corresponds to the letter R on the telephone.
Here’s another example:
Enter a single letter, and I will tell you what the corresponding
digit is on the telephone.
Q
There is no digit on the telephone that corresponds to Q.
Your code should display a message indicating that there is no matching digit
for any nonalphabetic character entered by the user. Also, the application should
recognize only uppercase letters. Include the lowercase letters with the invalid
characters.

–bb^2 –4ac
2 a
Free download pdf