Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 3, Problem 21C
Suppose you are given an array, A, containing 100 integers that were generated using the method r.nextInt(10), where r is an object of type java.util.Random. Let x denote the product of the integers in A. There is a single number that x will equal with probability at least 0.99. What is that number and what is a formula describing the probability that x is equal to that number?
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In java,
Write a method checks if a given matrix is a unit matrix. An N x N unit matrix is a matrix of N rows and N columns where all the diagonal elements are ‘1’s and all the remaining elements are ‘0’s. The method should take a 2D array of integers as argument and return a Boolean which is true/false depending on whether the matrix corresponding to the 2D array is a unit matrix or not. Test the method in the ‘main’ method.
For problems asking for 'methods', you need to write the method, and the 'main' method which is used as a 'driver' to test the given method.
Write a Java method that takes an array of type int and returns a boolean value. The
method returns true if the number of positive elements in the parameter array is
larger than the number of negative elements. The method returns false if any of the
following conditions is satisfied:
• The parameter array is of length of 0 or 1
• The number of positive elements of the parameter array is less than or equal
to the number of negative elements
• The number of positive elements or negative elements is equal to zero
The method's header is as follows.
public static boolean verify (int[] x)
Sample run
Result
int[] x =
(1, 2, 3};
verify(x);
{5};
verify(x);
int[] x = {1, 2, 3, -5, -1};
boolean b = verify(x);
int[] x = new int[0];
verify(x);
false
boolean b =
int[] x =
boolean b =
false
true
false
boolean b =
BIU A
Paragraph
+v
Programming exercise (in java eclipse, please add comments for each step). Write a complete Java program named FindProduct that contains the following: a. A main method asks the user to provide the number of rows and columns for a 2-dimensional array of integers. b. A main method calls the getArray() method that creates the 2D array of specified size and populates it with random values from 0 to 100. c. A main method prints the elements of the 2D array created by getArray(). d. A main method calls the printProduct method that will: i. Receive the two-dimensional array as input ii. Calculate the product of elements in each row of the array iii. Display the product on the Console. The product of all elements in each row should be formatted as "Row 1 Product XXXX"
Chapter 3 Solutions
Data Structures and Algorithms in Java
Ch. 3 - Prob. 1RCh. 3 - Write a Java method that repeatedly selects and...Ch. 3 - Prob. 3RCh. 3 - The TicTacToe class of Code Fragments 3.9 and 3.10...Ch. 3 - Prob. 5RCh. 3 - Prob. 6RCh. 3 - Prob. 7RCh. 3 - Prob. 8RCh. 3 - Prob. 9RCh. 3 - Prob. 10R
Ch. 3 - Prob. 11RCh. 3 - Prob. 12RCh. 3 - Prob. 13RCh. 3 - Prob. 14RCh. 3 - Prob. 15RCh. 3 - Prob. 16RCh. 3 - Prob. 17CCh. 3 - Prob. 18CCh. 3 - Prob. 19CCh. 3 - Give examples of values for a and b in the...Ch. 3 - Suppose you are given an array, A, containing 100...Ch. 3 - Write a method, shuffle(A), that rearranges the...Ch. 3 - Suppose you are designing a multiplayer game that...Ch. 3 - Write a Java method that takes two...Ch. 3 - Prob. 25CCh. 3 - Prob. 26CCh. 3 - Prob. 27CCh. 3 - Prob. 28CCh. 3 - Prob. 29CCh. 3 - Prob. 30CCh. 3 - Prob. 31CCh. 3 - Prob. 32CCh. 3 - Prob. 33CCh. 3 - Prob. 34CCh. 3 - Prob. 35CCh. 3 - Write a Java program for a matrix class that can...Ch. 3 - Write a class that maintains the top ten scores...Ch. 3 - Prob. 38PCh. 3 - Write a program that can perform the Caesar cipher...Ch. 3 - Prob. 40PCh. 3 - Prob. 41PCh. 3 - Prob. 42PCh. 3 - Prob. 43P
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Porter’s competitive forces model: The model is used to provide a general view about the firms, the competitors...
Management Information Systems: Managing the Digital Firm (15th Edition)
T F: Radio buttons that are placed inside a group box are treated as one group, separate and distinct from any ...
Starting Out With Visual Basic (8th Edition)
Categorize each of the following items as either hardware or software: CPU C++ compiler ALU C++ preprocessor in...
C How to Program (8th Edition)
What is the difference between a protected class member and a private class member?
Starting Out with Java: Early Objects (6th Edition)
What is the output produced by the following?
Absolute Java (6th Edition)
At what time(s) can an object diagram change? How is it changed?
Objects First with Java: A Practical Introduction Using BlueJ (6th 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
- Write a program in Java for the below situation In a royal casino, there is a lottery at 14 number card. If a person is playing that game, find the probability of his winnings. if we have array of all cards = %3D {1,2,014,78,90,56,105,789,43,89,56,90,123,71,101,51, 134,0,4,12,0,6,54,98,54)arrow_forwardwrite a program using java Write the class Student having attributes Id (int), name (String), age (int) and cgpa (double) and methods to set the values of attributes and to return them.Write another class name Student Management having array of students and count of students as attribute. There should be method for the following:1. Add new Student in Array2. Remove a Student from Array using Id (of Student)3. Sort the Students in array using Id (ascending order)4. Sort the Students in array using Age (ascending order)5. Sort the Students in array using CGPA (ascending order)6. Reverse the order of the Students in array7. Display information of all Students in array (from zero index to last index)Write another class named as Main having main method and show activity of above methods.1. Add 5 Students in array2. Display all students in array3. Remove a student by name [Hint: Use equals method to check equality e.g. str1.equals(str2) ]4. Display all student in array5. Sort array by Id and…arrow_forwardImplement the vacuum cleaning agent function in Python/Java for the scenario described below: Consider two-dimensional Boolean array of size m x n (m and n can each be equal to 4) where m is the number of rows, and n is the number of columns. Obtain numerical positive integer values for m and n as inputs from the user. Generate random numbers and scale them down to either a “0” or a “1” according to whether they represent “Clean” or “Dirty”, respectively. After filling the array with 0s or 1s, start at some random array position indicating the initial position of the vacuum cleaner. First check if at that current position, there is the presence of a “Dirt” or not determined by whether 0 or 1 is filled in at that initial position of the vacuum cleaner. If there is a “Dirt”, clean it, and if there is no “Dirt” do a No-operation. After this is done, generate random numbers modulo 4 to obtain random numbers 0, 1, 2, 3 indicating whether to take a left, right, up or down correspondingly. If…arrow_forward
- FOR JAVA Write a method that takes a one dimensional integer array and an integer, and returns the index of the integer in the array. If the integer is not in the array, your program must return -1. If there are multiple occurrences of that number, your program must return the smallest index. Repeat the second question, but if there are multiple occurrences of the number, your program must return the largest index instead of the small- est.arrow_forwardWrite a JAVA program that handles a 2-dimensional array representing a 3x3 tic-tac-toe board. In the game, players take turns writing an X or an O and trying to get three in a row. Write three methods, one to solve for a horizontal win, one to solve for a vertical win, and one to solve for a diagonal win. A win occurs when three symbols for a player are in a line (horizontal, vertical, or diagonal). Create four 2-d (3x3) arrays, one with a horizontal win for O, one with a vertical win for X, one with a diagonal win for O, and the last being a cat game (no player wins). It is tempting to populate the arrays with a ‘X’ and ‘O’ characters but consider representing these values with int values instead which makes calculating a win much easier. Consider a zero value as an empty cell. You do not have to write code to input the values, just define the four arrays using initializer lists (see example below). For example: an array with { {1, 0, -1}, {1, -1, 0}, {1, -1, 0} } has a vertical…arrow_forwardFOR JAVA Write a method that takes an integer array, prints the elements that are divisible by 3 and returns the sum of these elements. Write a method that takes an integer array and returns the difference between the sum of even indexed elements and the sum of odd indexed elements in that array. Write a method that takes a String array and an integer as parameters and prints the Strings in the array whose length is greater than the second parameter. Write a method that takes two integer arrays, and returns a new array by taking the intersection of the two arrays.arrow_forward
- Write a Java method that takes an array of type int and returns a boolean value. The method returns true if the number of positive elements in the parameter array is larger than the number of negative elements. The method returns false if any of the following conditions is satisfied: • The parameter array is of length of O or 1 • The number of positive elements of the parameter array is less than or equal to the number of negative elements • The number of positive elements or negative elements is equal to zero The method's header is as follows. public static boolean verify (int[] x) Sample run int[] x = boolean b = Result {1, 2, 3}; false verify(x); int[] x = (5}; boolean b = false verify(x); int[] x = {1, 2, 3, -5, -1}; verify(x); int[] x = new int[@]; verify(x); true boolean b% = false boolean b% = +v Paragrapharrow_forwardwrite a java method with followin instraction : Write a method called prioritizeQueue to give priority for vaccination for elderly persons. The method is to be in a class called VaccinationQueue and has two parameters q1 and q2 type ArrayQueue. The q1 and q2 data are of type Integer. Assume that initially q1 is not empty and q2 is empty. The method will insert from q1 those elements towards the beginning of the queue (front) q2 whose data is greater than or equal to 45 and will insert those elements whose data is less than 45 and greater than or equal to 18 towards the end (rear) of q2. Any data item less than 18 of q1 will not be inserted in q2. It returns the number of elements added to q2. Class ArrayQueue and all its methods including iterator are available for use. You can also create temporary queues. You are not allowed to use arrays or any other data structure. Example: Before method call: front rear q1: 14 25 50…arrow_forwardWrite in Java. Given an array of integers A, find the Majority Element. Majority Element in an array of size N in an element that appears more than N/2 times. Write a function: int findMajority(int[] A) that accepts an array A. The function should return the Majority Element in the array. If no majority element then return 0. Use following methods to solve the problem: int findCandidate(int a[]) that accepts the array and find a candidate for the majority boolean isMajority(int a[], int cand) that accept the array and the candidate element and check if the candidate occurs more than n/2 times Input: 5 1 2 1 2 2 Where, First line represents the size of an array. Second line represents array elements separated by single space. Output: 2 Here for the given array, 2 appears 3 times in the array of size 5. No space after the element in the output. Assume that, N is an integer within the range [1 to 10000]. Array elements are within the range…arrow_forward
- Write in Java. Given an array of integers A, find the Majority Element. Majority Element in an array of size N in an element that appears more than N/2 times. Write a function: int findMajority(int[] A) that accepts an array A. The function should return the Majority Element in the array. If no majority element then return 0. Use following methods to solve the problem: int findCandidate(int a[]) that accepts the array and find a candidate for the majority boolean isMajority(int a[], int cand) that accept the array and the candidate element and check if the candidate occurs more than n/2 times Input: 5 1 2 1 2 2 Where, First line represents the size of an array. Second line represents array elements separated by single space. Output: 2 Here for the given array, 2 appears 3 times in the array of size 5. No space after the element in the output. Assume that, N is an integer within the range [1 to 10000]. Array elements are within the range…arrow_forwardPlease complete the task by yourself only in JAVA with explanation. Don't copy. Thank you. Using quicksort to sort an array of car objects by various criteria. Define a class Car as follows: class Car { public String make; public String model; public int mpg; // Miles per gallon } a) Implement a comparator called CompareCarsByMakeThenModel that can be passed as an argument to the quicksort method from the lecture notes. CompareCarsByMakeThenModel should return a value that will cause quicksort to sort an array of cars in ascending order (from smallest to largest) by make and, when two cars have the same make, in ascending order by model. b) Implement a comparator called CompareCarsByDescendingMPG that can be passed as an argument to the quicksort method from the lecture notes. CompareCarsByDescendingMPG should return a value that will cause quicksort to sort an array of cars in descending order (from largest to smallest) by mpg. c) Implement a comparator called…arrow_forwardPlease complete the task by yourself only in JAVA with explanation. Don't copy. Thank you. Using quicksort to sort an array of car objects by various criteria. Define a class Car as follows: class Car { public String make; public String model; public int mpg; // Miles per gallon } a) Implement a comparator called CompareCarsByMakeThenModel that can be passed as an argument to the quicksort method from the lecture notes. CompareCarsByMakeThenModel should return a value that will cause quicksort to sort an array of cars in ascending order (from smallest to largest) by make and, when two cars have the same make, in ascending order by model.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
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License