Review Lab 0, City class coding Submitting work for credit before 1125. Solution
ID: 3876470 • Letter: R
Question
Review Lab 0, City class coding Submitting work for credit before 1125. Solutions will be given on 1/25 The class must implement Comparable interface Class city has three variables: name, state (of type String) and population (of type int) It has the three-variable constructor taking in parameters for publis city (String n, String s, int p) name = n; state population p; Define all three getters and the setter for field population Additionally, implement equals and compareTo methods as follows: RabiS int sompareTe (City st) f int temp = state. compareTo (cts tate); LE (temp != 0) return temp ; else return name compareTo (ct.name); Rublig boolean equals (Object obi) f City ot= (City) obj; return. this.compareTo(et) -0: You need also implement tostring method to provide a meaningful description of a city of known name, state and population.Explanation / Answer
#include <conio.h>
class city
{
void city(string n, string s) //constructor for city to return name and state
{
name=n
state=s
}
int get_name() //getter for city name
public boolean equals(Object obj){
city ct=(city) obj;
return this.compareTo(ct)==0;
}
public int compareTo(city ct){
int temp= state.compareTo(ct.state);
if(temp!=0) return temp;
else return name.compareTo(ct.name);
}
}
}
int main()
{
city c1= new city("abc", "xyz");
c1.set_population(1234567);
city c2= new city("lmn", "abb");
c2.equals(c1);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.