Answered step by step
Verified Expert Solution
Link Copied!

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#1: 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 Table(int maxSize){
cities = new City[maxSize];
size =0;
}
// Implement this method
public boolean isEmpty(){}
// Implement this method
public int length(){}
// Implement this method
public void insert(City newCity){}
// Implement this method
public void traverse(){}
}
class City extends KeyedItem {
private String country;
private int population;
public City(String city, String country, int population){
super(city);
this.country = country;
this.population = population;
}
@Override
public String toString(){
return getKey()+","+ country +""+ population;
}
}
abstract class KeyedItem>{
private KT searchKey;
public KeyedItem(KT key){
searchKey = key;
}
public KT getKey(){
return searchKey;
}
} Exercise#1: 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 Exercise1, implement the two methods, retrieve and updatePopulation
// Implement this method
public City retrieve(String searchKey){}
// Implement this method
public void updatePopulation(String 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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

What training is required for the position?

Answered: 1 week ago