Question

Please provide a solution to "Data Structures and Problem Solving Using Java (4th Edition)," Chapter 6,...

Please provide a solution to "Data Structures and Problem Solving Using Java (4th Edition)," Chapter 6, Problem 32E:

https://www.chegg.com/homework-help/maintaining-invariant-elements-priority-queue-sorted-non-inc-chapter-6-problem-32E-solution-9780133001679-exc

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given Question:

Please find the below answer:

package MasterSachin;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/*---Multiset Class---*/
class Multiset < E > {
private List < E > values; /* List to store distinct values */
private List < Integer > frequency; /* List to store counts of distinct values */
private final String ERROR_MSG = "Count cannot be negative: ";

public Multiset() {
values = new ArrayList < > ();
frequency = new ArrayList < > ();
}
//---setCount Method---
public int setCount(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
if (count == 0)
remove(element);
int index = values.indexOf(element);
if (index == -1)
return add(element, count);
int prevCount = frequency.get(index);
frequency.set(index, count);
return prevCount;
}
//---Adding all method---
boolean addAll(Collection << ? extends E > c) {
for (E element: c)
add(element, 1);
return true;
}
//---Adding element to multiset count---
public int add(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
int prevCount = 0;
if (index != -1) {
prevCount = frequency.get(index);
frequency.set(index, prevCount + count);
} else if (count != 0) {
values.add(element);
frequency.add(count);
}
return prevCount;
}

public boolean add(E element) {
return add(element, 1) >= 0;
}

//---checks if exists---
public boolean contains(Object element) {
return values.contains(element);
}
//---Specified number removes---
public int remove(Object element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
if (index == -1)
return 0;
int prevCount = frequency.get(index);
if (prevCount > count) {
frequency.set(index, prevCount - count);
} else {
values.remove(index);
frequency.remove(index);
}
return prevCount;
}
//---Deleting element from multiset---
public boolean remove(Object element) {
return remove(element, 1) > 0;
}
//---Checks the elements---
public boolean containsAll(Collection << ? > c) {
return values.containsAll(c);
}
//---Adding all elements to specified array for multiset---
public void addAll(E...arr) {
for (E element: arr)
add(element, 1);
}
//---For string method---
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < values.size(); i++) {
sb.append(values.get(i));
if (frequency.get(i) > 1)
sb.append(" x ").append(frequency.get(i));
if (i != values.size() - 1)
sb.append(", ");
}
return sb.append("]").toString();
}
}
public class SetUtils {
//---Implementation---
public static void main(String[] args) {
Multiset < String > multiset = new Multiset();
multiset.add("Sachin");
multiset.add("AB de Villiers", 2);
multiset.addAll("Ind", "Aus");
multiset.addAll(Arrays.asList("USA", "Pak", "Indo", "swiss"));
System.out.println(multiset);
multiset.remove("AB de Villiers");
multiset.remove("Pak", 2);
System.out.println(multiset);
multiset.setCount("Tokyo", 2);
multiset.setCount("France", 3);
multiset.setCount("Belgium", 4);
multiset.setCount("Aus", 0);
System.out.println(multiset);
}
}

Add a comment
Know the answer?
Add Answer to:
Please provide a solution to "Data Structures and Problem Solving Using Java (4th Edition)," Chapter 6,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "The Problem...

    Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "The Problem Statement is as follows": In a breadth-first traversal of a binary tree, the nodes are visited in an order prescribed by their level. First visit the node at level 1, the root node. Then visit the nodes at level 2, in left-to-right order, and so on. You can use a queue to implement a breadth-first traversal of a binary tree". Algorithm for Breath-First Traversal...

  • Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "T...

    Data Structures(2nd Edition Using JAVA Chapter Review, Problem 6PP does not provide an answer: "The Problem Statement is as follows": In a breadth-first traversal of a binary tree, the nodes are visited in an order prescribed by their level. First visit the node at level 1, the root node. Then visit the nodes at level 2, in left-to-right order, and so on. You can use a queue to implement a breadth-first traversal of a binary tree". Algorithm for Breath-First Traversal...

  • Book - Data abstraction and problem-solving with Java - Walls and Mirrors 3rd edition Language -...

    Book - Data abstraction and problem-solving with Java - Walls and Mirrors 3rd edition Language - Java 1) [100] Write an array-based implementation of a Queue (generic version) as the data structure and the Queuelnterface defined on page 415 of the textbook. Also use the class QueueException defined on the same page to catch all appropriate exceptions. To test you program, create a separate test class and add 15 unique integers to the queue. Create the following methods in the...

  • Hi, I don't quite understand the solution to Chapter 10, Problem 62C of Data Structures and...

    Hi, I don't quite understand the solution to Chapter 10, Problem 62C of Data Structures and Algorithms in Java (6th Edition). The question is to design a variation of binary search for performing the get(k) operation on a sorted search table that includes duplicates. But what happens in step 1? is it a search for the first key value of k? In step 2, I don't understand what is being backtraced? The table? And why? In step 3, why are...

  • Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I...

    Java (8th Edition) Chapter 6, Problem 12PGP Looking for a solution to this question. Edit: I was missing the question.. Create a GUI or JavaFX application that displays a button with the text "Button 1". Underneath the button add a label with the text "label 1". Repeat with and additional "Button 2" and "Label 2". Add an image icon of your choice to the first button and the first label.

  • Chemistry (4th Edition) Chapter 6, Problem 141QGWA Bookmark Show all steps ON Problem Classify each process...

    Chemistry (4th Edition) Chapter 6, Problem 141QGWA Bookmark Show all steps ON Problem Classify each process as endothermic or exothermic. What is the sign of AH for each process? Explain your answers. a gasoline burning in an engine b. steam condensing on a mirror c. water boiling in a pot Provide at least two additional examples of exothermic processes and two examples of endothermic processes. Have each member of exampl

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • Please help with a step by step solution for homework problem for chapter 16 . The...

    Please help with a step by step solution for homework problem for chapter 16 . The textbook I am using is Fundamental of Cost Accounting sixth edition. My question is in reference to exercise 16-65. Part A and Part B. The answer I submitted was incorrect McDormand, Inc reported a $3,200 unfavorable price variance for variable overhead and a $32,000 unfavorable price variance for fixed overhead. The flexible budget had $1,080,000 variable overhead based on 36,000 direct labor hours; only...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT