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

I\'ve been trying for two days, but I can\'t wrap my mind around ArrayLists to s

ID: 3809417 • Letter: I

Question

I've been trying for two days, but I can't wrap my mind around ArrayLists to solve this Intro to Computer Science question using JAVA. Can you please help me?

I created a class called CTAStation that holds information for each train station, and that is working fine. My problem arises when I need to create a new class called CTARoute that encapsulates an ArrayList of CTAStation. Im having trouble getting my mind around what I need to do because I dont know how to implement the ArrayList class and I get confused when trying to build the default and non-default constructor. I dont know what would be the passed variables for the non-default constructor, etc.

Here is the full question for the problem (I included the data at the end of the question):

In this assignment, you will write a user-defined class that will encapsulate an ArrayList of CTAStations called CTARoute You will allow users to look up stations by name, add new stations, and delete existing stations.

To clarify things, the UML for the class GeoLocation and class CTAStation I created is the following (CTAStation extends the GeoLocation class and I've already created these two classes)::

In addition, I'm having trouble understanding if CTARoute extends any of the two classes I've already created, or if it is a stand alone class.

After creating the CTARoute class, create an application class for the user. This application needs to have a menu like this:

1. Display the names of all stations
2. Display the stations with wheelchair access
3. Display the nearest station to a location
4. Display information for a station with a specific name
5. Display information for all stations
6. Add a new station
7. Delete an existing station
8. Exit

The following are suggestions of how to create the methods for the CTARoute class:

- Adding a station should ask for each instance variable individually as well as the name of the route that the station is on
- Adding a station should ask for the names of the previous and following stations and insert the new station into the correct position
- Removing an existing station needs to ask for the name of the station and the route (or routes) that it appears on.

Anyway, if you made it this far, I appreciate your time for trying to help me. Here is the data from a file called "CTAStops.csv" for the objects of CTAStation:

Name,Latitude,Longitude,Location,Wheelchair,Red,Green
Harlem,41.88706,-87.80486,elevated,TRUE,-1,0
Oak Park,41.886784,-87.794324,elevated,FALSE,-1,1
Ridgeland,41.886784,-87.784628,elevated,FALSE,-1,2
Austin,41.887293,-87.774135,elevated,FALSE,-1,3
Central,41.887389,-87.76565,elevated,TRUE,-1,4
Laramie,41.887163,-87.754986,elevated,TRUE,-1,5
Cicero,41.886519,-87.744698,elevated,TRUE,-1,6
Pulaski,41.885412,-87.725404,elevated,TRUE,-1,7
Conservatory-Central Park Drive,41.884904,-87.716513,elevated,TRUE,-1,8
Kenzie,41.884321,-87.706155,elevated,TRUE,-1,9
California,41.88422,-87.696234,elevated,TRUE,-1,10
Ashland,41.885268,-87.666969,elevated,TRUE,-1,11
Morgan,41.8856,-87.6522,elevated,TRUE,-1,12
Clinton,41.885678,-87.641782,elevated,TRUE,-1,13
Clark/Lake,41.885767,-87.630885,elevated,TRUE,-1,14
State/Lake,41.88574,-87.627835,elevated,FALSE,-1,15
Randolph/Wabash,41.884431,-87.626149,elevated,FALSE,-1,16
Adams/Wabash,41.884431,-87.6261,elevated,FALSE,-1,17
Roosevelt,41.867368,-87.627402,elevated/subway,TRUE,23,18
Cermak-McCormick,41.8531548,-87.626423,elevated,TRUE,-1,19
35th-Bronzeville-IIT,41.831677,-87.625826,elevated,TRUE,-1,20
Indiana,41.821732,-87.621371,elevated,TRUE,-1,21
43rd,41.816462,-87.619021,elevated,TRUE,-1,22
47th,41.8094,-87.61909,elevated,TRUE,-1,23
51st,41.8022,-87.61903,elevated,TRUE,-1,24
Garfield,41.79454,-87.61835,elevated,TRUE,-1,25
47th,41.810318,-87.63094,surface,TRUE,26,-1
63rd,41.780536,-87.630952,surface,TRUE,28,-1
69th,41.768367,-87.625724,surface,TRUE,29,-1
79th,41.750419,-87.625112,surface,TRUE,30,-1
87th,41.735372,-87.624717,surface,TRUE,31,-1
95th/Dan Ryan,41.722377,-87.624342,surface,TRUE,32,-1
Addison,41.947428,-87.653626,elevated,TRUE,12,-1
Argyle,41.973453,-87.65853,embankment,FALSE,8,-1
Belmont,41.939751,-87.65338,elevated,TRUE,13,-1
Berwyn,41.977984,-87.658668,embankment,FALSE,7,-1
Bryn Mawr,41.983504,-87.65884,embankment,FALSE,6,-1
Cermak-Chinatown,41.853206,-87.630968,elevated,TRUE,24,-1
Chicago,41.896671,-87.628176,subway,TRUE,17,-1
Clark/Division,41.90392,-87.631412,subway,TRUE,16,-1
Fullerton,41.925051,-87.62866,elevated,TRUE,14,-1
Garfield,41.79542,-87.631157,surface,TRUE,27,-1
Grand,41.891665,-87.628021,subway,TRUE,18,-1
Granville,41.993664,-87.659202,embankment,TRUE,4,-1
Harrison,41.874039,-87.627479,subway,FALSE,22,-1
Howard,42.019063,-87.672892,embankment,TRUE,0,-1
Jackson,41.878153,-87.627596,subway,TRUE,21,-1
Jarvis,42.015876,-87.669092,embankment,FALSE,1,-1
Lake,41.884809,-87.627813,subway,TRUE,19,-1
Lawrence,41.969139,-87.658493,embankment,FALSE,9,-1
Loyola,42.001073,-87.661061,embankment,TRUE,3,-1
Monroe,41.880745,-87.627696,subway,FALSE,20,-1
Morse,42.008362,-87.665909,embankment,FALSE,2,-1
North/Clybourn,41.910655,-87.649177,subway,FALSE,15,-1
Sheridan,41.953775,-87.654929,elevated,FALSE,11,-1
Sox-35th,41.831191,-87.630636,surface,TRUE,25,-1
Thorndale,41.990259,-87.659076,embankment,FALSE,5,-1
Wilson,41.964273,-87.657588,elevated,FALSE,10,-1

CTA Route -name: String -stops: AmayListcCTAStationD +addStation(CTAStation): void +removestation(CTAStation): void insertStation(int CTAStation): void HookupStation(String): CTAStation +nearest Station(double, double): CTAStation neareststation(GeoLocation): CTAStation the previous lab, you only need to con

Explanation / Answer


Hi,

Please see below the classes. Please update the functions in CTAStation class with your class functions

//CTAStation.java

public class CTAStation extends GeoLocation{

   private String name;
   private String location;
   private boolean opened;
   private boolean wheelchair;
  
   public CTAStation(String name, double latitude, double longitude, String location ,boolean wheelchair, int red,int green){
       this.name = name;
       this.latitude = latitude;
       this.longitude =longitude;
       this.location = location;
       this.wheelchair = wheelchair;
      
      
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getLocation() {
       return location;
   }
   public void setLocation(String location) {
       this.location = location;
   }
   public boolean isOpened() {
       return opened;
   }
   public void setOpened(boolean opened) {
       this.opened = opened;
   }
   public boolean isWheelchair() {
       return wheelchair;
   }
   public void setWheelchair(boolean wheelchair) {
       this.wheelchair = wheelchair;
   }

}

//GeoLocation.java
public class GeoLocation {

   protected double latitude;
   protected double longitude;
  
  
   public double calcDistance(GeoLocation loc){
      
       return 0.0;
   }
  
   public double calcDistance(double latitude , double longitude){
      
       return 0.0;
   }
  
   public double getLatitude() {
       return latitude;
   }
   public void setLatitude(double latitude) {
       this.latitude = latitude;
   }
   public double getLongitude() {
       return longitude;
   }
   public void setLongitude(double longitude) {
       this.longitude = longitude;
   }
  
}


//CTARoute.java
import java.util.ArrayList;


public class CTARoute {

   private String name;
   private ArrayList<CTAStation> stops ;
  
   public CTARoute(){
       this.stops = new ArrayList<CTAStation>();
      
   }
  
   public void addStation(CTAStation station){
      
       this.getStops().add(station);
   }
   public void removeStation(CTAStation station){
       for( CTAStation stop : stops){
           if(stop.getName().equalsIgnoreCase(station.getName())){
               this.getStops().remove(stop);
               break;
           }
       }
   }
  
   public void insertStation(int stop,CTAStation station){
       this.getStops().add(stop, station);
   }
  
  
   public CTAStation lookupStation(String name){
       CTAStation station =null;
       for( CTAStation stop : stops){
           if(stop.getName().equalsIgnoreCase(name)){
               station = stop;
               break;
           }
       }
      
       return station;
   }
  
   public CTAStation nearestStation(double latitude,double longitude){
       double minDistance =0;
       double currDistance =0;
       int minStopIndex =0;
       for( int i=0;i<stops.size();i++){
           CTAStation currstop = stops.get(i);
           if(i==0){
               minDistance =currstop.calcDistance(latitude, longitude);
           }
          
           currDistance = currstop.calcDistance(latitude, longitude);
           if(currDistance > minDistance ){
               minDistance = currDistance;
               minStopIndex =i;
           }
       }
      
       return this.getStops().get(minStopIndex);
   }
   public CTAStation nearestStation(GeoLocation loc){
       double minDistance =0;
       double currDistance =0;
       int minStopIndex =0;
       for( int i=0;i<stops.size();i++){
           CTAStation currstop = stops.get(i);
           if(i==0){
               minDistance =currstop.calcDistance(loc);
           }
          
           currDistance = currstop.calcDistance(loc);
           if(currDistance > minDistance ){
               minDistance = currDistance;
               minStopIndex =i;
           }
       }
      
       return this.getStops().get(minStopIndex);
   }
  
  
   public String toString(){
       String retString="";
       for( int i=0;i<stops.size();i++){
           CTAStation stop = stops.get(i);
           retString ="Name: "+stop.getName() + ", Location: "+stop.getLocation()
                   +", Is opened: "+stop.isOpened()+", Wheelchair?: "+stop.isWheelchair();
       }
      
       return retString;
      
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public ArrayList<CTAStation> getStops() {
       return stops;
   }
   public void setStops(ArrayList<CTAStation> stops) {
       this.stops = stops;
   }
}


//CTAApp.java

import java.util.Scanner;


public class CTAApp {

   public static void main(String[] args){
       Scanner scan = new Scanner(System.in);

       String stationName ="";
       //populating station Array
       CTAStation station1 = new CTAStation("Harlem",41.88706,-87.80486,"elevated",true,-1,0);
       CTAStation station2 = new CTAStation("Oak Park",41.886784,-87.794324,"elevated",false,-1,1);
       CTAStation station3 = new CTAStation("Ridgeland",41.886784,-87.784628,"elevated",false,-1,2);
       CTAStation station4 = new CTAStation("Austin",41.887293,-87.774135,"elevated",false,-1,3);
       CTAStation station5 = new CTAStation("Central",41.887389,-87.76565,"elevated",true,-1,4);

       CTARoute route = new CTARoute();
       route.getStops().add(station1);
       route.getStops().add(station2);
       route.getStops().add(station3);
       route.getStops().add(station4);
       route.getStops().add(station5);


       int choice=0;

      
           System.out.println("Welcome to CTA!");
           System.out.println("Please select any option ");
           System.out.println("1. Display the names of all stations");
           System.out.println("2. Display the stations with wheelchair access");
           System.out.println("3. Display the nearest station to a location");
           System.out.println("4. Display information for a station with a specific name");
           System.out.println("5. Display information for all stations");
           System.out.println("6. Add a new station");
           System.out.println("7. Delete an existing station");
           System.out.println("8. Exit");

           choice = scan.nextInt();
      


       switch(choice){
       case 1: for(int i=0;i<route.getStops().size();i++){
                       CTAStation stop = route.getStops().get(i);
                       System.out.println(stop.getName());
                   }
               break;          
       case 2: for(int i=0;i<route.getStops().size();i++){
                   CTAStation stop = route.getStops().get(i);
                   if(stop.isWheelchair()){
                       System.out.println(stop.getName());
                   }
               }
               break;
       case 3: System.out.println("Enter the location:");

               String location = scan.nextLine();
               GeoLocation loc= new GeoLocation();
               route.nearestStation(loc);
               break;

       case 4:System.out.println("Enter the Sation name:");

               stationName= scan.nextLine();
               CTAStation station =route.lookupStation(stationName);
      
               System.out.println("Name: "+station.getName() + ", Location: "+station.getLocation()
                       +", Is opened: "+station.isOpened()+", Wheelchair?: "+station.isWheelchair());
      
              
               break;
       case 5: System.out.println(route.toString());
               break;
       case 6: System.out.println("Please enetr the Sattion details");
               System.out.println("Name:");
               String stName = scan.nextLine();
              
               System.out.println("Location:");
               String stLoc = scan.nextLine();
              
               System.out.println("Latitude:");
               double stLat = scan.nextDouble();
               System.out.println("Longitude:");
               double stLong = scan.nextDouble();
              
               CTAStation stationtoAd = new CTAStation(stName, stLat, stLong, stLoc, true, 1, 1);
               route.addStation(stationtoAd);
              
       case 7: route.removeStation(station1);
       case 8: System.exit(0);
       }
      
      
   }

}

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