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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Random access
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education