Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I began the code but am having trouble to finish it! // write a method that sort

ID: 3736918 • Letter: I

Question

I began the code but am having trouble to finish it!

// write a method that sorts the policies by customer number
   // this one is tougher since you can not use the Collections.sort() method
   // so you need to just slug out some code.
   // Look at the bubble sort from the SortByHand in the search_sort package
   // You will want to do something similar
   // Here is some pseudocode to help
   //

   public static void sortCustNum(ArrayList<Insurance> insure) {

       {

           for (int out = insure.size() - 1; out > 1; out--)
               for (int in = 0; in < out; in++) {
                   // get the first insurance policy
                   Insurance first = insure.get(in);
                   // get the customer from that insurance policy
                   Customer cFirst = first.getCustomer();
                   // get the customer number from that insurance policy
                   Customer idFirst = cFirst.getId();

                   // get the second insurance policy
                   Insurance second = insure.get(in+1);
                   // get the customer from that insurance policy
                   Customer cSecond = second.getCustomer();
                   // get the customer number from that insurance policy
                   Customer idSecond = cSecond.getId();

                   // We want to check to see if the second customer number is
                   // less than the first one

                   // NOTE: When comparing customer numbers:
                   // SortByHand uses Strings so it uses the compareTo()
                   // method.
                   // We are comparing integers so we can just use <

                   // if the second customer number is less than the first one
                   // swap the two insurance policies in the original "insure"
                   // ArrayList
                   // check out the SortByHand to see how to swap.
                   String name1 = first.getLast()+first.getFirst();
                   String name2=second.getLast()+second.getFirst();
                  
                   if (name2.compareTo(name1)<0)
                   {
                       c.set(in,second);
                        c.set(in+1,first);
                   }
                  
                  
                  

               }
       }
   }

Explanation / Answer

You are sorted with name of customer name... You dont't need to sort with customer name. Just check with customer Id it means idFirst<idSecond, if it is true swap elements that's it...

public static void sortCustNum(ArrayList<Insurance> insure) {

{

for (int out = insure.size() - 1; out > 1; out--)

for (int in = 0; in < out; in++) {

// get the first insurance policy

Insurance first = insure.get(in);

// get the customer from that insurance policy

Customer cFirst = first.getCustomer();

// get the customer number from that insurance policy

Customer idFirst = cFirst.getId();

// get the second insurance policy

Insurance second = insure.get(in + 1);

// get the customer from that insurance policy

Customer cSecond = second.getCustomer();

// get the customer number from that insurance policy

Customer idSecond = cSecond.getId();

// We want to check to see if the second customer number is

// less than the first one

// NOTE: When comparing customer numbers:

// SortByHand uses Strings so it uses the compareTo()

// method.

// We are comparing integers so we can just use <

// if the second customer number is less than the first one

// swap the two insurance policies in the original "insure"

// ArrayList

// check out the SortByHand to see how to swap.

// String name1 = first.getLast() + first.getFirst();

// String name2 = second.getLast() + second.getFirst();

if (idFirst<idSecond) {

c.set(in, second);

c.set(in + 1, first);

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote