Use java.util.Map, java.util.Set and given class Parition.java, write a Java method that receives a positive integer and prints all of its partitions. For example, if the input is 5, it prints the following seven lines, each representing one partition of integer 5 Please can you help with the following activity? Use java. util.Map, java. util.Set and given class Parition.java: import java.util.*; public class Partition implements Comparable { /*represents a partition for int sum. * Example: * if sum = 14, * frequency can be { * 1:1, 3:1, 5:2} * which means 14 = 5 + 5 + 3 + 1 * */ public int sum; public String rep = ""; public TreeMap frequencies; Partition(){//default constructor, parition for sum = 1 frequencies = new TreeMap<>(); this.sum = 1; frequencies.put(1, 1); } Partition(int sum){ frequencies = new TreeMap<>(); this.sum = sum; } public String toString(){ if(!rep.isEmpty()) return rep; StringBuilder rv = new StringBuilder(); for(int i: frequencies.descendingKeySet()) for(int j = 0; j < frequencies.get(i);j++) rv.append((rv.length() == 0?"":"+") + i); rep = rv.toString(); return rep; } public boolean equals(Object o){ if(o instanceof Partition) return this.toString().equals(o.toString()); return false; } public int hashCode(){ return this.toString().hashCode(); } @Override public int compareTo(Partition o) { Iterator it1 = frequencies.descendingKeySet().iterator(); Iterator it2 = o.frequencies.descendingKeySet().iterator(); while(it1.hasNext() && it2.hasNext()){ int key1 = it1.next(), key2 = it2.next(); if(key1 != key2) return key2 - key1; int val1 = frequencies.get(key1), val2 = o.frequencies.get(key2); if(val1 != val2) return val2 - val1; } if(it1.hasNext()) return -1; else if(it2.hasNext()) return 1; else return 0; } } write a Java method that receives a positive integer and prints all of its partitions. For example, if the input is 5, it prints the following seven lines, each representing one partition of integer 5: 5 4+1 3+2 3+1+1 2+2+1 2+1+1+1 1+1+1+1+1 The java program will print the result with the plus sign and the way the numbers appear above. I would need it tomorrow Saturday, July 1st, 2023. Thank you so much.
Use java.util.Map, java.util.Set and given class Parition.java, write a Java method that receives a positive integer and prints all of its partitions. For example, if the input is 5, it prints the following seven lines, each representing one partition of integer 5
Please can you help with the following activity?
Use java. util.Map, java. util.Set and given class Parition.java:
import java.util.*;
public class Partition implements Comparable<Partition> {
/*represents a partition for int sum.
* Example:
* if sum = 14,
* frequency can be {
* 1:1, 3:1, 5:2}
* which means 14 = 5 + 5 + 3 + 1
* */
public int sum;
public String rep = "";
public TreeMap<Integer,Integer> frequencies;
Partition(){//default constructor, parition for sum = 1
frequencies = new TreeMap<>();
this.sum = 1;
frequencies.put(1, 1);
}
Partition(int sum){
frequencies = new TreeMap<>();
this.sum = sum;
}
public String toString(){
if(!rep.isEmpty())
return rep;
StringBuilder rv = new StringBuilder();
for(int i: frequencies.descendingKeySet())
for(int j = 0; j < frequencies.get(i);j++)
rv.append((rv.length() == 0?"":"+") + i);
rep = rv.toString();
return rep;
}
public boolean equals(Object o){
if(o instanceof Partition)
return this.toString().equals(o.toString());
return false;
}
public int hashCode(){
return this.toString().hashCode();
}
@Override
public int compareTo(Partition o) {
Iterator<Integer> it1 = frequencies.descendingKeySet().iterator();
Iterator<Integer> it2 = o.frequencies.descendingKeySet().iterator();
while(it1.hasNext() && it2.hasNext()){
int key1 = it1.next(), key2 = it2.next();
if(key1 != key2)
return key2 - key1;
int val1 = frequencies.get(key1), val2 = o.frequencies.get(key2);
if(val1 != val2)
return val2 - val1;
}
if(it1.hasNext())
return -1;
else if(it2.hasNext())
return 1;
else
return 0;
}
}
write a Java method that receives a positive integer and prints all of its partitions. For example, if the input is 5, it prints the following seven lines, each representing one partition of integer 5:
5
4+1
3+2
3+1+1
2+2+1
2+1+1+1
1+1+1+1+1
The java program will print the result with the plus sign and the way the numbers appear above. I would need it tomorrow Saturday, July 1st, 2023. Thank you so much.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images