0% found this document useful (0 votes)
3 views7 pages

Java 3

The document outlines an experiment demonstrating the implementation and practical applications of arrays in Java. It includes code snippets for creating 2D arrays to store stock data, counting element frequencies, managing conference room bookings, and illustrating multi-dimensional arrays in real-life scenarios. Additionally, it provides a menu-driven program for booking and checking room availability.

Uploaded by

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

Java 3

The document outlines an experiment demonstrating the implementation and practical applications of arrays in Java. It includes code snippets for creating 2D arrays to store stock data, counting element frequencies, managing conference room bookings, and illustrating multi-dimensional arrays in real-life scenarios. Additionally, it provides a menu-driven program for booking and checking room availability.

Uploaded by

routsatyaswarup0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT-2

(Demonstra ng Array and its applica ons)


Aim of the Experiment-To demonstrate the implementa on and prac cal applica on of arrays in
Java.

[Link] an 2D array to store the me and value of different stocks with their respec ve me. Print
the values at the respec ve med intervals.

CODE:
public class q1 {

public sta c void main(String[] args) {

String[][] stockData = {

{"09:00", "AAPL", "150"},

{"10:00", "GOOGL", "2800"},

{"11:00", "MSFT", "300"},

{"12:00", "AAPL", "152"},

{"13:00", "GOOGL", "2820"},

{"14:00", "MSFT", "305"}

};

[Link]("Time\tStock\tValue");

for (String[] record : stockData) {

[Link](record[0] + "\t" + record[1] + "\t" + record[2]);

CODE:

2. Count the frequency of each element in an array and print the count.

CODE:

import java.u [Link];

public class q2 {

public sta c void main(String[] args) {

18
EXPERIMENT-2
(Demonstra ng Array and its applica ons)
String[] elements = {"AAPL", "GOOGL", "MSFT", "AAPL", "GOOGL", "MSFT", "AAPL"};

HashMap<String, Integer> frequencyMap = new HashMap<>();

for (String element : elements) {

[Link](element, [Link](element, 0) + 1);

[Link]("Element Frequency:");

for (String element : [Link]()) {

[Link](element + ": " + [Link](element));

OUTPUT:

3. Create an array to manage booking of a conference room. To book the conference room provide
details like - date, start me, end me and your name. To check for the availability of the same
room, provide the same details ,check and provide the appropriate message.

CODE:
public class q3{

sta c String[][] bookings = new String[5][4];

sta c int count = 0;

public sta c void main(String[] args) {

bookRoom("10-02-2026", "10", "12", "Satya");

bookRoom("10-02-2026", "11", "13", "Ravi");

checkAvailability("10-02-2026", "12", "13");

sta c void bookRoom(String date, String start, String end, String name) {

for (int i = 0; i < count; i++) {

if (bookings[i][0].equals(date)) {

int s = [Link](start);

19
EXPERIMENT-2
(Demonstra ng Array and its applica ons)
int e = [Link](end);

int bs = [Link](bookings[i][1]);

int be = [Link](bookings[i][2]);

if (s < be && e > bs) {

[Link]("Room already booked for this me!");

return;

bookings[count][0] = date;

bookings[count][1] = start;

bookings[count][2] = end;

bookings[count][3] = name;

count++;

[Link]("Room booked successfully for " + name);

sta c void checkAvailability(String date, String start, String end) {

for (int i = 0; i < count; i++) {

if (bookings[i][0].equals(date)) {

int s = [Link](start);

int e = [Link](end);

int bs = [Link](bookings[i][1]);

int be = [Link](bookings[i][2]);

if (s < be && e > bs) {

[Link]("Room NOT available");

return;

[Link]("Room available");

20
EXPERIMENT-2
(Demonstra ng Array and its applica ons)
OUTPUT:

4. Illustrate the use of 3 or 4 dimensional arrays in real life scenarios, using a program.

CODE:

public class q4 {

public sta c void main(String[] args) {

int[][][] hospital = {

{{2, 3}, {1, 4}},

{{3, 2}, {2, 1}}

};

for (int floor = 0; floor < [Link]; floor++) {

for (int room = 0; room < hospital[floor].length; room++) {

for (int pa ent = 0; pa ent < hospital[floor][room].length; pa ent++) {

[Link]("Floor " + floor +

" Room " + room +

" Count " + hospital[floor][room][pa ent]);

OUTPUT:

21
EXPERIMENT-2
(Demonstra ng Array and its applica ons)
5. Create an array to manage the booking. Create a menu to book and availability for the same room.

CODE:

import java.u [Link];

public class q5 {

sta c String[][] bookings = new String[10][4];

sta c int count = 0;

public sta c void main(String[] args) {

Scanner sc = new Scanner([Link]);

while (true) {

[Link]("\n1. Book Room");

[Link]("2. Check Availability");

[Link]("3. Exit");

[Link]("Enter choice: ");

int choice = [Link]();

if (choice == 1) {

[Link]("Date: ");

String date = [Link]();

[Link]("Start me: ");

String start = [Link]();

[Link]("End me: ");

String end = [Link]();

[Link]("Name: ");

String name = [Link]();

bookRoom(date, start, end, name);

} else if (choice == 2) {

[Link]("Date: ");

String date = [Link]();

[Link]("Start me: ");

String start = [Link]();

[Link]("End me: ");

String end = [Link]();

checkAvailability(date, start, end);

} else {

break;

22
EXPERIMENT-2
(Demonstra ng Array and its applica ons)
}

[Link]();

sta c void bookRoom(String date, String start, String end, String name) {

bookings[count][0] = date;

bookings[count][1] = start;

bookings[count][2] = end;

bookings[count][3] = name;

count++;

[Link]("Room booked successfully!");

sta c void checkAvailability(String date, String start, String end) {

for (int i = 0; i < count; i++) {

if (bookings[i][0].equals(date)) {

[Link]("Room already booked!");

return;

[Link]("Room available!");

OUTPUT:

NAME:-Satya Swarup Rout

GROUP:-4

REGD. NO:-2401020493

BRANCH:- (AI & DS)

23
EXPERIMENT-2
(Demonstra ng Array and its applica ons)

24

You might also like