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

https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e The exe

ID: 3671815 • Letter: H

Question

https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e

The executable program on the last two pages has some sections replaced by question marks. Replace the question marks with code to make it execute correctly.
Just need help with question 1 only. Link to the pages of code
https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e
https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e

The executable program on the last two pages has some sections replaced by question marks. Replace the question marks with code to make it execute correctly.
Just need help with question 1 only. Link to the pages of code
https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e


The executable program on the last two pages has some sections replaced by question marks. Replace the question marks with code to make it execute correctly.
Just need help with question 1 only. Link to the pages of code
https://files.acrobat.com/a/preview/4b434789-f177-4a9d-b42a-c5d83c81276e

Explanation / Answer

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;

/**
* @author Srinivas Palli
*
*/
public class SortMountains {
   LinkedList<Mountain> mtn = new LinkedList<Mountain>();

   /**
   * @author Srinivas Palli
   *
   */
   class NameCompare implements Comparator<Mountain> {
       public int compare(Mountain one, Mountain two) {
           return one.name.compareTo(two.name);
       }
   }

   /**
   * @author Srinivas Palli
   *
   */
   class HeightCompare implements Comparator<Mountain> {
       public int compare(Mountain one, Mountain two) {
           return (two.height - one.height);
       }
   }

   /**
   * main method to call the go method
   *
   * @param args
   */
   public static void main(String[] args) {
       new SortMountains().go();
   }

   /**
   * method to add {@link Mountain} objects to the linked list and apply name
   * and height compares on the list
   */
   public void go() {
       mtn.add(new Mountain("Longs", 14255));
       mtn.add(new Mountain("Elbert", 14433));
       mtn.add(new Mountain("Maroon", 14156));
       mtn.add(new Mountain("Castle", 14265));
       System.out.println("as entered: " + mtn);
       NameCompare nc = new NameCompare();
       Collections.sort(mtn, nc);
       System.out.println("by name: " + mtn);
       HeightCompare hc = new HeightCompare();
       Collections.sort(mtn, hc);
       System.out.println("by height: " + mtn);
   }
}

/**
* @author Srinivas Palli
*
*/
class Mountain {
   String name;
   int height;

   /**
   * @param n
   * @param h
   */
   Mountain(String n, int h) {
       name = n;
       height = h;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   public String toString() {
       return name + " " + height;
   }
}

OUTPUT:

as entered:
[Longs 14255, Elbert 14433, Maroon 14156, Castle 14265]
by name:
[Castle 14265, Elbert 14433, Longs 14255, Maroon 14156]
by height:
[Elbert 14433, Castle 14265, Longs 14255, Maroon 14156]