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

Reference class Write a reference class called CTATrain that models information

ID: 3846988 • Letter: R

Question

Reference class Write a reference class called CTATrain that models information about a CTA "L" train making its run. The instance variables should contain information about the trains: run number, an integer: line color, a string containing one of the values "red", "green", "blue", "purple", "brown", "pink", "orange", or "yellow": next station, a string containing the name of the station it will next arrive at: arrival time, a string of the form "hh:mm:ss" indicating when it is expected to arrive at that station. The API consists of: a four-parameter constructor: individual methods for retrieving the value of each instance variable (i.e., accessor methods). For example, the signature of the one retrieving the line color will be public string getLineColor() tostring () method that produces a string looking like this:[brown, 421, Rockwell 13:34:54 >, where the values are the line color, run number, next station, and arrival time: compareTo (CTATrain that) method that compares CTA train objects based on their run number, which means you should define the class so that CTATrain objects are comparable. The test program has been written to read data from the file CTAdata.txt. Place this in your data folder in Eclipse. The test program then sorts an array list of trains in a couple of different ways, each time printing the list.

Explanation / Answer

PrintCTATrains.java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

import stdlib.In;
import stdlib.StdIn;
import stdlib.StdOut;

public class PrintCTATrains {
   public static void main(String[] args) {

        StdIn.fromFile ("CTAdata.txt");

        ArrayList<CTATrain> arrayToSort = new ArrayList<CTATrain>();

        while (StdIn.hasNextLine()){
            String line = StdIn.readLine();
            String[] fields = line.split("\s+");
            int runNumber= Integer.parseInt(fields[0]);
            String color= fields[1];
            String station=fields[2];
            String arrivalTime = fields[3];
            CTATrain train = new CTATrain(runNumber, color, station, arrivalTime);
            arrayToSort.add(train);
    }

       Collections.sort(arrayToSort);
       StdOut.println(" Sorted by run number:");
       for(CTATrain item:arrayToSort){
           StdOut.println(item);
}

       StdOut.println(" Sorted by next station:");
        Collections.sort(arrayToSort, CTATrain.BY_STATION);

        for(CTATrain item:arrayToSort){
            StdOut.println(item);
        }

       StdOut.println(" Sorted by arrival time:");
        Collections.sort(arrayToSort, CTATrain.BY_ARRIVAL_TIME);

        for(CTATrain item:arrayToSort){
            StdOut.println(item);
        }
    }
}


CTATrain.java


import java.util.Comparator;


public class CTATrain implements Comparable <CTATrain>{
    public static final Comparator<CTATrain> BY_STATION = new ByStation();
   public static final Comparator<CTATrain> BY_ARRIVAL_TIME = new ByArrivalTime();
    private final int runNumber;
    private final String lineColor;
    private final String nextStation;
    private final String arrivalTime;

    public CTATrain(
           int runNumber,
           String lineColor,
           String nextStation,
           String arrivalTime){
        this.runNumber = runNumber;
        this.lineColor = lineColor;
        this.nextStation = nextStation;
        this.arrivalTime = arrivalTime;
    
    }

   public int runNumber(){
       return runNumber;
    }
  
    public String lineColor(){
       return lineColor;

    }
    public String nextStation(){
       return nextStation;
    }
    public String arrivalTime(){
       return arrivalTime;
    }


   public String toString(){
       return "[" + this.lineColor + ", " + this.runNumber + ", " + this.nextStation + ", " + this.arrivalTime + "]";
   }
  

    private static class ByStation implements Comparator<CTATrain> {
        public int compare(CTATrain a, CTATrain b) {
            return a.nextStation.compareTo(b.nextStation);
           }
        }

    private static class ByArrivalTime implements Comparator<CTATrain> {
        public int compare(CTATrain a, CTATrain b) {
           return a.arrivalTime.compareTo(b.arrivalTime);
           }
}

   public int compareTo(CTATrain that){
        if (this.runNumber < that.runNumber) return -1;
        if (this.runNumber > that.runNumber) return +1;
        return 0;
        }
   }

CTAdata.txt

415 brown Merchandise-Mart 16:15:54
414 brown Merchandise-Mart 16:18:48
504 purple Merchandise-Mart 16:19:59
513 purple Merchandise-Mart 16:21:57
425 brown Merchandise-Mart 16:21:00
416 brown Merchandise-Mart 16:25:45
422 brown Merchandise-Mart 16:26:37
415 brown Merchandise-Mart 16:31:54
505 purple Merchandise-Mart 16:31:31
411 brown Merchandise-Mart 16:31:55
513 purple Adams/Wabash 16:14:57
614 green Adams/Wabash 16:16:12
310 pink Adams/Wabash 16:17:29
720 orange Adams/Wabash 16:18:52
422 brown Adams/Wabash 16:19:37
008 green Adams/Wabash 16:21:49
007 green Adams/Wabash 16:22:50
415 brown Adams/Wabash 16:24:54
504 purple Adams/Wabash 16:25:59
315 pink Adams/Wabash 16:26:00
711 orange Adams/Wabash 16:27:32
425 brown Adams/Wabash 16:30:00
015 green Adams/Wabash 16:30:34
607 green Adams/Wabash 16:31:48
423 brown Francisco 16:14:38
413 brown Francisco 16:17:32
419 brown Francisco 16:21:00
424 brown Francisco 16:20:47
420 brown Francisco 16:28:17
421 brown Francisco 16:30:42
420 brown Belmont 16:15:17
505 purple Belmont 16:14:31
411 brown Belmont 16:15:55
820 red Belmont 16:14:40
421 brown Belmont 16:16:42
824 red Belmont 16:16:21
511 purple Belmont 16:18:45
821 red Belmont 16:20:38
825 red Belmont 16:21:38
418 brown Belmont 16:22:46
517 purple Belmont 16:23:30
828 red Belmont 16:25:59
417 brown Belmont 16:27:11
423 brown Belmont 16:27:38
922 red Belmont 16:27:57
512 purple Belmont 16:28:48
514 purple Belmont 16:30:32
913 red Belmont 16:32:58
822 red Belmont 16:32:18

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