0% found this document useful (0 votes)
89 views4 pages

Medina - DATASTRUC - LAB EXERCISE 1 - ARRAYS

This document provides an overview and learning outcomes for a laboratory exercise on arrays in Java programming. It includes 4 sample programs to demonstrate: 1) declaring and initializing a string array, 2) taking user input to populate an array, 3) sorting an integer array, and 4) initializing a 2D integer array. The expected output is shown for each program.

Uploaded by

Glyn Medina
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)
89 views4 pages

Medina - DATASTRUC - LAB EXERCISE 1 - ARRAYS

This document provides an overview and learning outcomes for a laboratory exercise on arrays in Java programming. It includes 4 sample programs to demonstrate: 1) declaring and initializing a string array, 2) taking user input to populate an array, 3) sorting an integer array, and 4) initializing a 2D integer array. The expected output is shown for each program.

Uploaded by

Glyn Medina
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
You are on page 1/ 4

DATASTRUC: LABORATORY EXERCISE 1

ARRAYS

WEEK 4 DATE SEPTEMBER 25, 2021

SECTION II-ACSAD SCORE

OVERVIEW

Array is one of the data structures used in any programming language. It is an organize
storage of data or objects each having the same name and each having the same type. As a
review, sample programs are given to the students for them to recall the purpose of array,
how to declare and initialize an array, and maximize the use of array as data structure.

LEARNING OUTCOMES

1. Explain the concept and purpose of array in programming.


2. Use array as one of the data structures in Java programming.

MATERIAL/S

Java compiler (JCreator or any Java compiler available online)


Microsoft work or yellow paper/ bond paper

ACTIVITIES

A. Type the following programs in JCreator or any Java compiler available in your
desktop or online:
1. Declaring and initializing Array:
public class dispArray {
public static void main(String[]args) {
String dog[]= new String[5];
dog [0]= “Rocky”;
dog [1]= “Whitey”;
dog [2]= “Blackie”;
dog [3]= “Brownie”;
dog [4]= “Pappie”;
for (int a=0; a<5; a++) {
System.out.println(dog[a]);
}
}
}

What is the output of the program above?


Rocky
Whitey
Blackie
Brownie
Pappie

2. User input saved in Array:

import java.io.*; public class


UserInputArray {
public static InputStreamReader reader= new InputStreamReader (System.in);
public static BufferedReader in= new BufferedReader (reader);

public static void main(String[]args) {


String dog[]= new String[5];

for (int c=0; c<5; c++) {


System.out.print("Enter your dog's name: ");
dog[c]= in.readLine();
}
for (int d=0; d<5; d++) {
System.out.print(dog[d] + " ");
}
}
}
What is the output of the program above?
Enter your dog’s name: Yuno
Yuno1 Yuno2 Yuno3 Yuno4 Yuno5

3. Sorting Array: import


java.io.*; public class
Sorting {
public static InputStreamReader reader= new InputStreamReader
(System.in); public static BufferedReader in= new
BufferedReader (reader); public static void main
(String[]args) throws Exception {

int Num[]= new int[5];


for (int a=0; a<Num.length; a++) {
System.out.print("Enter five numbers: ");
Num[a]= Integer.parseInt(in.readLine());
}

System.out.println("Array List: ");

for (int b=0; b<5; b++) {


System.out.print(Num[b] + " ");
}

int temp=0;
System.out.println();
System.out.println("Sorted Array");

for (int d=0; d<Num.length; d++ ) {

Page 2 of 3

for (int c=0; c<Num.length-1; c++) {


if (Num[c]>Num[c+1]) {
temp= Num[c];
Num[c]= Num[c+1];
Num[c+1]= temp;
}
}
}

for (int e=0; e<5; e++) {


System.out.print(Num[e] + " ");
}
}
}
What is the output of the program above?
Enter five numbers: 2
Enter five numbers: 8
Enter five numbers: 6
Enter five numbers: 10
Enter five numbers: 4

Array List:
2 8 6 10 4
Sorted Array:
2 4 6 8 10

4. Two Dimensional Array

public class sample{


public static void main(String[ ] args) {
int[ ][ ] rowcol = new int[5][5];
for (int x=1; x<rowcol.length;x++) {
for (int j=1; j<rowcol[x].length; j++) {
rowcol[x][j] = x%j;
}
}
System.out.println("\nThe result is: ");
for (int x=1; x<rowcol.length; x++) {
for (int j=1; j<rowcol[x].length; j++) {
System.out.print("\t" + rowcol[x][j]);
}
System.out.print("\n");
}
}
}

What is the output of the program above?

The result is:


0 1 1 1
0 0 2 2
0 1 0 3
0 0 1 0

Page 3 of 3

You might also like