Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Exercise# 1 : Implement Sorted ADT Table You are given a partially implemented class Table that manages an array of City objects. Each City object
Exercise#: Implement Sorted ADT Table
You are given a partially implemented class Table that manages an array of City objects. Each City object
contains information about a city, including its name which acts as the search key country, and population.
The City class extends an abstract class KeyedItem which provides a method to get the search key.
Your task is to complete the implementation of the Table class by adding the following methods:
isEmpty Method:
Should return true if the table is empty, false otherwise.
length Method:
Should return the number of cities currently in the table.
insert Method:
Should insert a new city into the table.
Ensure no duplicate cities based on the city name are inserted.
Maintain the cities in sorted order by city name after each insertion.
traverse Method:
Should print all cities in the table in sorted order.
Complete the Table class by implementing the methods described above. Use the provided City and
KeyedItem classes.
import java.util.Arrays;
public class Table
private City cities;
private int size;
public Tableint maxSize
cities new CitymaxSize;
size ;
Implement this method
public boolean isEmpty
Implement this method
public int length
Implement this method
public void insertCity newCity
Implement this method
public void traverse
class City extends KeyedItem
private String country;
private int population;
public CityString city, String country, int population
supercity;
this.country country;
this.population population;
@Override
public String toString
return getKey country population;
abstract class KeyedItem
private KT searchKey;
public KeyedItemKT key
searchKey key;
public KT getKey
return searchKey;
Exercise#: Implement Sorted ADT Table
You are given a partially implemented class Table that manages an array of City objects. Each City object
contains information about a city, including its name which acts as the search key country, and population.
The City class extends an abstract class KeyedItem which provides a method to get the search key.
Your task is to complete the implementation of the Table class by adding the following methods:
isEmpty Method:
Should return true if the table is empty, false otherwise.
length Method:
Should return the number of cities currently in the table.
insert Method:
Should insert a new city into the table.
Ensure no duplicate cities based on the city name are inserted.
Maintain the cities in sorted order by city name after each insertion.
traverse Method:
Should print all cities in the table in sorted order.
Complete the Table class by implementing the methods described above. Use the provided City and
KeyedItem classes.
Following the code from Exercise implement the two methods, retrieve and updatePopulation
Implement this method
public City retrieveString searchKey
Implement this method
public void updatePopulationString cityName, int newPopulation
retrieve Method:
This method should search the array of cities for a city with the given name.
If found, it should return the City object.
If not found, it should return null.
updatePopulation Method:
This method should use the retrieve method to find the city by name.
If the city is found, it should update the citys population.
If the city is not found, it should print a message indicating that the city was not found
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started