Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a class hierarchy (classes and/or interfaces) to support a program dealin

ID: 3827106 • Letter: D

Question

Design a class hierarchy (classes and/or interfaces) to support a program dealing with geographic objects. Support the following classes (at last): Countries States/Provinces Cities Boundary Segments Rivers Support the following operations, where applicable, plus any others that seem useful (arguments have been omitted, but you will have to include them): area() capital() get Cities() get Country() distance() -between cities boundary Length()- total length of boundary neighbors() objects sharing boundaries border of () the countries/state this separates Write out the class definitions, instance variables and mu definitions. Some of the instance variables should probably be various kinds of Collections. You do not need to implement the methods, but you should include comments inside each method that describe the approach you would take (alternately, you can actually implement them -that might be simpler for some methods). Use interfaces and super classes where appropriate. Supply javadoc comments for all classes, interfaces, and methods. The system you write should compile, even if it doesn't actually work (because the methods are just stubs with comments). Identify all of the classes as belonging to the package "geography" and put the java files in a directory called geography, so that javadoc functions properly. Note that this means that the compiler must be run from outside the geography directory, and you should type "javac geograph/* java" say, to compile your code. This is annoying, and one of the reasons I dislike Java's package system. (You would do the thing to run a program in a package, but in this particular case you are not writing a program, so the issue won't come up.)

Explanation / Answer

boundary.java

package geography;

import java.util.ArrayList;

/**
* Boundary Class represents boundaries between other @geographical objects
*
*/
public class boundary {

    private coordinates beginning;
    private coordinates end;
    private static int count = 0;
    private int id;

    ArrayList<geographical> Places;

    /**
     *
     * @param beginning This will mark the coordinates for the beginning of the boundary
     * @param end This will mark the coordinates for the end of the boundary
     */
    boundary(coordinates beginning, coordinates end)
    {
        this.beginning = beginning;
        this.end = end;
        count++;
        this.id = count;
    }

    /**
     *
     * @return Returns distance of boundary using @beginning and @end
     */
    public double boundaryLength()
    {
        double dist;


        int X1 = beginning.getCoordinateX();
        int X2 = end.getCoordinateX();
        int Y1 = beginning.getCoordinateY();
        int Y2 = end.getCoordinateY();


        dist = Math.sqrt(Math.pow((X2 - X1), 2) + Math.pow((Y2 - Y1), 2));

        return dist;
    }

    /**
     *
     * @return getter for Places, returns list of places which use this object as boundary
     */
    public ArrayList<geographical> borderOf()
    {
        return Places;
    }

    /**
     *
     * @param Place Place to be added to this object, used when adding boundary
     */
    void addPlace(geographical Place)
    {
     Places.add(Place);
    }

    /**
     *
     * @return getter for id, used to identify boundary by number
     */
    int getId()
    {
        return id;
    }

}

city.java

package geography;

import java.util.ArrayList;

public class city extends geographical {


    boolean isCapitalCity;
    state State;
    ArrayList<river> Rivers;


    /**
     * city will have -1 in both X and Y coordinates
     */
    city() {
        super();
        isCapitalCity = false;
        Rivers = new ArrayList<>();
    }

    /**
     * @param location create city with coordinates
     */
    city(coordinates location) {
        super(location);
        isCapitalCity = false;
        Rivers = new ArrayList<>();
    }


    /**
     * @param name create a city with just name
     */
    city(String name) {
        super(name);
        isCapitalCity = false;
        Rivers = new ArrayList<>();
    }


    /**
     * @param name     create city with this name
     * @param location create city with this location as @coordinates
     */
    city(String name, coordinates location) {
        super(name, location);
        isCapitalCity = false;
        Rivers = new ArrayList<>();
    }

    /**
     * create city with name, coordinates, and knowledge of it being the capital
     */
    city(String name, coordinates location, boolean isCapitalCity) {
        super(name, location);
        this.isCapitalCity = isCapitalCity;
        Rivers = new ArrayList<>();
    }

    /**
     * Create city with name, state, location, and knowledge of it being the capital
     */
    city(state State, String name, coordinates location, boolean isCapitalCity) {
        super(name, location);
        this.State = State;
        this.isCapitalCity = isCapitalCity;
        Rivers = new ArrayList<>();
    }

    /**
     * create city with all parameters used
     */
    city(state State, String name, coordinates location, boolean isCapitalCity, double area) {
        super(name, location, area);
        this.State = State;
        this.isCapitalCity = isCapitalCity;
        Rivers = new ArrayList<>();
    }


    /**
     *
     * @return Returns distance between city A and B
     */
    public static double distance(city A, city B) {
        double dist;

        geography.coordinates coordinatesA = A.getCoordinates();
        geography.coordinates coordinatesB = B.getCoordinates();
        int X1 = coordinatesA.getCoordinateX();
        int X2 = coordinatesB.getCoordinateX();
        int Y1 = coordinatesA.getCoordinateY();
        int Y2 = coordinatesB.getCoordinateY();


        dist = Math.sqrt(Math.pow((X2 - X1), 2) + Math.pow((Y2 - Y1), 2));

        return dist;
    }

    /**
     *
     * @return returns true if it is the capital of the state it belongs to
     */
    public boolean Capital() {
        return isCapitalCity;
    }

    /**
     *
     * @param isCapitalCity Sets capital true/false
     */
    public void Capital(boolean isCapitalCity) {
        this.isCapitalCity = isCapitalCity;
    }


    /**
     *
     * @return Gets state this city belongs to
     */
    public state getState() {
        return this.State;
    }

    /**
     *
     * @param parentState Set state that this city belogns to
     */
    public void setState(state parentState) {
        this.State = parentState;
    }

    /**
     *
     * @param River Add river to city and city to river.
     */
    public void addRiver(river River) {
        River.addCity(this);
        Rivers.add(River);
    }

    /**
     *
     * @return Get rivers that run through city
     */
    public ArrayList<river> getRivers() {
        return Rivers;
    }


    /**
     *
     * @return Return area
     */
    @Override
    public double area() {
        return super.area();
    }

    /**
     *
     * @param area Sets area to this
     */
    @Override
    public void area(double area) {
        super.area(area);
    }


}

coordinates.java

package geography;

public class coordinates {

    private int coordinateX;
    private int coordinateY;

    /**
     * -1 coordinates means not a known location
     */
    public coordinates() {
        coordinateX = -1;
        coordinateY = -1;
    }

    /**
     * known coordinates
     * @param x x coordinate
     * @param y y coordinate
     */
    public coordinates(int x, int y) {
        coordinateX = x;
        coordinateY = y;
    }

    /**
     *
     * @return Getter for X
     */
    int getCoordinateX() {
        return coordinateX;
    }

    /**
     *
     * @return Getter for Y
     */
    int getCoordinateY() {
        return coordinateY;
    }

    /**
     *
     * @param x Setter for X
     */
    void setCoordinateX(int x) {
        coordinateX = x;
    }

    /**
     *
     * @param y Setter for Y
     */
    void setCoordinateY(int y) {
        coordinateY = y;
    }


}


river.java

package geography;


import java.util.ArrayList;

public class river extends geographical {

    ArrayList<city> cities; //cities that the water flows through

    /**
     * default river object, knows null
     */
    public river()
    {
        super();
    }

    /**
     * River object with name
     * @param name Name of river
     */
    public river(String name)
    {
        super(name);
    }

    /**
     * City list holds all cities river passes through
     * @param City City river passes through
     */
    void addCity(city City)
    {
        cities.add(City);
    }

    /**
     *
     * @return get cities river runs through
     */
    public ArrayList<city> getCities()
    {
        return cities;
    }


}

geographical.java

package geography;


import java.util.ArrayList;

/**
* Parent class for all geographical objects
*/
abstract class geographical {


    private geography.coordinates coordinates;
    private String name;
    private ArrayList<boundary> Boundaries;
    private double area;

    /**
     * Default constructor creates geographical object with null data
     */
    geographical() {
        coordinates = new coordinates();
        name = null;
    }

    /**
     * Constructs geographical with name, location and area
     * @param name store into private name
     * @param location stored into private location
     * @param area stored into private area
     */
    geographical(String name, coordinates location, double area)
    {
        this.coordinates = location;
        this.name = name;
        this.area = area;
    }

    /**
     * Constructs geographical with name and area
     * @param name name stored into private name
     * @param area area stored into private area
     */
    geographical(String name, double area)
    {
        this.coordinates = null;
        this.name = name;
        this.area = area;
    }

    /**
     * Constructs geographical with location
     * @param location pass a coordinate value for use with looking up location of geopgrahical
     */
    geographical(coordinates location) {
        this.coordinates = location;
        this.name = null;
        this.area = -1;
    }

    /**
     * Constructs geographical with only name
     * @param name Name of geographical object
     */
    geographical(String name) {
        this.coordinates = null;
        this.name = name;
        this.area = -1;
    }

    /**
     * Constructs geographical with name and location
     * @param name Name of geographical object
     * @param location Location as @coordinates
     */
    geographical(String name, coordinates location) {
        this.coordinates = location;
        this.name = name;
        this.area = -1;
    }

    /**
     * Getter for coordinates
     * @return Returns coordinates known by geographical
     */
    public coordinates getCoordinates()
    {
        return this.coordinates;
    }

    /**
     * Set new coordinates
     * @param Coordinates Pass coordinate object to set location of geographical
     */
    public void setCoordinates(coordinates Coordinates)
    {
        coordinates = Coordinates;
    }


    /**
     *     returns old coordinateX and sets new coordinates X
     */
    int setCoordinateX(int x) {
        int oldCoordinateX = coordinates.getCoordinateX();
        coordinates.setCoordinateX(x);
        return oldCoordinateX;
    }

    /**
     *
     * @param y sets new coordinates Y
     * @return returns old coordinateY
     */
    int setCoordinatesY(int y) {
        int oldCoordinateY = coordinates.getCoordinateX();
        coordinates.setCoordinateY(y);
        return oldCoordinateY;
    }


    /**
     *
     * @return returns name of geographical object
     */
    String getName() {
        return name;
    }

    /**
     *
     * @param name sets name of geographical object
     * @return returns old name.
     */
    String setName(String name) {
        String oldName = this.name;
        this.name = name;
        return oldName;
    }

    /**
     *
     * @param Boundary Add boundaries to this place
     */
    public void addBoundary(boundary Boundary) {
        Boundary.addPlace(this);
        Boundaries.add(Boundary);
    }

    /**
     * Creates an array list by doing a loop
     Loop loop passes through all Places and and checks to see if they have
     boundary with same ID as the boundaries in this object, (using this.Boundaries.getId()). If they do they are added to a new ArrayList which will be returned.
     * @return Returns neighbors connected to this
     */
    public ArrayList<geographical> neighbors(ArrayList<geographical> Places)
    {
        ArrayList<geographical> Neighbors = new ArrayList<>();
        /*
        Creates an array list by doing a loop
        Loop loop passes through all Places and and checks to see if they have
        boundary with same ID as the boundaries in this object, (using this.Boundaries.getId()). If they do they are added to a new ArrayList which will be returned.
         */
        return Neighbors;
    }

    /**
     *
     * @return Returns area for geographical
     */
    double area()
    {
        return area;
    }

    /**
     *
     * @param area Sets area to this
     */
    void area(double area)
    {
        this.area = area;
    }


}

country.java

package geography;

import java.util.ArrayList;

public class country extends geographical {

    ArrayList<state> states;

    /**
     *
     * @param name Country name
     */
    country(String name) {
        super(name);
    }

    /**
     *
     * @param name Country name
     * @param area Country area
     */
    country(String name, double area) {
        super(name, area);
    }

    /**
     *
     * @param State Adds this state to country
     */
    public void addState(state State) {
        State.setCountry(this);
        states.add(State);
    }

    /**
     *
     * @return returns states
     */
    public ArrayList<state> getStates() {
        return states;
    }

    /**
     *
     * @return returns area
     */
    @Override
    public double area()
    {
        return super.area();
    }

    /**
     *
     * @param area Sets area to this
     */
    @Override
    public void area(double area)
    {
        super.area(area);
    }

}

state.java

package geography;

import java.util.ArrayList;

public class state extends geographical {

    ArrayList<city> cities;
    city CapitalCity;
    country parentCountry;


    /**
     * constructs new state object with empty list of cities and null name
     */
    state() {
        cities = new ArrayList<>();
    }

    /**
     * Constructs new state with specified name and empty list of cities
     * @param name Name of state
     */
    state(String name) {
        super(name);
        cities = new ArrayList<>();
    }

    /**
     * Add a new city to this state
     * @param name Name of city
     * @param location Location of city
     * @param isCapitalCity Is this the capital city?
     */
    public void addCity(String name, coordinates location, boolean isCapitalCity) {
        city newCity = new city(this, name, location, isCapitalCity);
        if (isCapitalCity) {
            CapitalCity = newCity;
            this.setCoordinates(newCity.getCoordinates());
        }
        cities.add(newCity);
    }

    /**
     * Add Existing city to this state
     * @param City existing @city object
     * @param isCapitalCity is this the capital?
     */
    public void addCity(city City, boolean isCapitalCity) {
        City.Capital(isCapitalCity);
        City.setState(this);
        if (isCapitalCity) {
            CapitalCity = City;
            this.setCoordinates(City.getCoordinates());
        }
        cities.add(City);

    }

    /**
     *
     * @param CapitalCity Sets Capital City
     */
    public void Capital(city CapitalCity)
    {
        this.CapitalCity = CapitalCity;
    }

    /**
     *
     * @return returns Capital City
     */
    public city Capital()
    {
        return CapitalCity;
    }


    /**
     *
     * @param parentCountry sets parent country
     */
    public void setCountry(country parentCountry)
    {
        this.parentCountry = parentCountry;
    }

    /**
     *
     * @return Gets list of cities in this state
     */
    public ArrayList<city> getCities()
    {
        return cities;
    }

    /**
     *
     * @return Get country this state is in
     */
    public country getCountry()
    {
        return parentCountry;
    }

    /**
     *
     * @return Return area of state
     */
    @Override
    public double area()
    {
        return super.area();
    }

    /**
     *
     * @param area Sets area to this
     */
    @Override
    public void area(double area)
    {
        super.area(area);
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote