Tools: VSCode | Source Code: GitHub
The compareMethod program is designed to compare three cities by their names and populations. The program utilizes two main methods, compareToByName and compareToByPopulation, to perform these comparisons. The first method compares the cities based on their names in alphabetical order, while the second method compares their populations to identify which city has the largest population. The program operates by creating instances of cities and then calling these comparison methods to determine the results.
The class compareMethod has two instance variables, name and population, representing the name and population of each city. In the constructor, the program initializes the city object with its name and population, allowing the program to handle data for individual cities.
The compareToByName method takes three city names as parameters and compares them using the compareTo() method, which is a built-in Java method that compares strings lexicographically (alphabetically). Depending on the outcome of the comparison, the program determines which city name comes first in alphabetical order. The comparisons are performed between the first city and the second city, the first city and the third city, and the second city and the third city. Based on the results of these comparisons, the method prints the city that appears first in alphabetical order.
The compareToByPopulation method compares the populations of the three cities to determine which one has the largest population. It uses boolean variables to check if one city's population is greater than another's and then makes a decision about the order of the populations. Depending on the outcome, the method prints which city has the largest population. The logic considers various scenarios where one city's population could be larger than the others, and it prints the city with the highest population in increasing order.
In the main method, three city objects are created using the compareMethod constructor: New York, Los Angeles, and Chicago, each with their respective population values. The program then calls the compareToByName method to compare the names of these cities alphabetically and prints which city comes first. Following that, it calls the compareToByPopulation method to compare the populations and determine which city has the largest population.
Overall, this program demonstrates a clear understanding of how to use Java's compareTo() method for string comparisons and boolean logic for numerical comparisons. The program also showcases the use of constructors for creating objects and methods for comparing attributes of those objects. Through this project, I practiced using string comparison, boolean logic for conditions, and organizing code into methods to handle specific tasks related to comparing cities based on their attributes.
public class CompareMethod {
private String name;
private int population;
public CompareMethod(String name, int population) {
this.name = name;
this.population = population;
}
public void compareToByName(String cityNameOne, String cityNameTwo, String cityNameThree) {
int comparisonOne = cityNameOne.compareTo(cityNameTwo);
int comparisonTwo = cityNameOne.compareTo(cityNameThree);
int comparisonThree = cityNameTwo.compareTo(cityNameThree);
if (comparisonOne < 0 && comparisonTwo < 0) {
System.out.println(cityNameOne + " is the first in alphabetical order");
} else if (comparisonOne > 0 && comparisonTwo > 0) {
System.out.println(cityNameThree + " is the first in alphabetical order");
} else if (comparisonThree > 0 && comparisonTwo < 0) {
System.out.println(cityNameTwo + " is the first in alphabetical order");
}
}
public void compareToByPopulation(int cityPopOne, int cityPopTwo, int cityPopThree) {
boolean isCityOneGreater = cityPopOne > cityPopTwo;
boolean isCityTwoGreater = cityPopTwo > cityPopThree;
boolean isCityOneGreaterThanThree = cityPopOne > cityPopThree;
if (isCityOneGreater && isCityOneGreaterThanThree) {
System.out.println("The city with population " + cityPopOne + " is the highest in increasing order");
} else if (isCityOneGreaterThanThree == false && isCityTwoGreater) {
System.out.println("The city with population " + cityPopThree + " is the highest in increasing order");
} else if (isCityOneGreaterThanThree == false && isCityOneGreater) {
System.out.println("The city with population " + cityPopTwo + " is the highest in increasing order");
}
}
public static void main(String[] args) {
CompareMethod city1 = new CompareMethod("New York", int 8500000);
CompareMethod city2 = new CompareMethod("Los Angeles", int 4000000);
CompareMethod city3 = new CompareMethod("Chicago", int 2700000);
city1.compareToByName(city1.name, city2.name, city3.name);
city1.compareToByPopulation(city1.population, city2.population, city3.population);
}
}