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

Help w/ Connecting the following classes car source/ lane. I need car source to

ID: 3758570 • Letter: H

Question

Help w/ Connecting the following classes car source/ lane. I need car source to be able to access lanes individually (like a start lane, end lane, ect). So need help with this. I am thinking a linked list is the best way to go.

}

/** * Lanes are the containers for most of the * actual actors themselves. It contains a * queue of cars which are controlled by a LegacyTrafficLight, * and optionally constrained by a maxCount. */ public class Lane implements Actor { /** The traffic light this lane looks at*/ private LegacyTrafficLight trafficLight; /** The cars that this lane currently has */ private Queue<Car> cars; /** The maximum number of cars allowed in this lane */ private int maxcount; /** * Create a new lane with a give LegacyTrafficLight * @param tLight The light to make this lane look at */ public Lane(LegacyTrafficLight tLight) { trafficLight = tLight; cars = new Queue<>(); maxcount = 0; //Infinite lane } /** * Create a lane with a specified maxCount * @param tLight The light to make this lane look at * @param maxc How many cars will be allowed in this Lane at a time */ public Lane(LegacyTrafficLight tLight, int maxc) { trafficLight = tLight; //sbw this! cars = new Queue<>(); maxcount = maxc; } /** * Remove the first car and return it * @return The first car in the lane */ public Car dequeue() { return cars.dequeue(); } /** * Add a car to the back of the queue * @param c The car to add */ public void enqueue(Car c) { if (!this.isFull()) cars.enqueue(c); } /** * Check to see if the number of cars exceeds maxCount * @return True if the lane is full, false otherwise */ public boolean isFull() { if ( cars.size() >= maxcount && maxcount != 0) return true; return false; } /** * Check to see if the lane has any cars * @return True if there are no cars, false otherwise */ public boolean isEmpty() { return cars.isEmpty(); } /** * Non-destructively look at the car in the front * of the queue * @return The car in the front of the lane */ public Car front() { return cars.front(); } /** * Get how many cars are in this lane * @return The number of cars in this lane */ public int size() { return cars.size(); } /** * @return The traffic light this lane sees */ public LegacyTrafficLight getTrafficLight() { return trafficLight; } /** * @return A deep copy of the entire queue of cars that this lane has. */ public Queue getQueue() { return cars.clone(); } /** * Set the traffic light that this lane looks at to a new one * @param tLight The light to make this lane look at */ public void setTrafficLight(LegacyTrafficLight tLight) { trafficLight = tLight; } /** * See how many cars are allowed in this lane * @return The maxCount of this lane */ public int getMaxCount() { return maxcount; } /** * Act. Make the car in the front of the lane tick if * there is one. * @param tick */ public void act(int tick){ if(!cars.isEmpty()) cars.front().act(tick); //Debug. TODO System.out.println("Hi there! I have " + size() +" cars inside."); } /** * Look at a car in a given position i n this lane * @param location The index to peak at * @return The car at location */ public Car peek(int location){ return cars.peek(location); }

}

public class CarSource implements Actor { /** * The total number of cars this factory has created */ private int totalCars; /** * A blank car */ private Car newCar; /** * The blank Route */ private Route newRoute; /** * Reference to the Controller for finding the lanes and for adding cars to the actor list */ private Controller controller; /** * An ArrayList that contains all of the lanes */ private ArrayList<Lane> lanes; /** * The wrong constructor */ public CarSource() { System.out.println("Wrong Constructor"); } /** * The constructor, requires a reference to the controller object be given to it * this one id for the skeleton and requires that the three lane segments be given too it in the order: * first, middle, last. * @param c The controller * @param first The first lane * @param middle The middle lane * @param end The last lane */ public CarSource(Controller c, Lane first, Lane middle, Lane end) { controller = c; totalCars = 0; lanes = new ArrayList(); lanes.add(0, first); lanes.add(1, middle); lanes.add(2, end); } /** * "Act" (i.e, generate a new car) */ public void act(int tick) { //create a new route newRoute = new Route(lanes.get(0), lanes.get(1), lanes.get(2)); // Set the route time of the first lane to this time newRoute.setRouteTime(lanes.get(0), tick); //create a new car and pass it its route newCar = new Car(newRoute); // Put the car in the first lane lanes.get(0).enqueue(newCar); //increment the totalCars stat tracker by 1 totalCars++; } /** * A string description of the factory * @return The total number of cars ever created by this factory. */ public String toString() { return "Total cars: " + Integer.toString(totalCars); } /** * Accessor for the total cars created in the lifetime of this object. Mainly used by the stat's */ public int getTotalCars() { return totalCars; } }

Explanation / Answer

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <string.h>

#define TOP 1
#define BOT 2

struct node
{
    char plate [15] ;
    struct node *link ;
} *front[5], *rear[5] ;

char plate[15], temp[15] ;
int i ;

void add_dq ( struct node**, struct node**, int, char* ) ;
char* del_dq ( struct node**, struct node**, int ) ;
void push ( struct node**, char* ) ;
char* pop ( struct node** ) ;
void q_display ( struct node * ) ;

void main( )
{
    char ad ;
    int s, lane = -1, min, lc ;

    clrscr( );
    while ( 1 )
    {
        for ( i = 0 ; i < 5 ; i++ )
        {
            printf( "lane %d: ", i ) ;
            q_display ( front[i] ) ;
        }

        printf( " Arrival/Departure/Quit? ( A/D/Q ): " ) ;
        ad = getch( ) ;

        if ( toupper ( ad ) == 'Q' )
            exit ( 1 ) ;

        printf ( " Enter license plate num:" ) ;
        gets ( plate ) ;
        ad = toupper ( ad ) ;

        if ( ad == 'A' ) /* arrival of car */

        {
            lane = -1 ; /* assume no lane is available */

            min = 10 ;
            for ( i = 0 ; i < 5 ; i++ )
            {
                s = count ( front[i] ) ;
                if ( s < min )
                {
                    min = s ;
                    lane = i ;
                }
            }

            if ( lane == -1 )
                printf ( " No room available" ) ;
            else
            {
                add_dq ( &front[ lane ], &rear[ lane ], BOT, plate ) ;
                printf ( " park car at lane %d slot %d ", lane, s ) ;
            }
        }
        else
        {
            if ( ad == 'D' ) /* departure of car */

            {
                for ( i = 0 ; i < 5 ; ++i )
                {
                    s = search ( front[i], plate ) ;
                    if ( s != -1 )
                    {
                        lane = i ;
                        break ;
                    }
                }
                if ( i == 5 )
                    printf ( " no such car!! " ) ;
                else
                {
                    printf ( " car found at lane %d slot %d ", lane, s ) ;
                    del_dq ( &front[ lane ], &rear[ lane ], s ) ;
                }
            }
            elseif ( ad == 'Q' )
                    exit ( 1 ) ;
        }
    }
}

/* adds a new element at the end of queue */
void add_dq ( struct node **f, struct node **r, int tb, char *p )
{
    struct node *q ;
    /* create new node */

    q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, p ) ;
    q -> link = NULL ;

    /* if the queue is empty */
if ( *f == NULL )
        *f = q ;
    else
    {
        if ( tb == BOT )
            ( *r ) -> link = q ;
        else
        {
            q -> link = *f ;
            *f = q ;
            return ;
        }
    }
    *r = q ;
}

char* del_dq ( struct node **f, struct node **r, int n )
{
    struct node *q, *top = NULL ;
    /* if queue is empty */
if ( *f == NULL )
        printf ( "queue is empty" ) ;
    else
    {
        if ( n == 0 )
        {
            strcpy ( temp, ( *f ) -> plate ) ;
            q = *f ;
            *f = ( *f ) -> link ;
            free ( q ) ;
            return temp ;
        }

        /* locate node */
for ( i = 0 ; i < n ; i++ )
        {
            /* drive out cars */

            push ( &top, ( *f ) -> plate ) ;

            /* delete the node */

            q = *f ;
            *f = q -> link ;
            free ( q ) ;
        }

        /* delete the nth node */

        q = *f ;
        *f = q -> link ;
        free ( q ) ;

        for ( i = 0 ; i < n ; i++ )
        {
            strcpy ( temp, pop ( &top ) ) ;

            /* add the node */

            add_dq ( f, r, TOP, temp ) ;
        }
    }
}

int count ( struct node *q )
{
    int c = 0 ;

    /* traverse the entire linked list */
while ( q!= NULL )
    {
        q = q -> link ;
        c++ ;
    }
    return c ;
}

int search ( struct node *q, char *p )
{
    int s = -1, c = 0 ;

    while ( q != NULL )
    {
        if ( strcmp ( p, q -> plate ) == 0 )
        {
            s = c ;
            break ;
        }
        else
        {
            q = q -> link ;
            c++ ;
        }
    }
    return ( s ) ;
}

/* adds a new element to the top of stack */
void push ( struct node **s, char* item )
{
    struct node *q ;
    q = ( struct node* ) malloc ( sizeof ( struct node ) ) ;
    strcpy ( q -> plate, item ) ;
    q -> link = *s ;
    *s = q ;
}

/* removes an element from top of stack */
char* pop ( struct node **s )
{
    struct node *q ;

    /* if stack is empty */
if ( *s == NULL )
    {
        return NULL ;
    }
    else
    {
        q = *s ;
        strcpy ( temp, q -> plate ) ;
        *s = q -> link ;
        free ( q ) ;
        return ( temp ) ;
    }
}

void q_display ( struct node *q )
{
    while( q != NULL )
    {
        printf ( "%s", q -> plate ) ;
        q = q -> link ;
    }
    printf ( " " ) ;
}