BIG JAVA, LATE OBJECTS LL W/ EPUB
BIG JAVA, LATE OBJECTS LL W/ EPUB
17th Edition
ISBN: 9781119399018
Author: Horstmann
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 6, Problem 1RE

Explanation of Solution

a. Allocate an array of ten integers:

The following declaration statement allocates an array that can hold ten elements of type integers:

//Declare an array of ten integers

int[] values = new int[10];

Explanation:

In the above line,

  • “int” refers to the data type of the array.
  • “values” refers to the name of the array variable.
  • “new” refers to a keyword which allocates a memory space.
  • “int[10]” refers to the size of the array “10”.

The above line is used to allocate an integer array named “values” with size “10”

Explanation of Solution

b. The initial element of the array:

The statement to insert the integer value “17” into the initial element of the array values is as follows:

/*Insert the value 17 into the initial element of the array */

values[0] = 17;

Explanation:

The above line assigns the integer value “17” into the initial index (that is, 0th index) of the “values” array.

Explanation of Solution

c. The last element of the array:

The statement to insert the integer value “29” into the last element of the array values is as follows:

/*Insert the value 29 into the last element of the array */

values[9] = 29;

Explanation:

  • The above line assigns the integer value “29” into the last index (that is, 9th index) of the “values” array.

Explanation of Solution

d. Fill the remaining elements with –1:

The below “for” loop shows how to fill the remaining elements in an array “values” with “­1”:

// for loop to assign -1 to all elements of “values” array

for (int i = 1; i < 9; i++)

{

     // Assign the value

     values[i] = -1;

}

Explanation:

  • At each iteration, the above “for” loop iterates through each element in the “values” array.
  • In the “for” loop, initially “i = 0” and the loop will execute until the value of “i” is less than “9”.
    • Inside the “for” loop, at each iteration, the value “-1” is stored into “values[i]”.
  • At last, the loop will get terminated when it reaches “9 < 9”.

Explanation of Solution

e. Add 1 to each element of the array:

The below “for” loop shows how to add 1 to each element of the array “values” :

/* for loop to compute “values[i] + 1” and store the result into “values[i]” */

for (int i = 0; i < values.length; i++)

{

    //calculate the values

    values[i] = values[i] + 1

}

Explanation:

  • At each iteration, the above “for” loop iterates through each element in the “values” array.
  • In the “for” loop, initially “i = 0” and the loop will execute until the value of “i” is less than “values.length”.
    • Inside the “for” loop, it calculates “value[i] + 1” and stores the result into “values[i]”.
  • At last, the loop will get terminated when it reaches “10 < 10”.

Explanation of Solution

f. Print all elements of the array, one per line:    

The below “for” loop shows how to print all elements of the array “values”, one per line :

/* for loop to print all elements of the array */

for (int value : values)

{

  /*print the value of all elements in the array */

  System.out.println(value);

}

Explanation:

  • At each iteration, the given enhanced “for” loop iterates through each “element” in the “values” array.
  • The print statement “System.out.println” prints the values of all elements in the “values” array, one per line.

Explanation of Solution

g. Print all elements of the array in a single line, separated by commas:

Consider the below “for” loop to print all the elements in the array “values” in a single line, separated by commas except the last element:

// for loop to print the array values

for (int i = 0; i < values.length; i++)

{

     /* checks whether i is less than values.length-1 or not */

        if (i < values.length - 1)

          /print the values with separator

         System.out.print(values[i] + ",");

        else

          /print the values

         System.out.print(values[i]);

}

Explanation:

  • In the “for” loop, initially “i = 0” and the loop will execute until “i < values.length”.
    • Inside the “for” loop, the conditional statement “if (i < values.length-1))” checks whether “i” value is less than the length of the array or not.
      • If the condition is true, then the print statement “System.out.print(values[i] + ",");” prints the values of “values” array separated by commas.
      • The else part will print the last element of the array.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!

Chapter 6 Solutions

BIG JAVA, LATE OBJECTS LL W/ EPUB

