Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 1. Java Introduction
Current Topic: 1.2.6. Array and For loop utilities
Sub-Topics: 1.2.6.1. Java samples - working with numbers
-- Scroll to check for more content below...
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Make sure that you understand and completed the previous subject.

Thinking in Java takes time and exercises. There are many simple computational tasks that we do daily.
For example, we calculate a sum of several lines of numbers, hopefully on a receiving side.
We know how to calculate an average age in a group if we know all participant ages.
It is easy for us to look at the list of names to find out if our name is there.
We can quickly find maximum and minimum in a set of numbers.
How to do all these tricks with a program written in Java?

This module will answer these question and will provide the samples to make you comfortable.
This module introduces the array and the for loop concepts.
Take it slowly, do one sample at a time. Do not try to swallow too big of a piece :-)

First of all note that all these problems deal with several numbers or several strings. In Java several things are called an array.
int[] myNumbers = {3,6,8,9}; // an array of integers
String[] names = { "Ben", "Jeff", "Julie", "Eli" }; // an array of strings

Walking through an array is a boring, repeatable operation. In Java we walk over an array with the for loop:
/* There are four parts in the for loop:
1) Initialization ? we usually initialize the index of the next element to its initial value: int i = 0;
2) Condition ? the loop will continue till the condition is true: i < names.length; - the index is less than the length of the array
3) An action that the loop will perform each time at the end of the loop: i++ - in this case increasing the index, so the next loop will touch the next element of the array
4) The body of the loop ? with the if statement the program checks if the next string is equals the name "Jeff" and if so, the program will display the line "Jeff was found!!!" and will exit the loop
*/

for(int i=0; i < names.length; i++) { // Parts 1, 2, and 3 of the for loop are in this line of the code
// This is the part 4 that is often called the block of the code executed in the for loop
if(names[i].equals("Jeff")) { // check the next element by indexing this element in the array as names[i]
System.out.println("Jeff was found!!!");
break; // exit the for loop
}
}

Armed with what we just learned, let us create Java methods to handle basic algorithms, such as finding maximum or minimum and more.
We start with simple examples, each in a separate class.

Create a new package day4 in Eclipse under the project week1. For each sample below create a proper class by typing the code samples. Then, run the class by using Run As ? Java Application options.
The sample below finds maximum number in an array of numbers.

