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

Produce a Run / Short Form Trace for the following Segment of Java code: \"That

ID: 656070 • Letter: P

Question

Produce a Run / Short Form Trace for the following Segment of Java code:
"That is, trace all of the program's variables in a table and show everything which appears on the
screen, exactly as it appears".


final int SIZE = 3;
double[] coefs = {2.0, 3.0, 5.0};
double xVal = 2.0;
for (int pow = coefs.length-1; pow >= 0; pow--)
{
System.out.print(coefs[pow] + "x^" + pow);
if (pow != 0)
System.out.print(" + ");
}
System.out.println();
double result = 0.0;
double pox = xVal * xVal;
int pow = SIZE-1;
do
{
System.out.println();
System.out.println("result = " + result);
System.out.println("pox = " + pox);
System.out.println("pow = " + pow);
result = result + coefs[pow] * pox;
pox = pox / xVal;
--pow;
} while (pox >= 1);
System.out.println();
System.out.println("evaluated when x = " + xVal + ", equals " + result);

Explanation / Answer

Program:

public class Demo1 {
public static void main(String[] args) {
final int SIZE = 3;
double[] coefs = {2.0, 3.0, 5.0};
System.out.println("SIZE "+SIZE);
double xVal = 2.0;
System.out.println("xVal "+xVal);


System.out.println("coefs "+coefs[0]+" : "+coefs[1]+" : "+coefs[2]);


for (int pow = coefs.length-1; pow >= 0; pow--)
{

System.out.print(coefs[pow] + "x^" + pow);
if (pow != 0)
System.out.print(" + ");
}
System.out.println();
double result = 0.0;
double pox = xVal * xVal;
int pow = SIZE-1;
do
{
System.out.println();
System.out.println("result = " + result);
System.out.println("pox = " + pox);
System.out.println("pow = " + pow);
result = result + coefs[pow] * pox;
pox = pox / xVal;
--pow;
} while (pox >= 1);
System.out.println();
System.out.println("evaluated when x = " + xVal + ", equals " + result);
}

}

Output:

SIZE 3
xVal 2.0
coefs 2.0 : 3.0 : 5.0
5.0x^2 + 3.0x^1 + 2.0x^0

result = 0.0
pox = 4.0
pow = 2

result = 20.0
pox = 2.0
pow = 1

result = 26.0
pox = 1.0
pow = 0

evaluated when x = 2.0, equals 28.0

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