[SOLVED] CSE110 -Principles of Programming with Java  - Lab 8

30.00 $

Category:

Description

5/5 - (1 vote)

This lab is for practicing 1-D arrays.

Use the following Coding Guidelines:

  • When declaring a variable, you usually want to initialize it.
  • Use white space to make your program more readable.
  • Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs.

Getting Started

Create a class called Lab8. Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab8.java.

Hints

Please replace //–> with the correct program to finish the task according to the corresponding comment.

Please replace ??? with the correct program to enable the program to run as required.

//TASK 1

//Using a for loop which runs till <size>, print all the values of the array and find the sum of all elements of the array.

sum = 0;

for (???; ??? ; ???) {

// Print all the values of the array.

//—>

//Add the element to sum

//—>

}

//Print the value of sum

//—>

//TASK 2

//Using the array that we’ve created, we’ll rotate the elements in the arrays.

//Given an array, after computation the array will be with the elements “rotated left” so {1, 2, 3} yields {2, 3, 1}.

//Store the last element: int last = int_arr[int_arr.length – 1];

//Store the first element: int first = int_arr[0];

 

for(int i =0; i< int_arr.length -1; i++)

{

//Shift the elements one position upward.

}

 

//Assign the last and first variables to their positions.

//Display the array again using a for-loop

for (???; ??? ; ???) {

// Print all the values of the array.

//—>

}

//close scanner object

}

}

SAMPLE OUTPUT:

Please enter value for index 0:

10

Please enter value for index 1:

20

Please enter value for index 2:

33

Please enter value for index 3:

40

Please enter value for index 4:

50

Value at index 0: 10

Value at index 1: 20

Value at index 2: 33

Value at index 3: 40

Value at index 4: 50

The sum of all the elements of the array: 153 Array after left rotation:

Value at index 0: 20

Value at index 1: 33

Value at index 2: 40

Value at index 3: 50

Value at index 4: 10