JAVA 22 Show the value of x after this statement double x 1
JAVA
2.2 Show the value of x after this statement: double x = 1/2;
i =
j=
i =
j=
i =
j =
i =
j =
i =
j =
(20 points) 4.1. (15 points) Write the body of this method. i =
/**
* Returns the smallest value in a. *
* @param a * an array of ints
* @return smallest value in the array a
* @requires * a.length >= 1 * @ensures * minimum = [the smallest value in a] */
private static int minimum(int[] a) { }
4.2. (5 points) Clearly and concisely explain why the precondition is important here.
5. (10 points) Trace the effect of the line of code below that calls squareAndReset by tracing the execution of the method body of squareAndReset for this call.
Tracing table for the call:
num =
sq =
x =
square =
x =
square =
return square;
}
7. (9 points) The following is a valid XML document. Sunny with 90% probability of football
7.1. (8 points) Draw the XMLTree associated with this document. (Don’t worry about drawing the “tag” icon; just include the enclosing brackets <...> for each label that is a tag.)
7.2. (1 point) How many children does the root node of this XMLTree have?
8. (6 points) The following is not a valid XML document. Circle the locations of three nonoverlapping errors that make it invalid, briefly explaining each error in the space below.
<?xml version = \"1.0\"?>
<pony>
<unicorn mark = \"three lozenges\">Rarity
<color>cyan</unicorn>
</color>
<unicorn mark = \"six-pointed star\">Twilight Sparkle</unicorn>
<unicorn mark = \"two dolphins\" version=\"G4\" />
</pony>
<dragon>
<youth>Spike
</dragon>
| Code | State |
| int i = j; | |
| i = | |
| int j = 2; | |
| i = j= | |
| while(i < j) { | |
| i = j= | |
| i = i * i +1; | |
| i = j = | |
| j = j+2; | |
| i = j = | |
| } | |
| i = j = |
Solution
2.2 Show the value of x after this statement: double x = 1/2;
Result: 0.0
i = Variable not declared
j = 2
i = 2
j= 3
i = 5
j = 3
i = 5
j = 5
i = 5
j = 5
4.1. Answer
import java.util.*;
//Class defined
class Demo
{
//Method to return the minimum value from an array
private static int minimum(int[] a)
{
//Initialize the variable small to the first element of the array
int small = a[0];
//Loops from 1 index position to the end of the array
for(int x = 1; x < a.length; x++)
//If the current index position of the array is less than the small
if(a[x] < small)
//Update the small with current position of the array
small = a[x];
//Return the smallest value
return small;
}
public static void main(String ss[])
{
//Declare a variable to store the size of array. Initially zero.
int n = 0;
//Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
//Accept data till size is greater than zero
do
{
System.out.println(\"Enter the size of array must be >= 1\");
n = sc.nextInt();
}while(n <= 0);
//Declare array with the size entered by the user.
int [] a = new int[n];
//Accept data to the array
System.out.println(\"Enter \" + n + \" elements into array \");
for(int x = 0; x < n; x++)
a[x] = sc.nextInt();
//Call the method to display the smallest value
System.out.println(\"Smallest value: \" + minimum(a));
}
}
output
Enter the size of array must be >= 1
0
Enter the size of array must be >= 1
-2
Enter the size of array must be >= 1
4
Enter 4 elements into array
12
5
2
55
Smallest value: 2
4.2. Clearly and concisely explain why the precondition is important here.
Precondition: Size must be greater than zero.
Array size cannot be zero or negative.
5.
num = 13.0
sq = 169.0
x = 13.0
square = 169.0
x = 0.0
square = 169.0
return square;
}
| Code | State |
| int i = j; | |
| i = Invalid statement. Will generate error, because j is not declared. | |
| int j = 2; | |
| i = Variable not declared j = 2 | |
| while(i < j) { | if int i = j is our second statement then no output will occur, because the condition is false. if i = 2 and j = 3 then the follwoing output will display. |
| i = 2 j= 3 | |
| i = i * i +1; | |
| i = 5 j = 3 | |
| j = j+2; | |
| i = 5 j = 5 | |
| } | |
| i = 5 j = 5 |



