Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education