#I have problem completing the sort method in this code
7 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @green snow! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
this is the method public ArrayList<Cohort> sortMethod(boolean asc, String attr) ```
{
ArrayList<Cohort> sorted = new ArrayList<Cohort>(cohorts);
return sortMethod(sorted, 3, asc, attr);
}
/**
* This method should use specified sort approach to rearrange
* the references in the ArrayList 'cohorts' such that they are in
* order according to the attr (attribute) parameter.
* If asc is true, this should be ascending order,
* if asc is false, this should be descending order.
*
* You should not modify this code.
*
* @param list The cohort list to be sorted
* @param block_size The size of the blocks
* @param asc True if the list should be ascending order, false for descending
* @param attr Attribute (name or code) that will be use during the sorting
* @return The ArrayList 'cohorts' that has been sorted
*/
protected ArrayList<Cohort> sortMethod(ArrayList<Cohort> list, int block_size, boolean ascending, String attr)
{
// TODO
int size = list.size();
if(size<=1){
return list;
}
//we will divide the given arraylist by the given block sizes
//which is 3 in this case which means this arraylist should be divided
//into blocks of 3
} ```
This message has been formatted automatically. You can disable this using /preferences.
this is the cohort class
public class Cohort ```
{
private Module module; // internal code of the module eg. 5246
private BinaryTree btree; // binary tree that stores the students enrolled in this module
private Professor prof; // Professor teaching this module
public Cohort(Module m, Professor p) {
this.module = m;
this.btree = new BinaryTree(); // empty
this.prof = p;
}
public Cohort(Module m, BinaryTree bt, Professor p) {
this.module = m;
this.btree = bt;
this.prof = p;
}
// getters and setters!
public Module getModule() {
return module;
}
public BinaryTree getBtree() {
return btree;
}
public void setBtree(BinaryTree btree) {
this.btree = btree;
}
public Professor getProf() {
return prof;
}
public void setProf(Professor prof) {
this.prof = prof;
}
public void addStudent(Student s)
{
btree.addNode(s);
s.addModule(module);
}
/**
* Walk tree method
*
* @param type The type of the traversal - inOrder, preOrder, postOrder, or ownOrder
* @return A String with the names of all Students in the tree
*/
public String walkTree(String type)
{
return btree.walkTree(type);
}
/**
* Find a student in the binary tree using the name
* @param name The name of the student you are searching for
* @return A reference to the Student that was found or null if no Student found
*/
public Student find(String name)
{
return btree.find(name);
}
/**
* Method for printing the tree
*/
public void describeStudentTree()
{
btree.printTree();
}
/**
* Get a description of the Module as a String
*/
public String toString() {
return "Class: " + module.getCode() + " - " + module.getName();
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
and this is the module class import java.util.ArrayList;
/**
- Module - A representation of the Module.
- ATTENTION: You do NOT need to modify this code at all.
*/
public class Module ```
{
private int code; // internal code of the module eg. 5246
private String name; // name of the module
private ArrayList<Module> prerequisites; // the list of prerequisites Modules
/**
* Constructor
* @param c internal code
* @param n name
*/
public Module(int c, String n) {
this.code = c;
this.name = n;
this.prerequisites = new ArrayList<Module>();
// it is not important because every module doent have a prerequisite
}
/**
* Constructor
* @param c internal code
* @param n name
* @param pre prerequisite modules
*/
public Module(int c, String n, ArrayList<Module> pre) {
this.code = c;
this.name = n;
this.prerequisites = pre;
}
// getters and setters!
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Module> getPrerequisites() {
return prerequisites;
}
public void setPrerequisites(ArrayList<Module> pre) {
this.prerequisites = pre;
}
public void addPrerequisites(Module m) {
this.prerequisites.add(m);
}
public void removePrerequisites(Module m) {
this.prerequisites.remove(m);
}
/**
* Get a description of the Module as a String
*/
public String toString() {
return "Module: " + code + " - " + name;
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
i should complete the sort method according to these instructions
for example:
Module b,module z,module x,module c,module a,module m, module d, module g,module e
now this should be separated into blocks of 3 like:
module b,module z,module x
module c,module a, module m
module d,module g ,module e
then each block will get sorted like :
module b,module x,module z
module a,module c,module m
module d,module e,module g
then it will merge and then the final arraylist will be like:
module a,module b,module c,module d, module e, module g,module m,module x,module z
all this should happen in insertion sort