Han

Han Ferik

Home | Projects | AP CSA

Project City Order

Tools: VSCode | Source Code: GitHub

The City program is designed to compare two city names, determining if they are the same or which one comes first alphabetically. The program is encapsulated within a City class, which contains two private instance variables: citynameone and citynametwo. These variables store the names of the two cities, and their values are initialized through a constructor that takes two parameters, allowing for easy object creation with specific city names.

Within the City class, there are getter methods named getCityOne() and getCityTwo(). These methods serve the purpose of returning the names of the first and second cities, respectively. The core functionality of the program is in the compareCities() method, which takes two city names as arguments. This method uses the equals() method to check if the two city names are identical. If they are not the same, it employs the compareTo() method to compare the two names lexicographically. Depending on the results of this comparison, the program prints a message indicating whether the two cities are the same or which one comes first alphabetically.

The main method serves as the entry point for the program, where a City object is instantiated with the names "New York" and "Los Angeles." After creating the object, the program invokes the compareCities() method to perform the comparison and output the result.

Overall, this program illustrates fundamental concepts of object-oriented programming in Java, such as class definition, object instantiation, and method creation. It also emphasizes string manipulation techniques, specifically using equals() and compareTo() for string comparisons. This project reinforces the importance of encapsulation and demonstrates how to organize code effectively while performing comparisons on city names.


City Comparison

City Comparison in Java


		public class City {
		    private String citynameone;
		    private String citynametwo;
		
		    public City(String namecityone, String namecitytwo) {
		        this.citynameone = namecityone;
		        this.citynametwo = namecitytwo;
		    }
		
		    public String getCityOne() {
		        return citynameone;
		    }
		
		    public String getCityTwo() {
		        return citynametwo;
		    }
		
		    public void compareCities(String City1, String City2) {
		        citynameone = City1;
		        citynametwo = City2;
		
		        if (citynameone.equals(citynametwo)) {
		            System.out.println("City one and City two are the same");
		        } else if (citynameone.compareTo(citynametwo) < 0) {
		            System.out.println(citynametwo + " comes after " + citynameone + " alphabetically");
		        } else if (citynameone.compareTo(citynametwo) > 0) {
		            System.out.println(citynameone + " comes before " + citynametwo + " alphabetically");
		        }
		    }
		
		    public static void main(String[] args) {
		        City city = new City("New York", "Los Angeles");
		        city.compareCities(city.getCityOne(), city.getCityTwo());
		    }
		}
		    

Find me on the interwebs!

Github LinkedIn Instagram Facebook