In: Computer Science
Consider the following code fragment:
1 int a = 20;
2 int b;
3 double x = 3.5;
4 String s = "All";
5 char ch;
6
7 x += a;
8 x--;
9 a /= 4 - 1;
10 b = s.length();
11 b += 4;
12 s += "is well";
13 ch = s.charAt(b);
14 System.out.println("a = " + a + ", b = " + b);
15 System.out.println("x = " + x + "\ns = " + s);
16 System.out.println("ch = " + ch);
Trace the above code, using the following format:
Use a separate row for each statement.
Write a Java program that executes the commands in the code fragment.
import java.io.*;
class StringProcessing {
public static void main (String[] args) {
int a=20,b;
double x=3.5;
String s="All";
char ch;
x+=a; //return x=23.5
x--; //return x=22.5
a/=4-1; //return a=20/(4-1)=6
b=s.length(); //return b=3 as length of s is 3
b+=4; //return b=b+4=3+4=7;
s+="is well"; //s="All"+"is well"= "Allis well"
ch=s.charAt(b); //ch=s.charAt(7)= e
System.out.println("a= "+a+", b= "+b);
System.out.println("x= "+x+"\ns= "+s);
System.out.println("ch= "+ch);
}
}