Module 1 Laboratory Components
Program 1: Write Java Program that demonstrate assigning values to different primitive
datatypes and printing the variable values on the console.
There is total 8 primitive data types in Java i.e. boolean, char, byte, short, int, long, float and
double.
1. Integers: includes byte, short, int, and long.
2. Floating-point numbers: This group includes float and double, which represent
numbers with fractional precision.
3. Characters: This group includes char, which represents symbols in a character set,
like letters and numbers.
4. Boolean: This group includes boolean, which is a special type for representing
true/false values.
public class RocketLaunchMonitoring
{
public static void main(String[] args)
{
// Primitive Datatypes used in Rocket System
byte rocketID = 7; // Rocket identification number
short temperature = 320; // Engine temperature in Celsius
int altitude = 15000; // Altitude in meters
long distanceTravelled = 450000L; // Distance in meters
float fuelLevel = 65.5f; // Fuel percentage
double velocity = 27500.75; // Velocity in km/h
char missionCode = 'M'; // Mission identifier
boolean launchStatus = true; // Launch system status
// Printing Rocket Data
[Link]("Rocket ID: " + rocketID);
[Link]("Engine Temperature: " + temperature + " C");
[Link]("Current Altitude: " + altitude + " meters");
[Link]("Distance Travelled: " + distanceTravelled + " meters");
[Link]("Fuel Level: " + fuelLevel + " %");
[Link]("Velocity: " + velocity + " km/h");
[Link]("Mission Code: " + missionCode);
[Link]("Launch Status: " + launchStatus);
}
}
Output:
Rocket ID: 7
Engine Temperature: 320 C
Current Altitude: 15000 meters
Distance Travelled: 450000 meters
Fuel Level: 65.5 %
Velocity: 27500.75 km/h
Mission Code: M
Launch Status: true
Program 2: Write Java Program that use different types of operators and displays the results
of these operations on the console.
1. Arithmetic Operators
2. Relational Operators
3. Boolean Logical Operators
4. The Assignment Operator
5. The Ternary Operator.
6. Operator Precedence.
7. Using Parentheses.
public class OperatorsExample
{
public static void main(String[] args)
{
int a = 15;
int b = 5;
// Arithmetic Operators
[Link]("Arithmetic Operators:");
[Link]("Addition (a + b): " + (a + b));
[Link]("Subtraction (a - b): " + (a - b));
[Link]("Multiplication (a * b): " + (a * b));
[Link]("Division (a / b): " + (a / b));
[Link]("Modulus (a % b): " + (a % b));
// Relational Operators
[Link]("\n Relational Operators:");
[Link]("a > b : " + (a > b));
[Link]("a < b : " + (a < b));
[Link]("a == b : " + (a == b));
[Link]("a != b : " + (a != b));
// Logical Operators
boolean x = true;
boolean y = false;
[Link]("\n Logical Operators:");
[Link]("x && y : " + (x && y));
[Link]("x || y : " + (x || y));
[Link]("!x : " + (!x));
// Assignment Operators
int c = 10;
c += 5;
[Link]("\n Assignment Operator (c += 5): " + c);
// Unary Operators
int d = 5;
[Link]("\n Unary Operators:");
[Link]("++d : " + (++d));
[Link]("d-- : " + (d--));
[Link]("Final value of d : " + d);
}
}
Output:
Arithmetic Operators:
Addition (a + b): 20
Subtraction (a - b): 10
Multiplication (a * b): 75
Division (a / b): 3
Modulus (a % b): 0
Relational Operators:
a > b : true
a < b : false
a == b : false
a != b : true
Logical Operators:
x && y : false
x || y : true
!x : false
Assignment Operator:
(c += 5): 15
Unary Operators:
++d : 6
d-- : 6
Final value of d : 5
Program 3: Write Java Program to demonstrate various control structures.
1. Decision-Making Statements or Conditional Statements: if, if-else, if-else
if ladder, switch.
2. Iteration or Looping Statements: for, while, do-while
3. Jump Statements: break, continue, return.
1. Conditional Statements Example: E-commerce Order Decision
public class OrderDecisionSystem
{
public static void main(String[] args)
{
double orderAmount = 3500;
boolean primeUser = true;
// if statement
if(orderAmount > 3000)
{
[Link]("Order eligible for special discount");
}
// if-else
if(primeUser)
{
[Link]("Free Delivery for Prime User");
}
else
{
[Link]("Delivery charges applied");
}
// if-else-if ladder
if(orderAmount > 5000)
{
[Link]("20% Discount Applied");
}
else if(orderAmount > 3000)
{
[Link]("10% Discount Applied");
}
else
{
[Link]("No Discount");
}
// switch statement
int paymentMethod = 2;
switch(paymentMethod)
{
case 1:
[Link]("Payment by Credit Card");
break;
case 2:
[Link]("Payment by UPI");
break;
case 3:
[Link]("Cash on Delivery");
break;
default:
[Link]("Invalid Payment Method");
}
}
}
Output:
Order eligible for special discount
Free Delivery for Prime User
10% Discount Applied
Payment by UPI
2. Loop Example: Server Monitoring System
Real usage: Cloud platforms from companies like Google or Amazon monitor thousands of
servers in loops.
public class ServerMonitoring
{
public static void main(String[] args)
{
int[] cpuUsage = {45, 70, 85, 60, 90};
// for loop
for(int i = 0; i < [Link]; i++)
{
if(cpuUsage[i] > 80)
{
[Link]("High CPU usage detected: " + cpuUsage[i]);
}
}
// while loop
int i = 0;
while(i < [Link])
{
[Link]("Server CPU: " + cpuUsage[i]);
i++;
}
// do-while loop
int j = 0;
do
{
[Link]("Monitoring server: " + j);
j++;
}
while(j < 3);
}
}
Output:
High CPU usage detected: 85
High CPU usage detected: 90
Server CPU: 45
Server CPU: 70
Server CPU: 85
Server CPU: 60
Server CPU: 90
Monitoring server: 0
Monitoring server: 1
Monitoring server: 2
3. Jump Statements Example: Fraud Detection
public class FraudDetection
{
public static void main(String[] args)
{
int[] transactions = {5000, 20000, 75000, 12000, 90000};
for(int i = 0; i < [Link]; i++)
{
if(transactions[i] < 10000)
{
continue; // skip small transactions
}
if(transactions[i] > 80000)
{
[Link]("Critical Fraud Alert: " + transactions[i]);
break; // stop checking further
}
[Link]("Review Transaction: " + transactions[i]);
}
}
}
Output:
Review Transaction: 20000
Review Transaction: 75000
Review Transaction: 12000
Critical Fraud Alert: 90000
Program 4: Write Java programs that demonstrate various operations on arrays including
array initialization, traversing the array and manipulating array elements.
• In Java, array is an object that stores a fixed number of elements of the same data type.
• It stores multiple elements under a single variable name.
Syntax: int[] marks = {88, 74, 91, 82, 68, 94};
Features of Java Arrays
1. Index Based: Arrays elements are accessed using indices starting from 0.
2. Fixed Size: After creating an array, the size cannot be changed.
3. Stores Same Type of Elements: Arrays always stored homogeneous elements i.e. same
type of data.
4. Contiguous Memory: Array elements are stored in continuous memory locations,
which allows fast access.
Advantages of Arrays
1. Efficient way to store multiple values: Arrays allow storing multiple values of the same
type in a single variable.
2. Versatile: Arrays can store different types of data like numbers, characters or even
objects.
3. Easy to access elements using loop: We can easily access all elements using loops like
for or for-each.
4. Supports random access: We can directly access any element using its index.
Disadvantages of Arrays
1. Fixed size - Less flexibility: Once arrays are created, the size of an array cannot be
changed.
2. Cannot store elements of different types: All elements must be of the same data type.
3. Insertion or deletion in the middle is costly: To insert or delete an element, other
elements may need to be shifted.
4. Memory Wastage: If we create an array larger than needed, the extra space is wasted
because arrays have a fixed size.
5. No Built-in Methods: Arrays in Java do not have built-in methods for operations like
adding, removing, or searching elements.
Various Operations on Arrays
public class ArrayOperations
public static void main(String[] args)
int[] numbers = {10, 25, 30, 45, 50};
// 1. Traversing the array
[Link]("Array Elements:");
for(int i = 0; i < [Link]; i++)
[Link](numbers[i] + " ");
// 2. Sum of elements
int sum = 0;
for(int i = 0; i < [Link]; i++)
sum = sum + numbers[i];
[Link]("\nSum = " + sum);
// 3. Average
double average = (double)sum / [Link];
[Link]("Average = " + average);
// 4. Largest element
int max = numbers[0];
for(int i = 1; i < [Link]; i++)
if(numbers[i] > max)
max = numbers[i];
}
[Link]("Largest Element = " + max);
// 5. Smallest element
int min = numbers[0];
for(int i = 1; i < [Link]; i++)
if(numbers[i] < min)
min = numbers[i];
[Link]("Smallest Element = " + min);
// 6. Searching an element
int key = 30;
boolean found = false;
for(int i = 0; i < [Link]; i++)
if(numbers[i] == key)
found = true;
[Link]("Element " + key + " found at index " + i);
break;
if(!found)
{
[Link]("Element not found");
// 7. Updating element
numbers[2] = 100;
[Link]("Updated Element at index 2 = " + numbers[2]);
Output:
Array Elements: 10 25 30 45 50
Sum = 160
Average = 32.0
Largest Element = 50
Smallest Element = 10
Element 30 found at index 2
Updated Element at index 2 = 100