Starting from:

$25

CSE110 - Principles of Programming with Java  - Lab 8  - Solved

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. 



//              mates for help and/or clarification.  When you see //              //--> that is where you need to add code.


//import Scanner class import java.util.Scanner;

//declare the class Lab6 public class Lab8 {

    //declare the main method

    public static void main(String[] args)     {

// Define scan object of the type Scanner class

      //-->  

  

  

   //define an int variable <size>

      //—>

//Declare an integer array

int[] int_arr; 

          //Assign it memory location. Specify the dimension of the array(Let’s take 5)

int_arr = new int[5]; 

 

//Using a for loop which runs till <size>, read values from the user and store it in the integer array.

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

                    // Read the value the user enters

//—>

//Assign it to the ith element of the array. //—>

      }

//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 

More products