Write Java code for a loop that simultaneously computes both the maximum and min
ID: 3666255 • Letter: W
Question
Write Java code for a loop that simultaneously computes both the maximum and minimum of an array of ints called a.
Write Java code for a loop that sets boolean variable isOrdered to true if the elements of a given array of ints called a are in non-decreasing order, otherwise it sets isOrdered to false.
Consider the following XML document (adapted from here):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<breakfast_menu>
<food calories="650">
<name>Belgian Waffles</name>
<price>$5.95</price>
</food>
<food calories="630">
<name>Blueberry Pancakes</name>
<price>$4.95</price>
</food>
<food calories="600">
<name>French Toast</name>
<price>$4.50</price>
</food>
<food calories="950">
<name>Homestyle Breakfast</name>
<price>$6.95</price>
</food>
</breakfast_menu>
Answer the following questions.
Draw the complete XMLTree corresponding to this document.
Assume you have an XMLTree variable called menu initialized with the value of the XML document above. What is the value returned by each of the following calls?
menu.isTag()
menu.label()
menu.numberOfChildren()
menu.hasAttribute("calories")
menu.child(1)
menu.child(1).isTag()
menu.child(1).label()
menu.child(1).numberOfChildren()
menu.child(1).hasAttribute("calories")
menu.child(1).attributeValue("calories")
menu.child(1).child(0).isTag()
menu.child(1).child(0).child(0).isTag()
menu.child(1).child(0).child(0).label()
Modify the document so that the price is an attribute of the name tag.
Redraw the complete XMLTree corresponding to the new document.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<breakfast_menu>
<food calories="650">
<name>Belgian Waffles</name>
<price>$5.95</price>
</food>
<food calories="630">
<name>Blueberry Pancakes</name>
<price>$4.95</price>
</food>
<food calories="600">
<name>French Toast</name>
<price>$4.50</price>
</food>
<food calories="950">
<name>Homestyle Breakfast</name>
<price>$6.95</price>
</food>
</breakfast_menu>
Explanation / Answer
Program that simultaneously computes both the maximum and minimum of an array of ints called a
import java.util.*;
import java.lang.*;
import java.io.*;
class arrayMaxMin // class named arrayMaxMin
{
public static void main (String[] args) throws java.lang.Exception
{
int[] a = {4,8,2,9,7,1}; // array of 6 integer elements
int minValue = a[0]; // initialy taken value at first index as minimum value
int maxValue = a[0]; // initialy taken value at first index as maximum value
for (int i = 0; i < a.length; i++)
{
if(a[i] < minValue)
{
minValue = a[i];
}
if(a[i] > maxValue)
{
maxValue = a[i];
}
}
System.out.println("Minimum value of the array is = " + minValue);
System.out.println("Maximum value of the array is = " + maxValue);
}
}
Java code for a loop that sets boolean variable isOrdered to true if the elements of a given array of ints called a are in non-decreasing order, otherwise it sets isOrdered to false.
import java.util.*;
import java.lang.*;
import java.io.*;
class checkArrayOrder // class named checkArrayOrder
{
public static void main (String[] args) throws java.lang.Exception
{
int[] a = {4,8,2,9,7,1};
boolean isOrdered = false;
int i = 0;
while(i < a.length -1)
{
if(a[i] > a[i+1])
{
isOrdered = false;
}
else
{
isOrdered = true;
break;
}
i++;
}
if(isOrdered)
{
System.out.println("Element of array are non decreasing in order");
}
else
{
System.out.println("Element of array are in decreasing in order");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.