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 (8th Edition)
- package filterSort;import java.io.File;import java.io.FileNotFoundException;import java.util.Arrays;import java.util.Scanner;import java.util.*; public class FilterSort { /*** Allocates and returns an integer array twice the size of the one* supplied as a parameter. The first half of the new array will* be a copy of the supplied array and the second half of the new* array will be zeros.** @param arr the array to be copied* @return array twice the size of <tt>arr</tt> with its initial* elements copied from <tt>arr</tt>* @throws NullPointerException if <tt>arr</tt> is null.*/public static int[] doubleArrayAndCopy(int[] arr) {int len = arr.length;int max = arr.length * 2;int[] copyArr = Arrays.copyOf(arr,max);int length = copyArr.length-1;Arrays.sort(copyArr, 0, length);return copyArr; } public static void main(String[] args) {int[] data = new int[8];try {File file = new File("data.txt");Scanner myReader = new Scanner(file);for(int i = 0;…arrow_forwardStringFun.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_forward
- import 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_forwardimport 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_forward
- Make a game. package p1; public class Game { public static void main(String[] args) { throw new RuntimeException("Not implemented"); } }arrow_forwardPlease 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_forward
- import 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_forwardcode: import java.util.Random;import java.util.ArrayList;import java.util.Scanner;public class WeekendOrders {/*** * This method takes as input the scanner instance created from main and* asks the user for input on the number of orders placed on each of the * days on the long weekend (Saturday, Sunday, and Monday). * Put the number of orders for each day in an array of fixed size* * * @param scnr (an instance of Scanner)* @return an array of int which includes the number of orders* placed on each day, with one day per index of the array*/public static int[] getUserInput(Scanner scnr){// DO NOT MODIFY THIS LINEint[] userInput = new int[3];// TODO: Prompt user for input on number of orders on each day// and store in userInputreturn userInput; }/*** * This method appends all the new orders for the given day onto the * arraylist. It first generates a random int between 0-2 (inclusive).* If the int is 0: it adds "Burger" to the prevOrders list* If the int is 1: it adds "Fries" to the…arrow_forwardpublic static void main(String[] args) throws IOException{ BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input instructor Salary: "); double salary=Double.parseDouble(b.readLine()); System.out.print("Input Academic Rank: "); String rank=b.readLine(); double tax=0; if(rank.equalsIgnoreCase("Assistant")) tax=salary*9/100; else if(rank.equalsIgnoreCase("Associate")) tax=salary*12/100; else if(rank.equalsIgnoreCase("Lecturer")) tax=salary*7/100; double net_salary=salary-tax; System.out.println("\nThe amount of Tax is BD "+tax); System.out.println("The Net Salary is BD "+net_salary); } }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