package day4;
/**
* Find MAX in array
* int[] numbers = {1,2,3,7,5};
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindMaxSample {
public static void main(String[] args) {
// prepare for the loop
int[] numbers = {1,2,3,7,5};
// init the target
int max = numbers[0];
// arrange the loop
for(int i=0; i < numbers.length; i++) {
// work with the next element
if(numbers[i] > max) { // if the next element is more than current maximum
max = numbers[i]; // replace max
}
}
// print the results
System.out.println("MAX="+max);
}
}

Before going to the next sample, please create and run the FindMaxSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds minimum number in an array of numbers.

package day4;
/**
* Find MIN in array
* int[] numbers = {1,2,3,7,5};
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindMinSample {
public static void main(String[] args) {
// prepare for the loop
int[] numbers = {1,2,3,7,5};
// init the target
int min = numbers[0];
// arrange the loop
for(int i=0; i < numbers.length; i++) {
// work with the next element
if(numbers[i] < min) { // if the next element is less than current minimum
min = numbers[i]; // replace max
}
}
// print the results
System.out.println("MIN="+min);
}
}

Before going to the next sample, please create and run the FindMinSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a specific number in an array of numbers.

package day4;
/**
*Find out whether the number "5"
*is present in an integer array.
Input: int k[]={10,90,2,5,50}
Output:
Yes, the number 5 is found in the array
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindNumberSample {

public static void main(String[] args) {
// data
int k[]={10,90,2,5,50};
// arrange the FOR loop
for(int i=0; i < k.length; i++) {
// look for 5
if(k[i] == 5) {
System.out.println("Yes, "
+ "the number 5 is found!");
}
}
}
}

Before going to the next sample, please create and run the FindNumberSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a sum of numbers in an array of numbers.

package day4;
/**
* Write a Java program to print the sum of all numbers
* from an integer array.
* You can hard code all the values in the integer array.

Input:
Hard code integer numbers in array
Example:
int x[]={10,90,20,100}

Output:
The sum of all integers from the array is:
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindSumSample {

public static void main(String[] args) {
// data
int x[]={10,90,20,100};
int sum = 0;
for(int i=0; i < x.length; i++) {
// add next number
sum = sum + x[i];
}
System.out.println("Sum="+sum);
}

}

Before going to the next sample, please create and run the FindSumSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a average in an array of numbers.

package day4;
/**
* Find average of an array of numbers
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindAverageSample {
public static void main(String[] args) {
// data
int[] scores = {1,2,3,4,5,6,7};
// add all numbers and divide by the length
double sum = 0; // init the target
// arrange the loop
for(int i=0; i < scores.length; i++) {
// add next number
sum = sum + scores[i];
}
// calculate ave by dividing
double ave = sum / scores.length;
System.out.println("Average="+ave);
}
}

Before going to the next sample, please create and run the FindAverageSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a specific string in an array of strings.

package day4;
/**
* Find a specific string of an array of strings
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public class FindStringSample {
public static void main(String[] args) {
// data
String[] names = { "Ben", "Jeff", "Julie", "Eli" }; // an array of strings
// arrange the loop
for(int i=0; i < names.length; i++) { // Parts 1, 2, and 3 of the for loop are in this line of the code
// This is the part 4 that is often called the block of the code executed in the for loop
if(names[i].equals("Jeff")) { // check the next element by indexing this element in the array as names[i]
System.out.println("Jeff was found!!!");
break; // exit the for loop
} // end of if statement
} // end of the for loop
} // end of the main method
} // end of the class
Was it clear so far?

Before going to the next sample, please create and run the FindStringSample class in Eclipse.

Now, let us collect these convenient utilities into a single class with many methods, which can be called often and easily. We make these methods static. That means that the methods can be called directly with the class name without creation of the object of a class.

To make the methods more flexible we pass an array as a parameter in the methods and make methods to return the target result.
For example, the FindMaxSample will become the following method: public static int findMax(int[] numbers)

This was just a starting point for the method. Note that we pass an array of numbers as a parameter in the method. See public static int findMax (int[] numbers).
And the method returns one integer number (maximum). See public static int findMax(int[] numbers).

Here is the source code for the utility methods below. Note, that the class and each method started with a plan-header clearly stated the goals before the code implementation. Create a new class in Eclipse under the same package day4. Name the class as AlgorithmUtilities, type all the methods provided below and then test the class by using Run As ?Java Application options.

package day1;
/**
* The class is a collection of convenient utilities - methods, which can be called often and easily.
* We make these methods static.
* That means that the methods can be called directly with the class name without creation of the object of a class.
* To make the methods more flexible we pass an array as a parameter in the methods and make methods to return the target result.
* The class includes the following methods: findMax, findMin, findNumber, findSum, findAverage, findString
* @author Jeff
*
*/
public class AlgorithmUtilities {
/**
* Find MAX in array of integers
* @param numbers array of integers
* @return max
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static int findMax(int[] numbers) {
// init the target
int max = numbers[0];
// arrange the loop
for(int i=0; i < numbers.length; i++) {
// work with the next element
if(numbers[i] > max) { // if the next element is more than current maximum
max = numbers[i]; // replace max
}
}
// return the result
return max;
}
/**
* Find MIN in array of integers
* @param numbers array of integers
* @return min
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static int findMin(int[] numbers) {
// init the target
int min = numbers[0];
// arrange the loop
for(int i=0; i < numbers.length; i++) {
// work with the next element
if(numbers[i] < min) { // if the next element is less than current minimum
min = numbers[i]; // replace max
}
}
// return the result
return min;
}
/**
* Find out whether a specific number, for example, "5"
* is present in an integer array, numbers.
Input: int numbers[], for example, {10,90,2,5,50}
Input: int findMeNumber, for example, 5
Output: the index of the found number or -1 if not found

* @param numbers an integer array
* @param findMeNumber
* @return the index of the findMeNumber or -1 if not found
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static int findNumber(int[] numbers, int findMeNumber) {
// arrange the FOR loop
for(int i=0; i < numbers.length; i++) {
// look for findMeNumber
if(numbers[i] == findMeNumber) {
return i; // index of the number in the array
}
}
return -1; // if we finished the loop without finding the number, we return -1
}
/**
* Calculate and return the sum of all numbers
* from an integer array.
* @param numbers array of int
* @return sum
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static int findSum(int[] numbers) {
// initiate the target
int sum = 0;
for(int i=0; i < numbers.length; i++) {
// add next number
sum = sum + numbers[i];
}
return sum;
}
/**
* Calculate and return the average of all numbers
* from an integer array.
* Return double type as the result to be more precise while calculating average
* @param numbers array of int
* @return average
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static double findAverage(int[] numbers) {
// initiate the target
double sum = 0; // make it double for precision
for(int i=0; i < numbers.length; i++) {
// add next number
sum = sum + numbers[i];
}
double average = sum/numbers.length;
return average;
}
/**
* Find a specific string in an array of strings
* @param names array of strings
* @param findMeName specific string to find
* @return the index of the found string in array or -1 if not found
* @author Jeff.Zhuk@JavaSchool.com
*
*/
public static int findString(String[] names, String findMeName) {
// arrange the FOR loop
for(int i=0; i < names.length; i++) {
// look for findMeName
if(names[i].equals(findMeName) ) { // note that instead of == we use the equals method to compare strings
return i; // index of the string in the array
}
}
return -1; // if we finished the loop without finding the number, we return -1
}
/**
* The main method will test all the methods above by providing input parameters and displaying the results
* In the future, JUnit utilities can be used to automatically provide such testing
* @author Jeff.Zhuk@JavaSchool.com
*/
public static void main(String[] args) {
int max = findMax(new int[] { 1,3,6,4,8});
System.out.println("max="+max);
int min = findMin(new int[] { 1,3,6,4,8});
System.out.println("min="+min);
int indexOfNumber = findNumber(new int[] { 1,3,6,4,8}, 4);
System.out.println("indexOfNumber="+indexOfNumber);
int sum = findSum(new int[] { 1,3,6,4,8});
System.out.println("sum="+sum);
double average = findAverage(new int[] { 1,3,6,4,8});
System.out.println("average="+average);
int indexOfName = findString(new String[] { "Julie","Jeff","Ben","Eli"}, "Ben");
System.out.println("indexOfNumber="+indexOfName);
}
}


