3
QUESTION 1: Improve the understandability of the below given code: import java.util.*; //importing the required packages import java.io.*; class problem3 //creating a class { int[] numArray = new int[10]; //declaring an integer array of size 10 called numArray public static void incrementElements (int[] integerArray) //defining the 'incrementElementst()'method with one integer parameter { int arraylen = integerArray.length; // initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to print the current values of array { System.out.println(integerArray[i]); //printing the element at position or index ‘i’ } for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to increment the current values of array { integerArray[i] = integerArray[i] + 10; //add 10 to each value of array } for (int i=0; i < arraylen; i ++) { System.out.println(integerArray[i]);

Solution for Code Understandability Question 1 and Question 2

Embed Size (px)

Citation preview

Page 1: Solution for Code Understandability Question 1 and Question 2

QUESTION 1: Improve the understandability of the below given code:

import java.util.*; //importing the required packages import java.io.*;

class problem3 //creating a class

{

int[] numArray = new int[10]; //declaring an integer array of size 10 called numArray

public static void incrementElements (int[] integerArray) //defining the 'incrementElementst()'method with one integer parameter

{

int arraylen = integerArray.length; // initializing an integer variable 'arraylen' with the length, i.e. the no of elements in array

for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to print the current values of array

{System.out.println(integerArray[i]); //printing the element at position or index ‘i’

}

for (int i = 0; i < arraylen; i ++) //definition of 'for' loop to increment the current values of array

{

integerArray[i] = integerArray[i] + 10; //add 10 to each value of array

}

for (int i=0; i < arraylen; i ++)

{System.out.println(integerArray[i]);

}

}

EXPLANATION:

When any element of the integer array occurs, then it is printed first and after that it is incremented by 10 and then the resultant array is printed. The code is predefined in the package and we want to execute any application which requires the code. We don’t have to write the whole code as we can directly import the package and execute that application.

Page 2: Solution for Code Understandability Question 1 and Question 2

QUESTION 2: Improve the understandability of the below given code:

class Problem1 //creating a new class{

int[] a; //declaring a new integer variable 'a' of array type to store the numbersint nElems; //declaring an integer variable to hold the value of no. of elements in the array

public ArrayBub(int max) {

a = new int[max]; }

public void insert(int value) //defining the insert method to insert values in the array{

a[nElems] = value; //assigning the value to array at current positionnElems++; //incrementing the position counter

}

public void Sort() //defining the method to sort the array{

int out, in; // declaring two integer variables 'out' & 'in'for(out=nElems-1; out>1; out--) //outer loop

for(in=0; in<out; in++) //inner loopif( a[in] > a[in+1] ) //conditional statement to compare the two values

swap(in, in+1); //swapping the two values in specified order}

public void swap(int one, int two) //defining 'swap' function to perform swapping of elements{

long temp = a[one]; //interchanging the valuesa[one] = a[two]; a[two] = temp;

}

EXPLANATION:

This is a program for Bubble Sort. It is the simplest sorting algorithm. In bubble sort algorithm, the array is traversed from 0 to the length-1 index of the array and compared one element to the next element and swap values in between if the next element is less than the previous element. In other words, bubble sorting algorithm compare two values and put the largest value at largest index. The algorithm follows the same steps and performs multiple passes until the values of the array are sorted. Bubble sort has worst case if the elements are all in reverse order. In worst-case the complexity of bubble sort is O(n2) and in best-case the complexity of bubble sort is O(n).