Laboratory Manual No. 1 Arrays - Basics A.C. 2021-2022 Objectives: 1. Describe what array is and how to create an array.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Laboratory Manual No. 1 Arrays - Basics A.C. 2021-2022 Objectives: 1. Describe what array is and how to create an array.

Post by answerhappygod »

Laboratory Manual No. 1 Arrays - Basics A.C. 2021-2022
Objectives: 1. Describe what array is and how to create an array.
2. Identify on how to populate an array. 3. Identify on how to
manipulate an array. Course Learning Outcome: 1. CLO1. Demonstrate
the familiarity with basic data structures and algorithms. 2. CLO2.
Describe basic data structures in computer programs. 3. CLO3.
Implement data structures using Java language. Tools or Equipment
Needed: 1. PC 2. Eclipse 3. Java Theoretical Background: Arrays An
array is a group of variables (called elements or components)
containing values that all have the same type. Arrays are objects,
so they’re considered reference types. The position number of the
element is called the element’s index or subscript. A program
refers to any one of these elements with an array-access expression
that includes the name of the array followed by the index of the
particular element in square brackets ([]). The first element in
every array has index zero and is sometimes called the zeroth
element. Thus, the elements of array c are c[0], c[1], c[2] and so
on. The highest index in array c is 11, which is 1 less than 12—the
number of elements in the array. Array names follow the same
conventions as other variable names. An index must be a nonnegative
integer. A program can use an expression as an index. Figure 1. A
12-Element array Declaring and Creating Arrays Array objects occupy
space in memory. Like other objects, arrays are created with
keyword new. To create an array object, you specify the type of the
array elements and the number of elements as part of an
array-creation expression that uses keyword new. Such an expression
returns a reference that can be stored in an array variable. The
following declaration and array-creation expression create an array
object containing 12 integer elements and store the array’s
reference in array variable c: int[ ] c = new int[12]; or int c[ ]
= new int [12]; Initialization of an array using FOR loop. 1. int[
] ArrayOne = new int [10]; for ( int i = 0; i < 10; i++)
ArrayOne[ i ] = 0; 2. int ArrayOne[ ] = new int [10]; for ( int i =
0; i < ArrayOne.length ; i++) ArrayOne[ i ] = 0; Using an Array
Initializer You can create an array and initialize its elements
with an array initializer—a comma-separated list of expressions
(called an initializer list) enclosed in braces. In this case, the
array length is determined by the number of elements in the
initializer list. For example: int x[ ] = { 5 , 10, 3, 14, 5, 20 }
The enhanced for statement simplifies the code for iterating
through an array. Note, however, that the enhanced for statement
can be used only to obtain array elements—it cannot be used to
modify elements. If your program needs to modify elements, use the
traditional counter-controlled for statement. Example: Practice 1:
Write a program that will read 10 numbers and output the numbers
entered. import java.util.Scanner; public class Practice1 { public
static void main(String[] args) { Scanner input = new Scanner
(System.in); int[] a = new int[10]; for(int i=0; i<10 ; i++) {
System.out.print("Enter a number: "); a=input.nextInt(); }
for(int i=0;i<10;i++) { System.out.print(a + " "); } } }
Practice 2: Write a program that will read 10 numbers using an
array variable. Compute and output the sum. import
java.util.Scanner; public class Practice2 { public static void
main(String[] args) { Scanner input = new Scanner (System.in); int
sum=0; int[] a = new int[10]; for(int i=0; i<10 ; i++) {
System.out.print("Enter a number: "); a=input.nextInt(); }
for(int i=0;i<10;i++) { sum += a; } System.out.print("The sum
is " + sum); } }   Name: ______________¬¬¬¬¬¬¬¬__________ ID:
____________________ CRN: ____________ Laboratory Tasks Write a
java application ArrayOperations that includes the following
methods: 1. AverageArray method that accepts an array as a
parameter and calculate the average value. (2 marks) 2.
DiplayForward method that accepts an array as a parameter and
prints the elements of that array in forward order. (2 marks) 3.
DiplayBackward method that accepts an array as a parameter and
prints the elements of that array in backward order. (2 marks) 4.
MaxArray method that accepts an array as a parameter and return the
largest number. (2 marks) 5. MinArray method that accepts an array
as a parameter and return the smallest number. (2 marks) 6.
CompareArray method that accepts two arrays as parameters and
return true if they are equal or false in not equal. (2 marks) 7.
ReplaceElement method that accepts an array and an integer value as
parameters, replace the element from the array by -99 and return
the updated array. (2 marks) 8. AddElement method that accepts an
array and an integer value as parameters, copy the array to a new
array, insert the element at the end of the new array and return
the new array. (2 marks) 9. Main method that tests all the previous
8 methods. (4 marks) Program code Screenshot including package
explorer, program, console Remarks: • You need to upload: o The Lab
word file; rename as “Yourname and ID_Lab#No.docx”. Rubric: Marks
Task 1 2 Task 2 2 Task 3 2 Task 4 2 Task 5 2 Task 6 2 Task 7 2 Task
8 2 Task 9 4 Total 20
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply