Professional Documents
Culture Documents
Lab Exercise (Java)
Lab Exercise (Java)
com/java-programming/multidimensional-array
https://www.youtube.com/watch?v=rRFjmAtCQMY (important link)
Example 1: Java if Statement
class IfStatement {
public static void main(String[] args) {
if (number > 0) {
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}
}
Number is positive.
if (number > 0) {
System.out.println("Number is positive.");
}
else {
1|P ag e
System.out.println("Number is not positive.");
}
Number is positive.
char ch = 'i';
}
}
2|P ag e
Example 1: Check whether a number is even or odd
using if...else statement
import java.util.Scanner;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
3|P ag e
System.out.print("Enter a number: ");
int num = reader.nextInt();
}
}
int number = 0;
if (number > 0) {
System.out.println("Number is positive.");
}
else if (number < 0) {
System.out.println("Number is negative.");
}
else {
System.out.println("Number is 0.");
}
}
}
4|P ag e
Number is 0.
int week = 4;
String day;
switch (week) {
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
5|P ag e
break;
case 7:
day = "Saturday";
break;
default:
day = "Invalid day";
break;
}
System.out.println(day);
}
}
Wednesday
6|P ag e
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;
switch(operator)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
7|P ag e
Repetitive /Loops
_________________________________________________
• A loop can be used to tell a program to execute statements repeatedly.
• Java has four Loop control structures
– The While Loop
– The Do-while Loop
– For Loop
– For each or enhanced for loop
– Nested Loop
8|P ag e
The do-while Loop
– A do-while loop is the same as a while loop except that it executes the loop body first and then checks
the loop condition.
– The do-while loop is a variation of the while loop.
– Its syntax is:
do
{
Statement(s);
} while(condition);
Example
public class LoopW
{
public static void main(String[] args)
{
int i = 0;
do
{
System.out.println(i);
i++;
} while (i<5);
}
}
9|P ag e
The for Loop
– A for loop has a concise syntax for writing loops.
– In general, the syntax of a for loop is:
10 | P a g e
for-each Loop or enhanced for loop
– Java supports a convenient for loop, known as a for-each loop, which enables you to
traverse the array sequentially without using an index variable.
– The syntax for a for-each loop is:
for (elementType element: arrayRefVar)
{
// Process the element
}
Example
public class includehelp {
public static void main(String[] args)
{
int array[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("Demonstration of for-each loop");
Output
5. Nested Loops
– A loop can be nested inside another loop.
Nested loops consist of an outer loop and one or more inner loops
11 | P a g e
6. Jumping Statements Example using continue
The break and continue keywords provide additional controls in a loop
The continue statement skips the current iteration of a loop (for, while, and do...while loop).
Example
class Test {
public static void main(String[] args) {
1
2
3
4
9
10
12 | P a g e
Example 1: Java break statement
How break statement works?
The break statement terminates the loop immediately, and the control of the program moves to
the next statement following the loop
class Test {
public static void main(String[] args) {
1
2
3
4
13 | P a g e
METHODS IN JAVA
__________________________________________________________
Modularize the tasks of the program or structure the program.
Functions separate the concept (what is done) from the implementation (how it is
done).
Note:-( ) Denotes for creating method
14 | P a g e
nextLine() method in java
Defining a Method:
– A method definition consists of its method name, parameters, return value type, and body.
– The syntax for defining a method is:
15 | P a g e
Example
Sum of two integer number using method
EXERCISE
Maximum of two number using method in java
Method overloading
16 | P a g e
__________________________________________________________
Two or more METHODS can have the same name but different parameters.
Process of using the same name for two or more METHODS.
Used so that a programmer does not have to remember multiple METHOD names.
Array in Java
Array is a collection of similar type of elements that have contiguous memory location.
Java array is an object that contains elements of similar data type. We can store fixed of elements
on a java array.
Array in java is index based, first element of the array is stored at 0 index.
An array is a container that holds data (values) of one single type. For example, you can
create an array that can hold 100 values of int type.
17 | P a g e
One-dimensional arrays.
How to declare an array?
Here's how you can declare an array in Java:
datatype age [ ]
18 | P a g e
int age []= {12, 4, 5, 2, 5};
The length of the array is determined by the number of values provided which is separated by commas. In
our example, the length of age array is 5.
You can easily access and alter array elements by using its numeric index. Let's take an
example.
class ArrayExample {
public static void main(String[] args) {
age[2] = 14;
age[0] = 34;
19 | P a g e
}
}
}
Element at index 0: 34
Element at index 1: 0
Element at index 2: 14
Element at index 3: 0
Element at index 4: 0
Element at index 0: 12
Element at index 1: 4
Element at index 2: 5
Element at index 3: 2
Element at index 4: 5
20 | P a g e
Exercise
class SumAverage {
public static void main(String[] args) {
Sum = 36
Average = 3.6
21 | P a g e
Multidimensional Arrays
In Java, you can declare an array of arrays known as multidimensional array. Here's an
example to declare and initialize multidimensional array.
Here, a is a two-dimensional (2d) array. The array can hold maximum of 12 elements of
type int.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
22 | P a g e
Or int A[2][3] ={1, 2, 3, 10, 20, 30}
Accessing Two-Dimensional Array Elements
(How to read and print elements)
Exercise
What is output the “ff” code fragment?
Exercise
23 | P a g e
Initializing arrays with input values.
__________________________________________________________
Exercise
Write a c++ program that accepts 2*2 array and print the given array Element.
The following loop initializes the array with user input values:
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.println("Enter " + matrix.length + " rows and " + matrix[0].length + "
columns: ");
for(int row = 0; row <matrix.length; row++){
for(int column = 0; column <matrix[0].length; column++){
matrix[row][column] = input.nextInt();
}
}
24 | P a g e
25 | P a g e
String Processing
• The classes String, StringBuilder, and StringBuffer, StringTokenizer are used for
processing strings.
• The String Class
– A string is a sequence of characters.
– In many languages, strings are treated as an array of characters, but in Java
a string is treated as an object.
A String object is immutable: Its content cannot be changed once the string is created.
26 | P a g e
StringTokenizer Class
• The StringTokenizer class allows an application to break a string into tokens.
• When you read a sentence, your mind breaks it into tokens—individual words and
punctuation marks that convey meaning to you.
• Tokens are separated from one another by delimiters, typically white-space characters
such as space, tab, newline and comma.
27 | P a g e
Loop lab Exercise
Write a Java program that takes a number as input and prints its multiplication table up to 10.
Test Data:-
Input a number: 8
import java.util.Scanner;
28 | P a g e
NESTED FOR LOOP LAB EXERCISE
https://www.youtube.com/watch?v=m6gfX-siSdY (Java pattern)
https://www.youtube.com/watch?v=iL_tcbdwL-0 (Triangle pattern)
29 | P a g e