![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |
Here's theSort
program again:
public class Sort { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } }
- Identify all of the variables in the program.
- What is the name of each variable?
- What is the data type for each variable?
- What is the scope of each variable?
Recall theMaxVariables
program.
public class MaxVariables { public static void main(String args[]) { // integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; // other primitive types char aChar = 'S'; boolean aBoolean = true; // display them all System.out.println("The largest byte value is " + largestByte); System.out.println("The largest short value is " + largestShort); System.out.println("The largest integer value is " + largestInteger); System.out.println("The largest long value is " + largestLong); System.out.println("The largest float value is " + largestFloat); System.out.println("The largest double value is " + largestDouble); if (Character.isUpperCase(aChar)) { System.out.println("The character " + aChar + " is upper case."); } else { System.out.println("The character " + aChar + " is lower case."); } System.out.println("The value of aBoolean is " + aBoolean); } }Check your answers.
- Rewrite it to display the minimum value of each integer data type. You can either guess at the names of the class variables within each type wrapper class that provides the minimum, you can look them up in the javadocs, or you can calculate the values based on what you know about the size of the primitive types.
- Can you guess the name of a different method in the
Character
class that determines whether or not achar
is a upper case? Use that method in the program.- Modify the program so that
aBoolean
has a different value.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson | Search |