Ch. 6.2 - Prob. 11SCCh. 6.3 - Prob. 12SCCh. 6.3 - Prob. 13SCCh. 6.3 - Prob. 14SCCh. 6.3 - Prob. 15SCCh. 6.3 - Prob. 16SCCh. 6.3 - Prob. 17SCCh. 6.3 - Prob. 18SCCh. 6.4 - Prob. 19SCCh. 6.4 - Prob. 20SCCh. 6.4 - Prob. 21SCCh. 6.4 - Prob. 22SCCh. 6.4 - Prob. 23SCCh. 6.5 - Prob. 24SCCh. 6.5 - Prob. 25SCCh. 6.5 - Prob. 26SCCh. 6.5 - Prob. 27SCCh. 6.5 - Prob. 28SCCh. 6.6 - Prob. 29SCCh. 6.6 - Prob. 30SCCh. 6.6 - Prob. 31SCCh. 6.6 - Prob. 32SCCh. 6.6 - Prob. 33SCCh. 6.7 - Prob. 34SCCh. 6.7 - Prob. 35SCCh. 6.7 - Prob. 36SCCh. 6.7 - Prob. 37SCCh. 6.7 - Prob. 38SCCh. 6.8 - Prob. 39SCCh. 6.8 - Prob. 40SCCh. 6.8 - Prob. 41SCCh. 6.8 - Prob. 42SCCh. 6.8 - Prob. 43SCCh. 6.8 - Prob. 44SCCh. 6.8 - Prob. 45SCCh. 6 - Prob. 1RECh. 6 - Prob. 2RECh. 6 - Prob. 3RECh. 6 - Prob. 4RECh. 6 - Prob. 5RECh. 6 - Prob. 6RECh. 6 - Prob. 7RECh. 6 - Prob. 8RECh. 6 - Prob. 9RECh. 6 - Prob. 10RECh. 6 - Prob. 11RECh. 6 - Prob. 12RECh. 6 - Prob. 13RECh. 6 - Prob. 14RECh. 6 - Prob. 15RECh. 6 - Prob. 16RECh. 6 - Prob. 17RECh. 6 - Prob. 18RECh. 6 - Prob. 19RECh. 6 - Prob. 20RECh. 6 - Prob. 21RECh. 6 - Prob. 22RECh. 6 - Prob. 23RECh. 6 - Prob. 24RECh. 6 - Prob. 25RECh. 6 - Prob. 26RECh. 6 - Prob. 27RECh. 6 - Prob. 28RECh. 6 - Prob. 29RECh. 6 - Prob. 30RECh. 6 - Prob. 31RECh. 6 - Prob. 32RECh. 6 - Prob. 33RECh. 6 - Prob. 34RECh. 6 - Prob. 1PECh. 6 - Prob. 3PECh. 6 - Prob. 4PECh. 6 - Prob. 5PECh. 6 - Prob. 6PECh. 6 - Prob. 7PECh. 6 - Prob. 8PECh. 6 - Prob. 9PECh. 6 - Prob. 10PECh. 6 - Prob. 11PECh. 6 - Prob. 12PECh. 6 - Prob. 13PECh. 6 - Prob. 15PECh. 6 - Prob. 16PECh. 6 - Prob. 17PECh. 6 - Prob. 18PECh. 6 - Prob. 19PECh. 6 - Prob. 20PECh. 6 - Prob. 21PECh. 6 - Prob. 23PECh. 6 - Prob. 24PECh. 6 - Prob. 25PECh. 6 - Prob. 26PECh. 6 - Prob. 27PECh. 6 - Prob. 28PECh. 6 - Prob. 29PECh. 6 - Prob. 1PPCh. 6 - Prob. 2PPCh. 6 - Prob. 3PPCh. 6 - Prob. 4PPCh. 6 - Prob. 5PPCh. 6 - Prob. 6PPCh. 6 - Prob. 7PPCh. 6 - Prob. 8PPCh. 6 - Prob. 9PPCh. 6 - Prob. 10PPCh. 6 - Prob. 11PPCh. 6 - Prob. 12PPCh. 6 - Prob. 13PPCh. 6 - Prob. 14PPCh. 6 - Prob. 15PPCh. 6 - Prob. 16PPCh. 6 - Prob. 17PPCh. 6 - Prob. 18PPCh. 6 - Prob. 19PP
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education