There is more than one way to arrange a loop in Java.
Besides the for loop there is also while and do while loops.
The while statement continually executes a block of statements while a condition is true.

while (condition) {
// statements
}

Example:

int i = 5;
while (i > 0) {
System.out.println(i);
i--; // same as: i = i - 1;
}

The while loop above will display the lines with i ranging from 5 till 1.
Note, that we did initialization (i=5) in the beginning and we did the change (i--) at the end.
In the for loop all three parts together with the condition would be done in one line:

for(int i=5; i > 0; i--) {
System.out.println(i);
}
Another way of expressing the same thing would be:
do {
// statements
} while (condition);

Example:
int i=5;
do {
System.out.println(i--);
} while (i > 0);

Yes, it does exactly the same thing.
A conclusion: just use the for loop, which is more expressive than the others.

Assignments:
Answer the QnA and create at least 2 more QnA related to the subject.
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>for(int i=0; i < names.length; i++) { // Parts 1, 2, and 3 of the <b>for</b> loop are in this line of the code
<br/>   // This is the part 4 that is often called the block of the code executed in the for loop
<br/>   if(names[i].equals("Jeff")) { // check the next element by indexing this element in the array as <b>names[i]</b>
<br/>       System.out.println("Jeff was found!!!");
<br/>       break; // exit the for loop 
<br/>   }
<br/>}
<br/>

Armed with what we just learned, let us create Java methods to handle basic algorithms, such as finding maximum or minimum and more.
We start with simple examples, each in a separate class.

