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...
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- StringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forwardimport java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // TODO: Read and set base and height for triangle1 (use setBase() and setHeight()) // TODO: Read and set base and height for triangle2 (use setBase() and setHeight()) System.out.println("Triangle with smaller area:"); // TODO: Determine smaller triangle (use getArea()) // and output smaller triangle's info (use printInfo()) }} public class Triangle { private double base; private double height; public void setBase(double userBase){ base = userBase; } public void setHeight(double userHeight) { height = userHeight; } public double getArea() { double area = 0.5 * base * height; return area; } public void printInfo() { System.out.printf("Base:…arrow_forwardimport java.util.Scanner; public class Application {public static void main(String[] args){ int Num;float Ave;Employee[] Emp = new Employee[100]; System.out.println("Enter the number of employees:");Num = Integer.parseInt(new Scanner(System.in).nextLine());for (int i = 0; i < Num; i++){ Emp[i] = new Employee();Emp[i].Input();} System.out.printf("The list of employees:");System.out.println();Display(Emp, Num);Ave= AverageSalary(Emp, Num);System.out.printf("\nAverage salary is:" + "%.2f", Ave); System.out.printf("\nThe employee with highest salary is:\n" + employeeHigh(Emp, Num));}private static float AverageSalary(Employee [] emps, int n){float sum=0; for (int i = 0; i < n; i++)sum = sum + emps[i].salary;return sum/n;}private static void Display(Employee[] arr, int n){ for (int i = 0; i < n; i++){System.out.printf("%s",arr[i]);}}private static Employee employeeHigh(Employee[] arr, int n){ float high= arr[0].salary ;int iHigh = 0;for (int i = 1; i < n; i++){if( arr[i].salary…arrow_forward
- import java.util.Scanner; public class MorgansBonuses { public static void main(String[] args) { int WeeksWorked; int Reviews; int x, y; final int Quit = 99; double[][] bonuses = { { 5, 9, 16, 22, 30 }, { 10, 12, 18, 24, 36 }, { 20, 25, 32, 42, 55, 68 }, { 46, 54, 65, 77, 90 }, { 60, 72, 84, 96, 120 }, { 85, 100, 120, 140, 175 } }; Scanner ID = new Scanner(System.in); System.out.println("Please enter the number of weeks the employee has worked or " + Quit + " to quit.>>"); WeeksWorked = ID.nextInt(); while (WeeksWorked != Quit) { System.out.println("Please enter the number of positive reviews the employee has recieved or " + Quit +" to end program.>>"); Reviews = ID.nextInt(); if (WeeksWorked >= bonuses.length) { WeeksWorked = bonuses.length - 1; } if (Reviews >= bonuses[0].length) { Reviews =…arrow_forwardI have the following code: import java.util.*; import java.io.*; public class GradeBook { publicstaticvoidmain(String[] args)throwsIOException{ // TODO Auto-generated method stub File infile =newFile("students.dat"); Scanner in =newScanner(infile); while(in.hasNext()){ // Read information form file and create a student object and print String name = in.nextLine(); Student student =newStudent(name, in.nextLine()); for(int i =1; i <=4;++i){ student.setQuiz(i, in.nextInt()); } student.setMidtrmExm(in.nextInt()); student.setFinalExm(in.nextInt()); in.nextLine(); // Calculate grade and letter grade; double overallQuizScore=0.0,score=0.0; for(int i=1;i<=student.NUM_QUIZZES;i++) { overallQuizScore+=(student.getQuiz(i)/student.QUIZ_MAX_POINTS)*100; } overallQuizScore = (overallQuizScore/4)*0.30; score =…arrow_forwardfor the class ArrayAverage write a code that calculates the average of the listed numbers with a double result for the class ArrayAverageTester write a for each loop and print out the resultarrow_forward
- Please help me fix the code Make it looks like the expected import java.util.Scanner; public class NumberLoops { public static void main(String[] args) { double num; double result = 1; int i; Scanner sc = new Scanner(System.in); // Create a Scanner object // Take user input for the Positive Number System.out.print("Enter a positive integer: "); num = sc.nextDouble(); // Read user input // if the input number is not an integer if(num % 1 != 0) { // print the message System.out.println("Not an Integer: " + num); System.exit(0); } // if the number is not a positive number if(numarrow_forwardimport java.util.Scanner;public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); int credits; int seed; GVDie die1, die2; die1 = new GVDie(); die2 = new GVDie(); // Read random seed to support testing (do not alter) seed = scnr.nextInt(); die1.setSeed(seed); // Read starting credits credits = scnr.nextInt(); int rounds = 0; while (credits > 0) { // Step 1: Roll both dice die1.roll(); die2.roll(); int total = die1.getValue() + die2.getValue(); if (total == 7 || total == 11) { // Player wins one credit credits++; // UPDATE - print the dice total here System.out.println("Dice total: " + total); //UPDATE - break the loop and end the round break; } else if (total == 2 ||…arrow_forwardimport java.util.Scanner; public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); int credits; int seed; GVDie die1, die2; die1 = new GVDie(); die2 = new GVDie(); // Read random seed to support testing (do not alter) seed = scnr.nextInt(); die1.setSeed(seed); // Read starting credits credits = scnr.nextInt(); int rounds = 0; while (credits > 0) { // Step 1: Roll both dice die1.roll(); die2.roll(); int total = die1.getValue() + die2.getValue(); if (total == 7 || total == 11) { // Player wins one credit credits++; } else if (total == 2 || total == 3 || total == 12) { // Player loses one credit credits--; } else { // Set the goal for future rolls int goal = total;…arrow_forward
- import java.util.Scanner; public class CharMatch { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userString; char charToFind; int strIndex; userString = scnr.nextLine(); charToFind = scnr.next().charAt(0); strIndex = scnr.nextInt(); /* Your code goes here */ }}arrow_forwardjava program : Assume the following method is added to the ArrayStack class. What does this method do? public ArrayStack<E> Undefined() { int j = topOfStack; ArrayStack<E> st1= new ArrayStack<E>(topOfStack + 1); for(int i = 0; i < topOfStack + 1; i++) { st1.theData[j] = theData[i]; j-- ; } return st1; } The method will return a stack containing all the elements of “this” stack in the reverse order. The method will return a stack containing the alternative elements of “this” stack in the original order. The method will return a stack containing all the elements of “this” stack in the original order. The method will return a stack containing the alternative elements of “this” stack in the reverse order.arrow_forwardjava program : Assume the following method is added to the ArrayStack class. What does this method do? public ArrayStack<E> Undefined() { int j = topOfStack; ArrayStack<E> st1= new ArrayStack<E>(topOfStack + 1); for(int i = 0; i < topOfStack + 1; i++) { st1.theData[j] = theData[i]; j-- ; } return st1; } The method will return a stack containing all the elements of “this” stack in the reverse order. The method will return a stack containing the alternative elements of “this” stack in the original order. The method will return a stack containing all the elements of “this” stack in the original order. The method will return a stack containing the alternative elements of “this” stack in the reverse order.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education