Hi i have a java class called City and in this class i have this.

public class City {
	/**
	 * read the file and make 10 objects
	 * 
	 */
	
	private String name;
	private double population;
	public City(String name, double population) {
		super();
		this.name = name;
		this.population = population;
	}
	public String getName() {
		return name;
	}
	public double getPopulation() {
		return population;
	}
	

}

I am then making another class called CityTester that will test the class and i want to read a file that i have that has 10 different names and populations of city's, with each name/pop on one line each. i am then reading the file and making 10 different objects from the 10 different names. what i am having trouble with is i want to compare these 10 City objects and put them from smallest to largest(based on population) and then print them out. i know that you cant use Arrays.sort since it will only compare the one City array object. so does anyone have any suggestions on how i should go on reading/comparing the file?
thank you :)

Recommended Answers

All 7 Replies

i know that you cant use Arrays.sort since it will only compare the one City array object

What do you mean by that? Aren't you going to save all the City objects into one array?

City [] cities = new City[10];

Arrays.sort(cities);

Maybe I am missing something. Can you describe better where are you having problem

The above would work however Arrays.sort(cities); would sort the City objects by their natural ordering as quoted in the Javadocs and not as per the "population" of those individual objects.

So he would still have to either implement the Comparable interface in his City class so that the natural ordering is done as per the population field or he would have implement his own Comparator and use this variant of the sort() method.

Also the O.P would have to do the same using the PriorityQueue in the "util" package too.

why using a double for the population instead of Integer or BigInt?
think it is possible to have a 0.5 person walking around in there?

The data structure that would fit the bill here appears to be a priority queue (with population determining the priority).
You could either implement it by hand yourself or use the implementation that comes in the "util" package.

Hi thanks i looked at the documentation for the Interface Comparable but how would i implement this in my class? I have never worked with Interfaces before.

This should help you start up.
But if you haven't worked with interfaces then you must be just starting Java Programming, I would suggest you rather implement your own PrioritQueue backed with a LinkedList rather than let the Java collections library spoil you.
That way you will develop your logical thinking ability, however in the real world projects (production systems / live projects) when you are thorough with the concepts of Java you should use collections instead of implementing on your own.

Sort it yourself, even use a BubbleSort if you want, since its a small collection of cities. I used a PriorityQueue for one of my projects before and it isn't hard to implement the default Java class for it, but you'll learn more doing it yourself probably.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.