Create a new package day4 in Eclipse under the project week1. For each sample below create a proper class by typing the code samples. Then, run the class by using Run As ? Java Application options.
The sample below finds maximum number in an array of numbers.
<br/>package day4;
<br/>/**
<br/> * Find MAX in array
<br/> * int[] numbers = {1,2,3,7,5};
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class FindMaxSample {
<br/>	public static void main(String[] args) {
<br/>		// prepare for the loop
<br/>		int[] numbers = {1,2,3,7,5};
<br/>		// init the target
<br/>		int max = numbers[0];
<br/>		// arrange the loop
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// work with the next element
<br/>			if(numbers[i] > max) { // if the next element is more than current maximum
<br/>				max = numbers[i]; // replace max
<br/>			}
<br/>		}
<br/>		// print the results
<br/>		System.out.println("MAX="+max);
<br/>	}
<br/>}  
<br/>

Before going to the next sample, please create and run the FindMaxSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds minimum number in an array of numbers.
<br/>package day4;
<br/>/**
<br/> * Find MIN in array
<br/> * int[] numbers = {1,2,3,7,5};
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class FindMinSample {
<br/>	public static void main(String[] args) {
<br/>		// prepare for the loop
<br/>		int[] numbers = {1,2,3,7,5};
<br/>		// init the target
<br/>		int min = numbers[0];
<br/>		// arrange the loop
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// work with the next element
<br/>			if(numbers[i] < min) { // if the next element is less than current minimum
<br/>				min = numbers[i]; // replace max
<br/>			}
<br/>		}
<br/>		// print the results
<br/>		System.out.println("MIN="+min);
<br/>	}
<br/>}  
<br/>

Before going to the next sample, please create and run the FindMinSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a specific number in an array of numbers.
<br/>package day4;
<br/>/**
<br/> *Find out whether the number "5" 
<br/> *is present in an integer array. 
<br/>Input:   int k[]={10,90,2,5,50}
<br/>Output:
<br/>    Yes, the number 5 is found in the array
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> * 
<br/> */
<br/>public class FindNumberSample {
<br/>
<br/>	public static void main(String[] args) {
<br/>		// data
<br/>		int k[]={10,90,2,5,50};
<br/>		// arrange the FOR loop
<br/>		for(int i=0; i < k.length; i++) {
<br/>			// look for 5
<br/>			if(k[i] == 5) {
<br/>				System.out.println("Yes, "
<br/>				+ "the number 5 is found!");
<br/>			}
<br/>		}
<br/>	}
<br/>}
<br/>

Before going to the next sample, please create and run the FindNumberSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a sum of numbers in an array of numbers.
<br/>package day4;
<br/>/**
<br/> * Write a Java program to print the sum of all numbers 
<br/> * from an integer array. 
<br/> * You can hard code all the values in the integer array.
<br/>
<br/>Input:
<br/>    Hard code integer numbers in array
<br/>    Example:
<br/>        int x[]={10,90,20,100}
<br/>
<br/>Output:
<br/>    The sum of all integers from the array is:
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class FindSumSample {
<br/>	
<br/>	public static void main(String[] args) {
<br/>		// data
<br/>		int x[]={10,90,20,100};
<br/>		int sum = 0;
<br/>		for(int i=0; i < x.length; i++) {
<br/>			// add next number
<br/>			sum = sum + x[i];
<br/>		}
<br/>		System.out.println("Sum="+sum);
<br/>	}
<br/>
<br/>}
<br/>

Before going to the next sample, please create and run the FindSumSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a average in an array of numbers.
<br/>package day4;
<br/>/**
<br/> * Find average of an array of numbers
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class FindAverageSample {
<br/>	public static void main(String[] args) {
<br/>		// data
<br/>		int[] scores = {1,2,3,4,5,6,7};
<br/>		// add all numbers and divide by the length
<br/>		double sum = 0; // init the target
<br/>		// arrange the loop
<br/>		for(int i=0; i < scores.length; i++) {
<br/>			// add next number
<br/>			sum = sum + scores[i];
<br/>		}
<br/>		// calculate ave by dividing
<br/>		double ave = sum / scores.length;
<br/>		System.out.println("Average="+ave);
<br/>	}
<br/>}		
<br/>

Before going to the next sample, please create and run the FindAverageSample class in Eclipse.
--------------------------------------------------------------------------------
The sample below finds a specific string in an array of strings.
<br/>package day4;
<br/>/**
<br/> * Find a specific string of an array of strings
<br/> * @author Jeff.Zhuk@JavaSchool.com
<br/> *
<br/> */
<br/>public class FindStringSample {
<br/>	public static void main(String[] args) {
<br/>		// data
<br/>		String[] names = { "Ben", "Jeff", "Julie", "Eli" }; // an array of strings
<br/>		// arrange the loop
<br/>		for(int i=0; i < names.length; i++) { // Parts 1, 2, and 3 of the <b>for</b> loop are in this line of the code
<br/>  		      // This is the part 4 that is often called the block of the code executed in the for loop
<br/>   		      if(names[i].equals("Jeff")) { // check the next element by indexing this element in the array as <b>names[i]</b>
<br/>       			System.out.println("Jeff was found!!!");
<br/>       			break; // exit the for loop 
<br/>  		     } // end of <b>if</b> statement
<br/>      } // end of the <b>for</b> loop
<br/>} // end of the main method
<br/>}    // end of the class	
<br/>






