Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
thumb_up100%
Chapter 7.7, Problem 7.7.1CP
Suppose the following code is written to reverse the contents in an array, explain why it is wrong. How do you fix it?
int[] list;: {1 , 2 , 3 , 5 , 4 };
for (int i ;: 0 , j ;: list.length- 1 ; < 1ist. 1ength; i ++, j --) {
// Swap list[i] with list[j]
int temp;: list[i];
list[i] list[j] ;
list[j] ;: temp ;
}
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
// pre: list != null, list.length > 0
// post: return index of minimum element of array
public static int findMin(int[] list)
{ assert list != null && list.length > 0 : "failed precondition";
int indexOfMin = 0;
for(int i = 1; i < list.length; i++)
{ if(list[i] < list[indexOfMin])
{ indexOfMin = i;
}
}
return indexOfMin;
}
draw DFG, find the all-def/c/p use paths. then write junit test to test the paths for all-def/c/p use for findMin()
// pre: list != null, list.length > 0 // post: return index of minimum element of array
public static int findMin(int[] list) { assert list != null && list.length > 0 : "failed precondition"; int indexOfMin = 0; for(int i = 1; i < list.length; i++) { if(list[i] < list[indexOfMin]) { indexOfMin = i; } } return indexOfMin; }
Question:
draw DFG from the code above
find the all-def/c/p use paths.
Generate test cases to test this function using JUnit! (to test all-def/c/p use path)
Q1. Write the following method that returns thesmallest element in an ArrayList:public static <E extends Comparable<E>> E min(ArrayList<E> list).
Chapter 7 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 7.2 - Prob. 7.2.1CPCh. 7.2 - Prob. 7.2.2CPCh. 7.2 - What is the output of the following code? int x =...Ch. 7.2 - Indicate true or false for the following...Ch. 7.2 - Which of the following statements are valid? a....Ch. 7.2 - Prob. 7.2.6CPCh. 7.2 - What is the array index type? What is the lowest...Ch. 7.2 - Write statements to do the following: a. Create an...Ch. 7.2 - What happens when your program attempts to access...Ch. 7.2 - Identify and fix the errors in the following code:...
Ch. 7.2 - What is the output of the following code? 1....Ch. 7.4 - Will the program pick four random cards if you...Ch. 7.5 - Use the arraycopy method to copy the following...Ch. 7.5 - Prob. 7.5.2CPCh. 7.7 - Suppose the following code is written to reverse...Ch. 7.8 - Prob. 7.8.1CPCh. 7.8 - Prob. 7.8.2CPCh. 7.9 - Prob. 7.9.1CPCh. 7.9 - Prob. 7.9.2CPCh. 7.10 - If high is a very large integer such as the...Ch. 7.10 - Prob. 7.10.2CPCh. 7.10 - Prob. 7.10.3CPCh. 7.11 - Prob. 7.11.1CPCh. 7.11 - How do you modify the selectionSort method in...Ch. 7.12 - What types of array can be sorted using the...Ch. 7.12 - To apply java.util.Arrays.binarySearch (array,...Ch. 7.12 - Show the output of the following code: int[] list1...Ch. 7.13 - This book declares the main method as public...Ch. 7.13 - Show the output of the following program when...Ch. 7 - (Assign grades) Write a program that reads student...Ch. 7 - (Reverse the numbers entered) Write a program that...Ch. 7 - (Count occurrence of numbers) Write a program that...Ch. 7 - (Analyze scores) Write a program that reads an...Ch. 7 - (Print distinct numbers) Write a program that...Ch. 7 - (Revise Listing 5.1 5, PrimeNumber.java) Listing...Ch. 7 - (Count single digits) Write a program that...Ch. 7 - (Average an array) Write two overloaded methods...Ch. 7 - (Find the smallest element) Write a method that...Ch. 7 - Prob. 7.10PECh. 7 - (Statistics: compute deviation) Programming...Ch. 7 - (Reverse an array) The reverse method in Section...Ch. 7 - Prob. 7.13PECh. 7 - Prob. 7.14PECh. 7 - 7 .15 (Eliminate duplicates) Write a method that...Ch. 7 - (Execution time) Write a program that randomly...Ch. 7 - Prob. 7.17PECh. 7 - (Bubble sort) Write a sort method that uses the...Ch. 7 - (Sorted?) Write the following method that returns...Ch. 7 - (Revise selection sort) In Listing 7 .8, you used...Ch. 7 - (Sum integers) Write a program that passes an...Ch. 7 - (Find the number of uppercase letters in a string)...Ch. 7 - (Game: locker puzzle) A school bas 100 lockers and...Ch. 7 - (Simulation: coupon collectors problem) Coupon...Ch. 7 - (Algebra: solve quadratic equations) Write a...Ch. 7 - (Strictly identical arrays) The arrays 1ist1 and...Ch. 7 - (Identical arrays) The arrays 1ist1 and 1ist2 are...Ch. 7 - (Math: combinations) Write a program that prompts...Ch. 7 - (Game: pick four cards) Write a program that picks...Ch. 7 - (Pattern recognition: consecutive four equal...Ch. 7 - (Merge two sorted Lists) Write the following...Ch. 7 - (Partition of a list) Write the following method...Ch. 7 - Prob. 7.33PECh. 7 - (Sort characters in a string) Write a method that...Ch. 7 - (Game: hangman) Write a hangman game that randomly...Ch. 7 - (Game: Eight Queens) The classic Eight Queens...Ch. 7 - Prob. 7.37PE
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
State if the members are in tension or compression. Prob. F6-2
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
In the following exercises, write a program to carry out the task. The program should use variables for each of...
Introduction To Programming Using Visual Basic (11th Edition)
Show that for circular motion, force = mass * velocity squared/radius.
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Look at the following class definitions: class Plant: def _ _init_ _ (self, plant_type) : self. _ _plant_type =...
Starting Out with Python (4th Edition)
Distinguish among data definition commands, data manipulation commands, and data control commands.
Modern Database Management
The composite beam is made of steel (A) bonded to brass (B) and has the cross section shown. If it is subjected...
Mechanics of Materials (10th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Duplicate Set This function will receive a list of elements with duplicate elements. It should add all of the duplicate elements to a set and return the set containing the duplicate elements. A duplicate element is an element found more than one time in the specified list. The order of the set does not matter. Signature: public static HashSet<Object> duplicateSet(ArrayList<Object> list) Example: INPUT: [2, 4, 5, 3, 3, 5] OUTPUT: {5, 3}arrow_forwardSubject: Java Programmingarrow_forwardProject Description: In this project you implement an ArrayStack ADT and use the stack for implementing the following methods: a) Reverse an array of Words: Accept an array of words as input parameter and return an array of words in reverse order. Use the method signature: public static String[] reverseWords (String[] wordList) Example Input: Bird Cat Dog Elephant Output: Elephant Dog Cat Birdarrow_forward
- Project Description: In this project you implement an ArrayStack ADT and use the stack for implementing the following methods: a) Reverse an array of Words: Accept an array of words as input parameter and return an array of words in reverse order. Use the method signature: public static String[] reverse Words (String[] wordList) Example Input: Bird Cat Dog Elephant Output: Elephant Dog Cat Birdarrow_forwardList<String> colors = Arrays.asList(“red”, ”green”, ”blue”, "yellow","purple", "green","red"); Write a line of code to create and pass this list of colors to another type of collection so that the colors are sorted and contain duplicates .arrow_forwardSubject-Object oriented programing Write a program which:• creates a new Array List• adds 5 decimal numbers to it• prints the list to the screen In the same program, replace the element in the ArrayList at index 2 by null. Print the resulting list tothe screen.arrow_forward
- Java: Yellow highlighted columns are outer ‘for’ loop Blue highlighted columns are inner ‘for’ looparrow_forward6. the grade is under 20 which is outlier, remove it from the array list. 7. Print array list using System.out.println() 8. Use indexOf to print index of 80. 9. Use get function. 10. What is the difference between get and index of? 11. Print the values of the array list using Iterator class. 12.. Delete all the values of the array list using clear function. 13. Print all the values of the array after you execute clear using System.out.println(). what is the result of using clear function? 14. What is the shortcoming of using array List?arrow_forward6. Consider the following code segment: ArrayList list = new ArrayList(10) ; list.add( "Ali" ); list.add( "Ahmed" ); list.add( "Hassan" ); list.add( 0, "Qasim" ); Which of the following elements will be at index 1 of the list? K Ali Ahmed Hassan Qasimarrow_forward
- def count_types(lst: List[Any]) -> List[int]: """ Given a list <lst> of random types, return the number of occurrences of each type, in the form of a list, in the order that they were first seen. For example, if the input ['str1', 1, 'str2'], the output would be [2, 1], as a string type appears first, and occurs twice in the list. An integer type appears next, and only occurs once in the list. Another example could be [True, 'str1', 1, False, 'str2', True], where the output would be [3, 2, 1], as a boolean type appears first, and occurs three times in the list. A string appears next, and occurs twice in the list. Finally, an integer appears next, and occurs once in the list. """don't use any import, dictionaries, or dictionary methods or try-except statements.arrow_forwardIn Java List all the values of the array after the function is called static void Fun(int[][]list){ int temp=list[1][2]; list[1][2]=list[0][2]; list[0][2]=temp; }arrow_forwardTo declare an array of String called myStrings, we would code: ArrayList<Blank 1> myStrings = Blank 2 ArrayList<String>();arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- 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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
9.1: What is an Array? - Processing Tutorial; Author: The Coding Train;https://www.youtube.com/watch?v=NptnmWvkbTw;License: Standard Youtube License