Tino APCS

Lab 17.3 InsertionSort

Create a new class

Create an InsertionSort class in Eclipse.

insertionSort1 method

  1. Add a public static void function named insertionSort1 that takes in an array of ints and sorts the list into ascending order by inserting items into their proper place on the left. This is the same way that Lesson 17 Page D explains InsertionSort.

  2. Test your method by creating several test arrays in the main method and then passing them into your insertion sort function. In order to verify that you are inserting the right elements in the right direction (and to pass the autograder) you should print the array after each completion of the outer loop.

  3. For example, if the starting array was [9, 5, 1, 3, 7, 4], then your method should print:

    [5, 9, 1, 3, 7, 4]
    [1, 5, 9, 3, 7, 4]
    [1, 3, 5, 9, 7, 4]
    [1, 3, 5, 7, 9, 4]
    [1, 3, 4, 5, 7, 9]
    

insertionSort2 method

  1. Add a public static void function named insertionSort2 that takes an array of Strings and sorts the list into descending order by inserting items into their proper place on the right. This means your outer loop should run backward and your inner loop should run forward. Print the list at the end of each outer loop iteration.

    Remember that to compare Strings, you must use the compareTo() function to determine which String is larger or smaller.

    1. Test your method thoroughly.

YelpRating

Both the AP Exam and our APCS class will have you write code on paper. Practice this now by writing the following YelpRating class on paper. Afterward, you will be able to check your work by 'autogenerating' the same thing in Eclipse.

  1. Write a YelpRating class on paper with these properties:
    • A YelpRating implements the Comparable interface
    • A YelpRating has the following instance variables:
      • String subject stores what the review is for (Ex: "airpods," "Chipotle," etc.)
      • String review stores a written review for this rating
      • String userName stores the name of the reviewer
      • double rating stores a rating value between 0 and 5
    • A YelpRating is created by passing in all three attributes to a constructor
    • A YelpRating object compares itself to other YelpRating objects by comparing their ratings

YelpRating in Eclipse

Now let's re-create the YelpRating class and let Eclipse do some of the work for us.

  1. Make a new YelpRating class in Eclipse by choosing new Class from the menu.

    • For the class name, enter YelpRating
    • Next to the Interfaces section, there's an Add button. Click it to add an interface declaration to the class.
    • Then type in Comparable and choose the Comparable interface from the list.
    • After pressing OK, you should see this in the interfaces section:
    • Replace the <T>, which means type, with <YelpRating> so that we only compare YelpRatings with other YelpRatings.
    • Finally, click the Finish button.

  2. Add these instance variables:

    • private String subject (what this review is for)
    • private String review (a written review summary)
    • private double rating (a rating between 0 and 5)
    • private String userName (name of reviewer)

  3. Let Eclipse autogenerate a constructor that accepts all our instance variables:

    • From the menu, select Source → Generate Constructors using Fields.
    • Check all the attribute boxes and set the insertion point as After 'userName'.

  4. Let Eclipse autogenerate getter and setter methods for the instance variables:

    • From the menu, select Source → Generate Getters and Setters.
    • Check all the boxes and set the insertion point to be after the constructor.

  5. In order for YelpRatings to be comparable, you need to complete the compareTo method.

    • Complete the compareTo method so that it compares YelpRatings by their rating.

    The compareTo method requires you to return an integer that represents how much larger the current object is when compared to the parameter object. Normally you would just return the difference in values between the two objects. However, that won't work in this case since the ratings are doubles. For example, a rating of 5.5 compared to 5.6 should return a negative value to indicate that 5.5 is less than 5.6. You can't just typecast the difference and return it because (int)(5.5 - 5.6) would return 0 which would imply the ratings are the same even though they aren't. So, you'll need to think of a different strategy.

  6. In order for YelpRatings to be displayed on the screen we need to override Object's toString() method. In Eclipse, make space for a new method and then type toString and hit Ctrl + Space and hit Enter. Eclipse automatically inserts a toString method stub for you.

    Update the toString method so that it returns a String in the following format (make sure you use the correct spacing - there is a tab between the labels and the values which is why they line up):

    Subject:\tsubject_text\n
    Review:\treview_text\n
    Rating:\trating\n
    User:\tuserName\n
    

    Note: You shouldn't do any printing in toString(). The way toString works is you build up a String and then return it. Your toString() method contains the String that will ultimately be printed by some other code but it doesn't do the printing itself.

    For example, if we created this YelpRating:

    • YelpRating y = new YelpRating("Pieology", "awesome pizza", 5, "Eric F"); then System.out.println(y) would display:
      Subject:    Pieology
      Review:     awesome pizza
      Rating:     5.0
      User:       Eric F
      

Note: If tab does not make them line up perfectly, don't worry about it, just make sure you have exactly one tab between labels and values. Don't add extra spaces to try to make it look right.

insertionSort3 method

Go back to your InsertionSort class and create a public static void insertionSort3 function that takes in an ArrayList of YelpRating objects and sorts the list into descending order by inserting items into their proper place on the left. This means your outer loop will run from left to right and your inner loop will run backward.

Print the list after each iteration of the outer loop

In the testing section of your driver, create some YelpRating objects, add them to an ArrayList, print the list, sort it, then print the list again. The ratings should be displayed in sorted order.

Submission files

Before submitting, rename your files and class names accordingly:

  • PX_LastName_FirstName_InsertionSort
  • PX_LastName_FirstName_YelpRating

Eclipse has a smart rename tool that will do this for you.

  1. Go to your YelpRating class and highlight YelpRating.
  2. Choose Refactor → Rename from the Eclipse menu.
  3. Edit the class name to PX_Last_First_YelpRating. For example, P3_Wang_Michael_YelpRating. Eclipse will automatically rename your class EVERYWHERE it is used. Awesome!
  4. Do the same for your InsertionSort class. Rename it PX_LastName_FirstName_InsertionSort.

You must Sign In to submit to this assignment

Dark Mode

Outline