Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume all necessary imports have been made, an appropriate plotSegment method h

ID: 3534895 • Letter: A

Question

Assume all necessary imports have been made, an appropriate plotSegment method has been defined, and x and y have been declared with the statement, int x, y;.
a) What's still wrong with the following code fragment?


while (!xStr.equalsIgnoreCase("q"))
{
yStr = stdIn.next();
try
{
x = Integer.parseInt(xStr);
y = Integer.parseInt(yStr);
}
catch (NumberFormatException nfe)
{
System.out.println("Invalid entry: " + xStr + " " + yStr
+ " Must enter integer space integer.");
}
line.plotSegment(x, y);
System.out.print("Enter x & y coordinates (q to quit): ");
xStr = stdIn.next();
} // end while

Explanation / Answer

//you should try like this


while (true)

{

System.out.print("Enter x & y coordinates (q to quit): ");

xStr = stdIn.next();

if(xStr.equalsIgnoreCase("q"))

break;

yStr = stdIn.next();

try

{

x = Integer.parseInt(xStr);

y = Integer.parseInt(yStr);

}

catch (NumberFormatException nfe)

{

System.out.println("Invalid entry: " + xStr + " " + yStr

+ " Must enter integer space integer.");

}

line.plotSegment(x, y);

} // end while

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote