
Concept explainers
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 nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- USING JAVAarrow_forwardimport java.util.Scanner; public class LabProgram { public static Roster getInput(){ /* Reads course title, creates a roster object with the input title. Note that */ /* the course title might have spaces as in "COP 3804" (i.e. use nextLine) */ /* reads input student information one by one, creates a student object */ /* with each input student and adds the student object to the roster object */ /* the input is formatted as in the sample input and is terminated with a "q" */ /* returns the created roster */ /* Type your code here */ } public static void main(String[] args) { Roster course = getInput(); course.display(); course.dislayScores(); }} public class Student { String id; int score; public Student(String id, int score) { /* Student Employee */ /* Type your code here */ } public String getID() { /* returns student's id */…arrow_forwardIn Java, please. Complete the Course class by implementing the findStudentHighestGpa() method, which returns the Student object with the highest GPA in the course. Assume that no two students have the same highest GPA. Given classes: Class LabProgram contains the main method for testing the program. Class Course represents a course, which contains an ArrayList of Student objects as a course roster. (Type your code in here.) Class Student represents a classroom student, which has three fields: first name, last name, and GPA. (Hint: getGPA() returns a student's GPA.) Note: For testing purposes, different student values will be used. Ex. For the following students: Henry Nguyen 3.5 Brenda Stern 2.0 Lynda Robison 3.2 Sonya King 3.9 the output is: Top student: Sonya King (GPA: 3.9) LabProgram.java import java.util.Scanner; public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); Course course = new Course(); int…arrow_forward
- PRACTICE CODE import java.util.TimerTask;import org.firmata4j.ssd1306.MonochromeCanvas;import org.firmata4j.ssd1306.SSD1306;public class CountTask extends TimerTask {private int countValue = 10;private final SSD1306 theOledObject;public CountTask(SSD1306 aDisplayObject) {theOledObject = aDisplayObject;}@Overridepublic void run() {for (int j = 0; j <= 3; j++) {theOledObject.getCanvas().clear();theOledObject.getCanvas().setTextsize(1);theOledObject.getCanvas().drawString(0, 0, "Hello");theOledObject.display();try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}theOledObject.clear();theOledObject.getCanvas().setTextsize(1);theOledObject.getCanvas().drawString(0, 0, "My name is ");theOledObject.display();try {Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}while (true) {for (int i = 10; i >= 0; i--)…arrow_forwardIn Java: -Using a BST Interface (Binary Search Tree) -Create a class Disk. Disk contains four fields: model (String), size (int), measure (String, for GB or TB), and performance index (int). -Disk needs a constructor to initialize all fields, a toString method, and an equals. Make the class Comparable and write a compareTo method that compares Disks based on the size of the disk. Make sure your comparison accounts for the fact that some numbers are in GB and some are in TB. Write a method that will return true if the disk is the same size as the parms; parms are the size and measure("GB" or "TB"). Also write a method to create a Comparator which compares Disks based on the performance rating. -The client code should do the following: Write a function called build, which will open the disks.txt file, read in each disk and add it to the BST. For each disk the model is on the first line, and the second line contains the size, measure, and performance index. The BST is passed to build as…arrow_forwardwrite a java class definition 4. Give the class definition of ArrayChecker which has an integer array variable arr. This has 2 methods a. Constructor which take an integer parameter numElements and reads all the numElements from the user using Scanner class into arr. b. public boolean checkConsecutiveValues (int n), which checks whether arr has atleast n consecutive elements with the same values. (it is enough if it returns the first occurrence of n consecutive elements) (Write only the class definition of ArrayChecker) Eg. 12345555 10, if n = 4, this has four consecutive 5s, return true 10 12 111 11456666. If n = 3, this has 3 consecutive 1s, return true. 10 12 111 11456666. If n = 7, then return falsearrow_forward
- Write a Java class to calculate temperature conversion to degree. O celcius = 32 degree, 0 Kelvin = -273.15 degree, write a method to take temp type and number and convert them in degree. Have to utilize java map. Your answer 4 2 +arrow_forwardWrite a Java program that expands a given binomial (x + y)^n, where integer n is user input. To do the work of the binomial expression, first create a method that accepts n as a parameter and then returns an array holding the coefficients needed for the binomial expansion using the Pascal’s Triangle method. Create a 2nd method which takes the array holding the coefficients as a parameter and prints the appropriate binomial expansion. For example, if n = 5 is entered by the user, the method calculating the coefficients should return {1,5,10,10,5,1} and the method that prints the expansion should print the following: (x + y)^5 = x^5 + 5x^4y + 10x^3y^2 + 10x^2y^3 + 5xy^4 + y^5 Your main method should use an appropriate loop for repeated inputs and automatically call the methods to calculate the coefficients and print the binomial expansion. There isn’t a need for a menu although you should ask the user if they want to quit or continue after their binomial expansion is printed each time.…arrow_forward
- 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





