Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Bartleby Related Questions Icon
Related questions
Question
Please use java.
In this assignment, you will implement a class calledArrayAndArrayList. This class includes some interesting methods for working with Arrays and ArrayLists.
For example, the ArrayAndArrayList class has a "findMax" method which finds and returns the max number in a given array. For a defined array: int[] array = {1, 3, 5, 7, 9}, calling findMax(array) will return 9.
There are 4 methods that need to be implemented in the A rrayAndArrayList class:
●くろまる howMany(int[] array, int element) - Counts the number of occurrences of the given element in the given array.
●くろまる findMax(int[] array) - Finds the max number in the given array.
●くろまる maxArray(int[] array) - Keeps track of every occurrence of the max number in the given
array.
●くろまる swapZero(int[] array) - Puts all of the zeros in the given array, at the end of the given
array.
Each method has been defined for you, but without the code. See the javadoc for each method for instructions on what the method is supposed to do and how to write the code. It should be clear enough. In some cases, we have provided hints and example method calls to help you get started.
For example, we have defined a "howMany" method for you (see below) which counts and returns the number of occurrences of a given element in a given array. For now, the method returns a 0 value as a placeholder. Read the javadoc, which explains what the method is
supposed to do. Then write your code where it says "// TODO" to implement the method. You’ll do this for each method in the program.
/**
* Counts the number of occurrences of the given element in the given
array.
* @param array to search
* @param element to search for
* @return number of times element is in array */
public int howMany(int[] array, int element) { // TODO Implement method
return 0; }
In addition, you will write unit tests to test your method implementations. Each unit test method has been defined for you, including some test cases. First make sure you pass all of the provided tests, then write a dditional and distinct test cases for each unit test method.
For example, we have defined a "testHowMany" method for you (see below) which tests the "howMany" method. Pass the tests provided then write additional tests where it says "// TODO". You’ll do this for each unit test method in the program.
/**
* Test howMany method in ArrayAndArrayList. */
@Test
void testHowMany() {
// element in the array
int[] array = {1, 3, 5, 7, 9, 1, 2, 3, 4, 5}; assertEquals(2, this.myArrayAndArrayList.howMany(array, 1));
// TODO write at least 3 additional test cases }
Tips for this Assignment
In this assignment, some tips are given as follows:
●くろまる Unit testing a method that doesn't return anything in Java:
○しろまる Although a method doesn't return a value, it has some side effects and can be
tested. There may be a way to verify that the side effects actually occurred as expected.
For example: Consider a method that modifies a given array. The elements in the array will be changed after the method is called. Thus, an array can be created, and then the method can be called. After that, the same array can be tested for correctness.
Here’s an example testing the "swapZero" method, which puts all of the zeros in a given array, at the end of the array. The method updates the array itself, so we need to call the method, then test the array directly.
//create array
int[] array = {0, 1, 0, 2};
//call swapZero method with array this.myArrayAndArrayList.swapZero(array);
//test updated array by comparing to another test array int[] testArray = {1, 2, 0, 0}; assertArrayEquals(testArray, array);
●くろまる Creating an empty array in Java: ○しろまる For example:
int[] array = new int[0];
Transcribed Image Text:Penn
Engineering
|ONLINE LEARNING
Introduction to Java & Object Oriented Programming
For example: Consider a method that modifies a given array. The elements in
the array will be changed after the method is called. Thus, an array can be
created, and then the method can be called. After that, the same array can be
tested for correctness.
Here's an example testing the "swapZero" method, which puts all of the zeros in
a given array, at the end of the array. The method updates the array itself, so
we need to call the method, then test the array directly.
//create array
int[] array = {0, 1, 0, 2};
//call swapZero method with array
this.myArrayAndArrayList.swapZero (array);
//test updated array by comparing to another test array
int[] testArray = {1, 2, 0, 0};
assertArrayEquals (testArray, array) ;
Creating an empty array in Java:
For example:
int[] array = new int[0];
Transcribed Image Text:The Assignment
In this assignment, you will implement a class called ArrayAndArrayList. This class includes
some interesting methods for working with Arrays and ArrayLists.
For example, the ArrayAndArrayList class has a "findMax" method which finds and returns the
max number in a given array. For a defined array: int) array = {1, 3, 5, 7, 9), calling
findMax(array) will return 9.
There are 4 methods that need to be implemented in the ArrayAndArrayList class:
• howManylint] array, int element) - Counts the number of occurrences of the given
element in the given array.
• findMax(int] array) - Finds the max number in the given array.
• maxArray(int[] array) - Keeps track of every occurrence of the max number in the given
array.
• swapZero(int] array) - Puts all of the zeros in the given array, at the end of the given
array.
Each method has been defined for you, but without the code. See the javadoc for each method
for instructions on what the method is supposed to do and how to write the code. It should be
clear enough. In some cases, we have provided hints and example method calls to help you
get started.
For example, we have defined a "howMany" method for you (see below) which counts and
returns the number of occurrences of a given element in a given array. For now, the method
returns a 0 value as a placeholder. Read the javadoc, which explains what the method is
APenn
A Engineering
ONLINE LEARNING
Introduction to Java & Object Oriented Programming
supposed to do. Then write your code where it says "// TODO" to implement the method.
You'll do this for each method in the program.
/**
* Counts the number of occurrences of the given element in the given
array.
* eparam array to search
* @param element to search for
* ereturn number of times element is in array
public int howMany (int [] array, int element) {
// TODO Implement method
return 0:
In addition, you will write unit tests to test your method implementations. Each unit test
method has been defined for you, including some test cases. First make sure you pass all of
the provided tests, then write additional and distinct test cases for each unit test method.
For example, we have defined a "testHowMany" method for you (see below) which tests the
"howMany" method. Pass the tests provided then write additional tests where it says "/
TODO". You'll do this for each unit test method in the program.
* Test howMany method in ArrayAndarrayList.
@Test
void testHowMany () {
// element in the array
int () array - (1, 3, 5, 7, 9, 1, 2, 3, 4, 5};
assertEquals (2, this.myArrayAndArrayList.howMany (array, 1)):
// TODO write at least 3 additional test cases
Tips for this Assignment
In this assignment, some tips are given as follows:
• Unit testing a method that doesn't return anything in Java:
o Although a method doesn't return a value, it has some side effects and can be
tested. There may be a way to verify that the side effects actually occurred as
expected.
APenn
Engineering
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
This is a popular solution
bartleby
Trending nowThis is a popular solution!
bartleby
Step by stepSolved in 3 steps with 1 images
Knowledge Booster
Background pattern image
Similar questions
- JAVA complete a method that swaps the first and second half of an array of integers. For example, if the array contains the values 1 4 9 16 25 36then after calling the method, it should contain the values16 25 36 1 4 9If the array contains an odd number of elements, leave the middle element in place. For example, 1 4 9 16 25 36 45becomes25 36 45 16 1 4 9arrow_forwardWrite the definition of a method reverse, whose parameter is an array of chars. The method reverses the elements of the array. The method does not return a value.arrow_forwardFour integers are read from input and stored into the array arrayToModify. Write a static method resetFirstValue() that takes an integer array parameter and replaces the first element with 1. Ex: If the input is 55 45 70 65, then the output is: Original array: 55 45 70 65 Changed array: 1 45 70 65 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Scannerscnr=newScanner(System.in); finalintNUM_ELEM=4; int[] arrayToModify=newint[NUM_ELEM]; inti; intuserNum; for (i=0; i<arrayToModify.length; ++i) { arrayToModify[i] =scnr.nextInt(); } System.out.print("Original array: "); printArr(arrayToModify); resetFirstValue(arrayToModify); System.out.print("Changed array: "); printArr(arrayToModify); } }arrow_forward
- Write a program named GradeStats.java then implement the following method: public static int[] gradeStats(int[] arr) It accepts int array representing test grades for CISY432 then calculates and returns the following grade statictics: highest, lowest, average, number of grades that are greater than or equal to 70, the number of grades that are less than 70 It returns these five numbers as an int array, and the 1st element is for highest, 2nd for lowest, 3rd for average 4th element for number of grades that are greater than or equal to 70 5th for the number of grades that are less than 70 For example. O Type here to search DELLarrow_forwardI need to have a main method in a Tester class that will: Fill an array of type Vehicle[] of size NUM_VEHICLES with Vehicle objects. You need to generate a random number between 0 and 2. If the number is 0, add a Vehicle to the array. 1, add a Car, 2, add a Boat. The Vehicle/Car/Boat that you add must be initialized with a random efficiency between 0 and 100.0. Do this until you have added NUM_VEHICLES objects to the array How do I write it so that the array generates random numbers 0 through 2 (inclusive) and how would I then correlate those numbers to a different class object?arrow_forwardIn java i have some code for a method that finds the index of an integer that is passed in thet int is (target) the method also has the array called (array) and the first and last element of the array. also for this method to work the array has to be in descending order "static int find(int [] array, int first, int last, int target)" thats the method with all the variables that are passed to it my guess is that the first if statement just will have a short line that is "return -1;" because since the array has to be in descending order the first element cant be greater then the last element in the array but im confused on the rest because i cant find a way to make them one line codes with the conditions that i have to meet in the comments any help would be aprreaciatedarrow_forward
- can you plz write it in java.util.scanner formarrow_forwardIntellij - Java Write a program which does the following: Create a class with the main() method. Take a string array input from the user in main() method. Note: Do not hardcode the string array in code. After the String array input loop, the array should be: words[] = {"today", "is", "a", "lovely", "spring","day", "not", "too", "hot", "not", "too", "cold"}; Print the contents of this array using Arrays.toString() method. Write a method called handleString() that performs the following: Takes a String[] array as method parameter (similar to main method). Loop through the array passed as parameter, and for each word in the array: If the length of the word is even, get the index of the last letter & print that index. Use length() and indexOf() String object methods. If the string length is odd, get the character at the 1st position in the string & print that letter. Use length() and charAt() String object methods.arrow_forwardIn Java: Write a method that multiplies all elements in the array by 3 and returns it to the main method.arrow_forward
- Method Details: public static java.lang.String getArrayString(int[] array, char separator) Return a string where each array entry (except the last one) is followed by the specified separator. An empty string will be return if the array has no elements. Parameters: array - separator - Returns: stringThrows:java.lang.IllegalArgumentException - When a null array parameter is providedarrow_forwardA method named getUserStringsAndPopulate Array(). This method should keep prompting the user to enter as many strings as the user wants. The user should be able to stop entering strings by entering EXIT. This method should take the strings entered by the user and populate a one-dimensional array of strings with the user input. The string EXIT should NOT be added to the array because it is a special flag that will tell the method to stop prompting the user to enter more strings. This method should return the one-dimensional array of strings populated with user input and should not accept any arguments. 2.b) A method named searchArray() that would accept a one-dimensional array of strings and search the array for the string Winter. The search logic should NOT be case sensitive. If a match is found, this method should print the string in the array followed by a message like " --- Found a match!". Otherwise, the method should not print anything. This method should NOT return anything....arrow_forward2. Write a method that takes a rectangular two-dimensional array of type int and returns a valu of type int. The method returns the number of rows with negative sum. A row has a negative s if the sum of all its elements is less than 0. public static int countNegativeSum(int[][] in) Taken array Result 1 -3 17 -5 -5 14 -5 -2 30 -19 -8 -8 1 1 3 -1 -4 78 8 -3 -5 -1 -1 3 -4arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY