You are on page 1of 5

Datastruc

Computer Engineering Department

Lab 1: Arrays

Objectives:

1. To introduce the array data structure.


2. To understand how to use array of objects.

Background:

It is a linear data structure and it's mainly used to store similar data. An array is a
particular method of storing elements of indexed data. Elements of data are stored
sequentially in blocks within the array. Each element is referenced by an index, or
subscripts.
The index is usually a number used to address an element in the array. For example, if
you were storing information about each day in August, you would create an array
with an index capable of addressing 31 values -- one for each day of the month.
Indexing rules are language dependent, however most languages use either 0 or 1 as
the first element of an array.

The notebook (array) contains 12 pages (elements)

Lab Example:

1. Simulate the execution of the following method with each of the following arrays
passed as its parameter, and write the value it would return:

public static int mystery1(int[] list) {


int x = 0;
for (int i = 1; i < list.length; i++) {
int y = list[i] - list[0]; if (y >
x) { x = y;
}
} return
x;
}
Array Value returned
{5} ______________
{3, 12} ______________
{4, 2, 10, 8} ______________
{1, 9, 3, 5, 7} ______________
{8, 2, 10, 4, 10, 9} ______________
Datastruc

Solution:
Array Value returned
{5} 0
{3, 12} 9
{4, 2, 10, 8} 6
{1, 9, 3, 5, 7} 8
{8, 2, 10, 4, 10, 9} 2

2. Write a static method named findMin that returns the minimum value in an array of
integers. For example, if a variable named list refers to an array containing the values
{16, 12, 25, 44}, the call of findMin(list) should return 12 (the smallest value in the
list). You may assume that the array has at least one element.
Test your code with the following class:

public class TestFindMin { public


static void main(String[] args) { int[]
a1 = {16, 12, 25, 44}; int[] a2 = {587,
23, 8975, 19}; int[] a3 = {42};
System.out.println(findMin(a1)); // 12
System.out.println(findMin(a2)); // 19
System.out.println(findMin(a3)); // 42
}
// your code goes here
}

Solution:

public static int findMin(int[] list) {


int best = list[0];
for (int i = 1; i < list.length; i++) {
if (list[i] < best) { best = list[i];
} }
return best;
}

Lab Exercise:

Implement the following classes:

1. class Department that includes three instance variables:


String name;
Course crs[];
final int noOfCourses = 50;

Your class should have the following:


Datastruc

a constructor that initializes the department name and creates the array.
Datastruc

set and get methods for the name instance variable.


boolean exist(int) that checks whether the course object with id passed as
parameter exists in the array or not.
void addCourse(int, String) creates and adds the course if the course with id
passed as parameter does not exist in the array.
void deleteCourse(int) to delete a course if it exists in the array. void
printCourses() that prints all the courses in the array.

2. class Course that includes two instance variables:


int id; // the course ID
String name; // the course name

Your class should have the following:

A constructor that initializes the two instance variables id and name.


set and get methods for each instance variable.

3.Write a test application named Lab1Test. In the main method, do the following:
Input the department name and create a department object.
Input the number of courses in the department.
Input the id and name of each course and add it to the department
(Hint: Use a loop).
Print the courses.
Input the id of a course to be deleted. Delete the course.
Print the courses.

Sample Output

Enter the department name: Computer Engineering


How many courses are in Computer Engineering: 3

Enter course 1 ID: 207


Enter course 1 Name: Data Structures

Enter course 2 ID: 201


Enter course 2 Name: Java Programming

Enter course 3 ID: 200


Enter course 3 Name: C++ Programming
Datastruc

Enter a course ID to be deleted: 200


1 - 207 Data Structures
2 - 201 Java Programming

Common System/Syntax Errors:

This section includes common system/syntax errors that may occure during the lab.

You might also like