Was it clear so far?


Before going to the next sample, please create and run the FindStringSample class in Eclipse.

Now, let us collect these convenient utilities into a single class with many methods, which can be called often and easily. We make these methods static. That means that the methods can be called directly with the class name without creation of the object of a class.

To make the methods more flexible we pass an array as a parameter in the methods and make methods to return the target result.
For example, the FindMaxSample will become the following method: public static int findMax(int[] numbers)

This was just a starting point for the method. Note that we pass an array of numbers as a parameter in the method. See public static int findMax (int[] numbers).
And the method returns one integer number (maximum). See public static int findMax(int[] numbers).

Here is the source code for the utility methods below. Note, that the class and each method started with a plan-header clearly stated the goals before the code implementation. Create a new class in Eclipse under the same package day4. Name the class as AlgorithmUtilities, type all the methods provided below and then test the class by using Run As ?Java Application options.
<br/>package day1;
<br/>/**
<br/> * The class is a collection of convenient utilities - methods, which can be called often and easily. 
<br/> * We make these methods <b>static</b>. 
<br/> * That means that the methods can be called directly with the class name without creation of the object of a class.
<br/> * To make the methods more flexible we pass an array as a parameter in the methods and make methods to return the target result.
<br/> * The class includes the following methods: findMax, findMin, findNumber, findSum, findAverage, findString
<br/> * @author Jeff
<br/> *
<br/> */
<br/>public class AlgorithmUtilities {
<br/>	/**
<br/>	 * Find MAX in array of integers
<br/>	 * @param numbers array of integers
<br/>	 * @return max
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 *
<br/>	 */
<br/>	public static int findMax(int[] numbers) {
<br/>		// init the target
<br/>		int max = numbers[0];
<br/>		// arrange the loop
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// work with the next element
<br/>			if(numbers[i] > max) { // if the next element is more than current maximum
<br/>				max = numbers[i]; // replace max
<br/>			}
<br/>		}
<br/>		// return the result
<br/>		return max;
<br/>	}  
<br/>	/**
<br/>	 * Find MIN in array of integers
<br/>	 * @param numbers array of integers
<br/>	 * @return min
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 *
<br/>	 */
<br/>	public static int findMin(int[] numbers) {
<br/>		// init the target
<br/>		int min = numbers[0];
<br/>		// arrange the loop
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// work with the next element
<br/>			if(numbers[i] < min) { // if the next element is less than current minimum
<br/>				min = numbers[i]; // replace max
<br/>			}
<br/>		}
<br/>		// return the result
<br/>		return min;
<br/>	}  
<br/>	/**
<br/>	 * Find out whether a specific number, for example, "5" 
<br/>	 * is present in an integer array, numbers. 
<br/>	Input:   int numbers[], for example, {10,90,2,5,50}
<br/>	Input:   int findMeNumber, for example, 5
<br/>	Output: the index of the found number or -1 if not found
<br/>    
<br/>	 * @param numbers an integer array
<br/>	 * @param findMeNumber
<br/>	 * @return the index of the findMeNumber or -1 if not found
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 * 
<br/>	 */
<br/>	public static int findNumber(int[] numbers, int findMeNumber) {
<br/>		// arrange the FOR loop
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// look for findMeNumber
<br/>			if(numbers[i] == findMeNumber) {
<br/>				return i; // index of the number in the array
<br/>			}
<br/>		}
<br/>		return -1; // if we finished the loop without finding the number, we return -1
<br/>	}
<br/>	/**
<br/>	 * Calculate and return the sum of all numbers 
<br/>	 * from an integer array. 
<br/>	 * @param numbers array of int
<br/>	 * @return sum
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 *
<br/>	 */
<br/>	public static int findSum(int[] numbers) {
<br/>		// initiate the target
<br/>		int sum = 0;
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// add next number
<br/>			sum = sum + numbers[i];
<br/>		}
<br/>		return sum;
<br/>	}
<br/>	/**
<br/>	 * Calculate and return the average of all numbers 
<br/>	 * from an integer array. 
<br/>	 * Return <b>double</b> type as the result to be more precise while calculating average
<br/>	 * @param numbers array of int
<br/>	 * @return average 
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 *
<br/>	 */
<br/>	public static double findAverage(int[] numbers) {
<br/>		// initiate the target
<br/>		double sum = 0; // make it double for precision
<br/>		for(int i=0; i < numbers.length; i++) {
<br/>			// add next number
<br/>			sum = sum + numbers[i];
<br/>		}
<br/>		double average = sum/numbers.length;
<br/>		return average;
<br/>	}
<br/>	/**
<br/>	 * Find a specific string in an array of strings
<br/>	 * @param names array of strings
<br/>	 * @param findMeName specific string to find
<br/>	 * @return the index of the found string in array or -1 if not found 
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 *
<br/>	 */
<br/>	public static int findString(String[] names, String findMeName) {
<br/>		// arrange the FOR loop
<br/>		for(int i=0; i < names.length; i++) {
<br/>			// look for findMeName
<br/>			if(names[i].equals(findMeName) ) { // note that instead of <b>==</b> we use the <b>equals</b> method to compare strings
<br/>				return i; // index of the string in the array
<br/>			}
<br/>		}
<br/>		return -1; // if we finished the loop without finding the number, we return -1
<br/>	}
<br/>	/**
<br/>	 * The main method will test all the methods above by providing input parameters and displaying the results
<br/>	 * In the future, JUnit utilities can be used to automatically provide such testing
<br/>	 * @author Jeff.Zhuk@JavaSchool.com
<br/>	 */
<br/>	public static void main(String[] args) {
<br/>		int max = findMax(new int[] { 1,3,6,4,8});
<br/>		System.out.println("max="+max);
<br/>		int min = findMin(new int[] { 1,3,6,4,8});
<br/>		System.out.println("min="+min);
<br/>		int indexOfNumber = findNumber(new int[] { 1,3,6,4,8}, 4);
<br/>		System.out.println("indexOfNumber="+indexOfNumber);
<br/>		int sum = findSum(new int[] { 1,3,6,4,8});
<br/>		System.out.println("sum="+sum);
<br/>		double average = findAverage(new int[] { 1,3,6,4,8});
<br/>		System.out.println("average="+average);
<br/>		int indexOfName = findString(new String[] { "Julie","Jeff","Ben","Eli"}, "Ben");
<br/>		System.out.println("indexOfNumber="+indexOfName);		
<br/>	}
<br/>}
<br/>


There is more than one way to arrange a loop in Java.
Besides the for loop there is also while and do while loops.
The while statement continually executes a block of statements while a condition is true.

while (condition) {
// statements
}

Example:

int i = 5;
while (i > 0) {
System.out.println(i);
i--; // same as: i = i - 1;
}

The while loop above will display the lines with i ranging from 5 till 1.
Note, that we did initialization (i=5) in the beginning and we did the change (i--) at the end.
In the for loop all three parts together with the condition would be done in one line:

for(int i=5; i > 0; i--) {
System.out.println(i);
}
Another way of expressing the same thing would be:
do {
// statements
} while (condition);

Example:
int i=5;
do {
System.out.println(i--);
} while (i > 0);

Yes, it does exactly the same thing.
A conclusion: just use the for loop, which is more expressive than the others.

Assignments:
Answer the QnA and create at least 2 more QnA related to the subject.

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy