You are on page 1of 3

Mediterranean Institute of Technology

CS341 - Data Structures and Algorithms

LABORATORY

By
Saadaoui Ikram, Labed Abdeldjalil

September, 2022
Lab 2: Programming with arrays

I Objective:

• Implementing arrays in Java.


• Understanding array operations
• Understanding and implementing the searching algorithms in Java.

II Exercises

Exercise 1:

Create an array that can hold ten integers, and fill each element with a random
value from 1-50. Display those values on the screen, and then ask the user to enter a
target. Search through the array, and if the target is found at index x, print ’target
found at index x’. It is not necessary to display anything if the value was not found.

Exercise 2:

Create a sorted array that can hold ten integers. Find a specific element in the array
using the binary Search.

Exercise 3:

In this exercise create three arrays to store the data of five students, where:

• The first array should contain String elements and should hold the students’
full names.
• The second array should contain double elements and should hold students’
grade average (on a 100-point scale).
• The last array should contain int elements and should hold the students’ id
numbers.

Display the values of all arrays on the screen. Finally, ask the user for an ID number
to look for. Search through the ID array until you find that ID, and then print out
the student data from the other two arrays.
Notes: Create two Scanners one for String input and the other for number (Int and
Double) input.

Exercise 4: (Homework)

A bus company wants to create an online booking system. You have been entrusted
with developing a part of the application that will allow a user to book his seat on
the bus (capacity: 10 seats).

1
• At the beginning the program should display the following options: Please enter
1 for First Class and 2 for Second Class.
• If the user enters 1, your program should book him a seat from 1–5 in the
first-class.
• If the user enters 2, your program should book him a seat from 6-10 in the
second-class.
• Your program should then display a ticket with the user’s seat number together
with the chosen class.
• To represent the bus seating, use a 1D array of type boolean.
• Initialize all the elements of the array to false to indicate that all the seats are
empty.
• As each seat is booked for a user, set the corresponding element of the array
to true to indicate that the seat is no longer available.
• Your program should never give a ticket to a seat that has already been reserved.
When the second class is full, your program should ask the user if it’s acceptable
to be placed in the first-class section (and vice versa). If yes, make the booking.
If no, display the message ”We are sorry, check the next bus.”

Grading guide of the homework (exercise 4)

I will check the correctness of the homework code during the next lab session on
your laptop. Please, make sure that your code is compilable and runnable.

• 75%: Program correctness.


• 25%: Your code. Efficiency. Programming style, comments, formats

You might also like