Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 7.2, Problem 12STQ

Explanation of Solution

Error in method definition “copyArray”:

  • • The given method definition “copyArray” does not return an each element from the given argument array “anArray”.
    • ○ Relatively, it returns a reference to the array it is given.
  • • To produce a duplicate array, user would modify the statement “temp = anArray” to following statement.

    for(int index = 0; index < anArray.length; index++)

        temp[i] = anArray[i];

    • ○ The above statement is used to returns the each element in the array “temp” using “for” loop.

Modified method definition of “copyArray”:

public static int[] copyArray(int[] anArray)

{

    //Declare an array "temp"

    int[] temp = new int[anArray.length];

/* Copy values of "anArray" to "temp" array using "for" loop */

    for(int index = 0; index < anArray.length; index++)

        //Store value of "anArray" to "temp" array

        temp[index] = anArray[index];

    //Display statement

    System.out.println("Values in 'temp' array");

    //Display the values in "temp" array

    for(int index = 0; index < anArray.length; index++)

        System.out.println(temp[index]);

    //Return the values in "temp" array

    return temp;

}

Explanation:

The above method definition is used to copy the array to another array variable.

  • • Declare an array “temp”.
  • • Copy the value of “anArray” to “temp” array using “for” loop.
  • • Display the values in values of “temp” array using “for” loop.

Complete executable code:

The complete executable code for “copyArray” is given below:

//Import required package

import java.util.Scanner;

//Define "Main" class

class Main

{

    //Define main function

    public static void main(String[] args)

    {

        //Create an array "arr"

        int[] arr = new int[5];

        //Create scanner object

        Scanner input = new Scanner(System...

Blurred answer
Students have asked these similar questions
import java.util.Scanner; public class ModifyArray {    public swapArrayEnds(10, 20, 30, 40) {       for (i = 0; i < (arrVals.length / 2); ++i) {         tempValue = arrVals[i]; // Do swap                              //*please fix code//         arrVals[i] = arrVals[arrVals.length - 1 - i];         arrVals[arrVals.length - 1 - i] = tempValue;      Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. public static void main (String [] args) {      Scanner scnr = new Scanner(System.in);      int numElem = 4;      int[] sortArray = new int[numElem];      int i;      int userNum;       for (i = 0; i < sortArray.length; ++i) {         sortArray[i] = scnr.nextInt();      }       swapArrayEnds(sortArray);       for (i = 0; i < sortArray.length; ++i) {         System.out.print(sortArray[i]);         System.out.print(" ");      }      System.out.println("");   }}
public class FindArrayDifference {        static void arrayDifference(int a[], int b[]) {        int k =  0;        int [] c = new int[a.length];        for(int i=0; i < a.length; i++) {            int j;            for(j = 0; j < b.length; j++)                if(a[i] == b[j])                    break;                            if(j == b.length)                c[k++] = a[i];        }        for(int j = 0; j < k; j++)            System.out.println(c[j]);    }     public static void main(String[] args) {                int a[] = {1,2,3,7,8,15,26};        int b[] = {1,2,3,15,4,8,6};                arrayDifference(a,b);    } }   Calculate the algorithm step number and algorithm time complexity of the above program?
Beginner Java import java.util.Arrays;import java.util.Scanner; Arrays and scanner required.   Suppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example:               100.5 65 pizza 100 45.75 70 5.8 5 Also suppose that you have two integers n and m that are both positive. For example: n = 5 and m = 10 Write a static method: public static double partialSum(Scanner s, int n, int m) that takes a Scanner and two integers n and m as arguments and returns the sum of the integers read by the Scanner that are divisible by n and not divisible by m.   In the above example, the integers divisible by 5 and not divisible by 10 are 65 and 5, so the result would be 70.   If the Scanner is empty, or no tokens are valid integers, the result should be 0. You can assume the Scanner is not null.   It is probably easiest to test your method by creating a Scanner with a String instead of System.in. The starter code for an example of how to do this is…

Chapter 7 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 7.2 - Give the definition of a static method called...Ch. 7.2 - Prob. 12STQCh. 7.2 - The following method compiles and executes but...Ch. 7.2 - Suppose that we add the following method to the...Ch. 7.3 - Prob. 15STQCh. 7.3 - Replace the last loop in Listing 7.8 with a loop...Ch. 7.3 - Suppose a is an array of values of type double....Ch. 7.3 - Suppose a is an array of values of type double...Ch. 7.3 - Prob. 19STQCh. 7.3 - Consider the partially filled array a from...Ch. 7.3 - Repeat the previous question, but this time assume...Ch. 7.3 - Write an accessor method getEntryArray for the...Ch. 7.4 - Prob. 23STQCh. 7.4 - Write the invocation of the method selectionSort...Ch. 7.4 - How would you need to change the method...Ch. 7.4 - How would you need to change the method...Ch. 7.4 - Consider an array b of int values in which a value...Ch. 7.5 - What output is produced by the following code?...Ch. 7.5 - Revise the method showTable in Listing 7.13 so...Ch. 7.5 - Write code that will fill the following array a...Ch. 7.5 - Write a void method called display such that the...Ch. 7.6 - Prob. 33STQCh. 7.6 - Prob. 34STQCh. 7 - Write a program in a class NumberAboveAverage that...Ch. 7 - Write a program in a class CountFamiles that...Ch. 7 - Write a program in a class CountPoor that counts...Ch. 7 - Write a program in a class FlowerCounter that...Ch. 7 - Write a program in a class characterFrequency that...Ch. 7 - Create a class Ledger that will record the sales...Ch. 7 - Define the following methods for the class Ledger,...Ch. 7 - Write a static method isStrictlyIncreasing (double...Ch. 7 - Write a static method removeDuplicates(Character[]...Ch. 7 - Write a static method remove {int v, int [] in}...Ch. 7 - Suppose that we are selling boxes of candy for a...Ch. 7 - Create a class polynomial that is used to evaluate...Ch. 7 - Write a method beyond LastEntry (position) for the...Ch. 7 - Revise the class OneWayNoRepeatsList, as given in...Ch. 7 - Write a static method for selection sort that will...Ch. 7 - Overload the method selectionSort in Listing 7.10...Ch. 7 - Revise the method selectionSort that appears in...Ch. 7 - Prob. 18ECh. 7 - Write a sequential search of an array of integers,...Ch. 7 - Write a static method findFigure (picture,...Ch. 7 - Write a static method blur (double [] [] picture)...Ch. 7 - Write a program that reads integers, one per line,...Ch. 7 - The following code creates a small phone book. An...Ch. 7 - Write the method rotateRight that takes an array...Ch. 7 - The following code creates a ragged 2D array. The...Ch. 7 - Write a program that will read a line of text that...Ch. 7 - A palindrome is a word or phrase that reads the...Ch. 7 - Add a method bubbleSort to the class ArraySorter,...Ch. 7 - Add a method insertionSort to the class...Ch. 7 - The class TimeBook in Listing 7.14 is not really...Ch. 7 - Define a class called TicTacToe. An object of type...Ch. 7 - Repeat Programming Project 10 from Chapter 5 but...Ch. 7 - Prob. 8PPCh. 7 - Write a GUI application that displays a picture of...Ch. 7 - ELIZA was a program written in 1966 that parodied...Ch. 7 - Prob. 11PPCh. 7 - Create a GUI application that draws the following...Ch. 7 - Practice Program 2 used two arrays to implement a...Ch. 7 - Practice Program 5.4 asked you to define Trivia...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
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