0% found this document useful (0 votes)
4 views12 pages

Lab 5 - ARRAYS

The document provides an overview of arrays and strings in Java, including their declaration, initialization, and methods for accessing and manipulating their values. It covers one-dimensional and multi-dimensional arrays, along with code examples for calculating averages and initializing 2D arrays with random numbers. Additionally, it explains string operations and presents a problem scenario for developing a temperature monitoring program using arrays.

Uploaded by

pes OP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Lab 5 - ARRAYS

The document provides an overview of arrays and strings in Java, including their declaration, initialization, and methods for accessing and manipulating their values. It covers one-dimensional and multi-dimensional arrays, along with code examples for calculating averages and initializing 2D arrays with random numbers. Additionally, it explains string operations and presents a problem scenario for developing a temperature monitoring program using arrays.

Uploaded by

pes OP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

OBJECT

ORIENTED PROGRAMMING

Lab 5
Arrays and Strings

By: Abdul Wahid


Faculty of Engineering Science and Technology
Outline
• What is an Array?
• Access the values of arrays using loops
• Multi dimensional arrays.
• Declaration and initialization of arrays.
• What are strings in java
• String methods
• Code Examples
• Problems
What is an Array?
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

Array Declaration
DataType [ ] ArrayName

Array Initialization
DataType [ ] ArrayName = {val1, val2, val3}

Array Initialization
int [ ] num = {2,6,3,9,7,0}
Access the values of array
In Java there are two ways to access the element of an array, lets look at examples below:

int [ ] num = {2,6,3,9,7,0}  Example Array

By referring the index


We need to add reference of the index on which your desired value is placed.
System.out.println(num[2]

By using iterative method


We need start the loop to access all elements in the array..
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]); }
Changing the values of an array
To change the value of a specific element, refer to the index number

int [ ] num = {2,6,3,9,7,0}  Example Array

num[2] = 4;
System.out.println(num[2])
Multi Dimensional array
A multidimensional array is an array of arrays. Multidimensional arrays are useful when you want to store data
as a tabular form like a table with rows and columns.

Creating a Multidimensional Array


int [ ][ ] numbers = { {2,4,6,8,10,12}, {1,3,5,7,9,11} }  Example Array
System.out.println(numbers[1][3])

Access elements of a Multidimensional Array through loop


for (int i = 0; i < myNumbers.length; ++i) {
for (int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}
Array Example 1
Calculate Average of an Array
Write a program using a one-dimensional array to find the average of a set of numbers.

class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
for (int i = 0; i < nums.length; i++) {
result += nums[i];
}
System.out.println("Average is " + result / nums.length);
}
}
Array Example 2
Initialize 2D Array with Random Numbers
Write a program to initialize and print a 5x5 2D array with random numbers.

class RandomArray2D {
public static void main(String[] args) {
double[][] array2d = new double[5][5];
for (int row = 0; row < array2d.length; row++) {
for (int col = 0; col < array2d[row].length; col++) {
array2d[row][col] = Math.round(Math.random());
}}
for (int row = 0; row < array2d.length; row++) {
for (int col = 0; col < array2d[row].length; col++) {
System.out.print(array2d[row][col] + " ");}
System.out.println();
}}}
What are the strings in Java
Strings are used for storing text. A String variable contains a collection of characters
surrounded by double quotes

String text = "Hello World";


String methods
A String in Java is actually an object, which contain methods that can perform certain operations on strings.

String text = "Hello World";

Length of a String
System.out.println("The length of the txt string is: " + text.length());

Uppercase Strings
System.out.println(“String to Upper Case: " + text.toUpperCase());

Lowercase Strings
System.out.println(" String to Lower Case : " + text.toLowerCase());

Finding the index of word


System.out.println(“the index no of world is: " + text.indexOf(“world”));
Problems
Scenario:
You are developing a program to help a weather monitoring system analyze temperature
data for a city. The system collects daily temperature readings for 7 days, and you are
required to generate a summary report based on this data.
Task Description:

Write a Java program that:


●Uses a one-dimensional array to store the temperature readings for 7 days.
●Calculates and displays the average temperature for the week.
●Identifies and prints the highest and lowest temperatures recorded.
●Traverses the array using a loop to print the temperature readings for each day.
Thank you

You might also like