1

I am trying to make a new method that tells the user what the country with the highest point out of my array is. What I have currently done is inputted 2 country names followed by their highest point name and the number of the highest point, but now I am trying to output the one country that has the indefinite highest point, in my case from what i've added, its Argentina with Aconcagua as its highest point as 6960.

Code: Main file:


public class continentTest
{
        public static void main(String[] args) {
        Continent southAmerica = new Continent();
        Country southAmericanRepublic = new Country("Argentina", new HighestPoint("Aconcagua", 6960));
        southAmerica.addCountry(southAmericanRepublic);
        Country anotherSouthAmericanRepublic = new Country("Columbia", new HighestPoint("Pico Cristóbal Colón",5730));
        southAmerica.addCountry(anotherSouthAmericanRepublic);
        
        

        System.out.println (southAmerica.toString());
        
        }
}

Other files:

class Country {
    String name;
    HighestPoint hp;
    public Country (String nm, HighestPoint pt) {
        name = nm;
        hp = pt;
    }
    public String toString () {
        return name + ": " + hp.toString() + "\n";
    }
}
class HighestPoint {
    String name;
    int height;
    public HighestPoint (String nm, int ht) {
        name = nm;
        height = ht;
    }
    public String toString () {
        return name + " " + String.valueOf (height);
    }
    
}
import java.util.*;

class Continent {
    ArrayList<Country> countries;
    public Continent () {
        countries = new ArrayList<Country>();
    }
    public void addCountry (Country c) {
        countries.add (c);
    }
    public String toString () {
        String s = "";
        for (Country c : countries)
            s += c.toString();
        return s;
    }
}

I am not quite sure how to take the largest value from an array and display it along with the country name. Any help would be appreciated, thanks!

3 Answers3

3

The following method in the continent class may help:

public Country getHighestPoint() {
    int highest = 0;
    Country temp;
    for(int index = 0; index < countries.size(); index++) {
        if(countries.get(index).hp.height > highest) {
            highest = countries.get(index).hp.height
            temp = countries.get(index)
        }
    }
    return temp;
}
2

This exercise is a good opportunity to learn about the Comparable and Comparator

Starting with Comparable, you should apply this to your HighestPoint

class HighestPoint implements Comparable<HighestPoint> {
    String name;
    int height;
    public HighestPoint (String nm, int ht) {
        name = nm;
        height = ht;
    }
    public String toString () {
        return name + " " + String.valueOf (height);
    }
    public int compareTo(HighestPoint hp) {
      return height - hp.height;
    }
    
}

Now that's done, you can compare two HighestPoints and determine which is bigger.

Next: Comparator. We can use this with Continent, as you have a Collection (ArrayList) of all the Countries in a Content.

class Continent {
  //... keep what is already in Continent
  Comparator countryComparator = new Comparator<Country> () {
    public int compare(Country a, Country b) {
      return a.highestPoint.compareTo(b.highestPoint);
    }
  }
}

Now we can compare Countries and sort the array list by their HighestPoint

The reason it makes sense to use Comparable with HighestPoint and Comparator with your Countries array is that HighestPoint is a class defined with two data points: A name and a height. Whereas a Country could have many data points, and you could have many Comparators to sort Countries based on different criteria

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • I've tried implementing this into my code but it gives me 19 compilinge errors, mostly class, interface, or enum expected related, or cannot find symbol etc. I appreciate the help though and will look into comparators! – WanttoBeScripter147 Dec 11 '20 at 13:52
  • my apologies, after rerunning, the only issue I find is that a ";" is expected, but I am not too sure where? – WanttoBeScripter147 Dec 11 '20 at 14:19
  • @WanttoBeScripter147 there was a problem in my code before "Compare(Country a" I had forgotten to include visibility and return type. Add that and see if it helps – ControlAltDel Dec 11 '20 at 14:23
1

You can solve it with Collections.

Country countryWithHighestPoint = Collections.max(southAmerica.countries, Comparator.comparing(s -> s.hp.height));

continentTest

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

public class continentTest {

    public static void main(String[] args) {

        Continent southAmerica = new Continent();

        Country southAmericanRepublic = new Country("Argentina", new HighestPoint("Aconcagua", 6960));

        southAmerica.addCountry(southAmericanRepublic);

        Country anotherSouthAmericanRepublic = new Country("Columbia", new HighestPoint("Pico Cristóbal Colón", 5730));

        southAmerica.addCountry(anotherSouthAmericanRepublic);
                
        Country countryWithHighestPoint = Collections.max(southAmerica.countries, Comparator.comparing(s -> s.getHighestPoint().getHeight()));
        System.out.println(countryWithHighestPoint.toString());
                
        System.out.println(southAmerica.toString());
    }
}

Country

class Country {
    
    private String name;
    private HighestPoint hp;
    public Country (String nm, HighestPoint pt) {
        name = nm;
        hp = pt;
    }
    public String toString () {
        return name + ": " + hp.toString() + "\n";
    }
    
    public HighestPoint getHighestPoint()
    {
        return hp;
    }
}

HighestPoint

class HighestPoint {
    
    private String name;
    private int height;
    
    public HighestPoint (String nm, int ht) {
        name = nm;
        height = ht;
    }
    public String toString () {
        return name + " " + String.valueOf (height);
    }
    
    public int getHeight()
    {
        return height;
    }    
}
tataelm
  • 679
  • 8
  • 17
  • Do I have to import a comparator? When adding comparators and trying to run this, I get errors in my different classes that says it is unable to be accessed – WanttoBeScripter147 Dec 11 '20 at 14:03
  • import java.util.Collections; import java.util.Comparator; are necessary – tataelm Dec 11 '20 at 14:06
  • Interesting, thanks! I have changed the Country countryWithHighestPoint line, but now I have to create a new country method in a different class? As well as the classes apparently can't be accessed – WanttoBeScripter147 Dec 11 '20 at 14:08
  • I didn't modify any other class except 2 getters. Check my edit on the answer. Whole continentTest class should look like that. Did you maybe change your other classes after you posted the question? – tataelm Dec 11 '20 at 14:11
  • Nothing changed, it is just saying that I should make a new method in my "country" file based off of getHighestPoint. It then says the same for getHeight – WanttoBeScripter147 Dec 11 '20 at 14:15
  • This doesn't make any sense. I'm putting the other classes in my answer too, just in case. – tataelm Dec 11 '20 at 14:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225829/discussion-between-tataelm-and-wanttobescripter147). – tataelm Dec 11 '20 at 14:20