🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #54

Real-Life Examples


Real-Life Example

To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;

// Get the length of the array
int length = ages.length;

// Loop through the elements of the array
for (int age : ages) {
  sum += age;
}

// Calculate the average by dividing the sum by the length
avg = sum / length;

// Print the average
System.out.println("The average age is: " + avg);

And in this example, we create a program that finds the lowest age among different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

// Get the length of the array
int length = ages.length;

// Create a 'lowest age' variable and assign the first array element of ages to it
int lowestAge = ages[0];

// Loop through the elements of the ages array to find the lowest age
for (int age : ages) {
  // Check if the current age is smaller than the current 'lowest age'
  if (lowestAge > age) {
    // If the smaller age is found, update 'lowest age' with that element
    lowestAge = age;
  }
}

// Output the value of the lowest age
System.out.println("The lowest age in the array is: " + lowestAge);

Create a program with a list of numbers where you want to skip negative values, but stop completely if you find a zero:

Example

int[] numbers = {3, -1, 7, 0, 9};

for (int n : numbers) {
  if (n < 0) {
    continue; // skip negative numbers
  }
  if (n == 0) {
    break; // stop loop when zero is found
  }
  System.out.println(n);
}

Create a program that keeps track of the highest and lowest values in an array:

Example

int[] numbers = {45, 12, 98, 33, 27};

int max = numbers[0];
int min = numbers[0];

for (int n : numbers) {
  if (n > max) {
    max = n;
  }
  if (n < min) {
    min = n;
  }
}

System.out.println("Max: " + max);
System.out.println("Min: " + min);

Create a program that prints the seat numbers in a theater row, showing both the seat number (the index) and who is sitting there (the value):

Example

String[] seats = {"Jenny", "Liam", "Angie", "Bo"};

for (int i = 0; i < seats.length; i++) {
  System.out.println("Seat number " + i + " is taken by " + seats[i]);
}

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Real-Life Examples – FAQs

Quick answers about learning Real-Life Examples in Java.

This free note from CodingNow 2.0 explains Real-Life Examples in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Real-Life Examples, is 100% free with no signup required.
With focused practice, most students grasp Real-Life Examples in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now