Description
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. Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*————————————————————————–// AUTHOR: (Put your name here) // FILENAME: Lab6.java // SPECIFICATION: This program is for practicing arrays. // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments. Ask your TA or your class// mates for help and/or clarification. When you see // //–> that is where you need to add code. // LAB LETTER: (Put your lab letter) //————————————————————————-*/ 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. /*————————————————————————–// AUTHOR: (Put your name here) // FILENAME: Lab8.java // SPECIFICATION: This program is for practicing arrays. // // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments. Ask your TA or your class// mates for help and/or clarification. When you see // //–> that is where you need to add code. // LAB LETTER: (Put your lab letter) //————————————————————————-*/ //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
//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
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