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

CTAdata.txt: 415 brown Merchandise-Mart 16:15:54 414 brown Merchandise-Mart 16:1

ID: 3846984 • Letter: C

Question

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

TestFinalAnswer.java:

package finalanswer;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

import stdlib.StdIn;

import stdlib.StdOut;

public class TestFinalAnswer {

               public static <Item extends Comparable<? super Item>> boolean isSorted(Item[] a) {

                              for (int i = 0; i < a.length-1; i++) {

                                             if (a[i].compareTo(a[i+1]) > 0) return false;

                              }

                              return true;

               }

               public static void main(String[] args) {

                              StdOut.println("*** " + FinalAnswer.yourName() + " ***");

                              StdOut.println();

                              Integer[] array = new Integer[12];

                              Random r = new Random();

                              for (int i = 0; i < array.length; i++) {

                                             array[i] = r.nextInt(1000);

                              }

                              StdOut.println("Array before sorting: " + Arrays.toString(array));

                              FinalAnswer.minpqSort(array);

                              StdOut.println("Array after sorting: " + Arrays.toString(array));

                              StdOut.println("Array is sorted: " + isSorted(array));

                              StdOut.println();

                              Queue<String> queue = new Queue<String>();

                              queue.enqueue("first");

                              queue.enqueue("second");

                              queue.enqueue("third");

                              queue.enqueue("fourth");

                              StdOut.println("Queue before reversing: " + queue);

                              FinalAnswer.reverseQueue(queue);

                              StdOut.println("Queue after reversing: " + queue);

                              StdOut.println();

                              double[] frequencies = {110.0, 110.0*1.224, 110.0*1.224*1.224};

                              ArrayList<Chord> risingChords = FinalAnswer.createRisingChordList(0.2, frequencies, 10);

                              for (Chord risingChord: risingChords) {

                                             StdOut.println("Playing: " + risingChord);

                                             risingChord.play();

                              }

                              StdOut.println();

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

                              StdIn.fromFile("data/CTAdata.txt");

                              while (!StdIn.isEmpty()) {

                                             int runNumber = StdIn.readInt();

                                             String lineColor = StdIn.readString();

                                             String nextStation = StdIn.readString();

                                             String arrivalTime = StdIn.readString();

                                             ctaTrains.add(new CTATrain(runNumber, lineColor, nextStation, arrivalTime));

                              }

                              StdOut.println("--- Trains before sorting ---");

                              for (CTATrain ctaTrain: ctaTrains) {

                                             StdOut.println(ctaTrain);

                              }

                              StdOut.println();

                              ctaTrains.sort(null);

                              StdOut.println("--- Trains after sorting by run number ---");

                              for (CTATrain ctaTrain: ctaTrains) {

                                             StdOut.println(ctaTrain);

                              }

                              StdOut.println();

                              ctaTrains.sort((CTATrain t1, CTATrain t2) -> (t1.getArrivalTime().compareTo(t2.getArrivalTime())));

                              StdOut.println("--- Trains after sorting by arrival time ---");

                              for (CTATrain ctaTrain: ctaTrains) {

                                             StdOut.println(ctaTrain);

                              }

                              StdOut.println();

                              StdOut.println("=== " + FinalAnswer.yourName() + " ===");

               }

}

Need help with a Java problem Instructions 1. In Eclipse, create a package called finalanswer. 2. Copy and place into the package the file TestEinalAnswer ava, provided below. 3. In the package, create a class called FinalAnswer. All of the static methods you are asked to write will go in this class. 4. You will also add to the package a reference class called CTATrain. 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 train's: 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:mmiss" indicating when it is expected to arrive at that station. The API consists of: 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 getLine Color O toString O 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 ATrain objects are comparable.

Explanation / Answer

Class CTATrain.java:

package finalanswer;

public class CTATrain implements Comparable<CTATrain>{
   private int runNumber;
   private String lineColor, nextStation, 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 getRunNumber() {
       return runNumber;
   }
   public String getLineColor() {
       return lineColor;
   }
   public String getNextStation() {
       return nextStation;
   }
   public String getArrivalTime() {
       return arrivalTime;
   }

   @Override
   public String toString() {
       return "[" + lineColor + ", " + runNumber
               + ", " + nextStation + ", "
               + arrivalTime + ">";
   }
  
   public int compareTo(CTATrain that) {
       return that.getRunNumber() > this.getRunNumber() ? -1 : (that.getRunNumber() < this.getRunNumber() ? 1 : 0);
   }
}