-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo_for_nov4_2016
864 lines (815 loc) · 29.5 KB
/
Demo_for_nov4_2016
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
Script started on Fri Nov 4 13:40:32 2016
ds_for_demoid@mactop: ~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-projdavid@mactop:~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-proj$ cat unix_comman
# How to use the current file:
# Step 1: Copy and paste current file into a file with the name
# unix_commands_for_nov2
# and save the resulting file in the iMac's Desktop directory
# Step 2: Preserve that file using the Unix command:
# cp -p unix_commands_for_nov2 save_unix_commands_for_nov2
# Step 3: Edit the file unix_commands_for_nov2 replacing File2, File3, etc.,
# with your choice of class names; see NOTE1 below to understand
# what this step means as File2, File 3, etc.
# Step 4: Edit the file unix_commands_for_nov2 replacing "data1" with
# your choice of "data2", etc., IF you wish to make such a change;
# see NOTE2 below for what this step means as "data1", etc.
# Step 5: Change the "mode" of the resulting file so the file becomes
# executable under the Unix command interpreter, via the
# following Unix command:
# chmod +x unix_commands_for_nov2
# Step 6: Try an experiment using the resulting file:
# ./unix_commands_for_nov2
# Scroll up and down the Unix window to see if that experiment
# was as successful as you wanted it to be; if it is,
# go to Step 7. (Otherwise go back to Step 3 or Step 4.)
# Step 7: Do the following Unix to obtain a demo file:
# script
# cat unix_commands_for_nov2
# ./unix_commands_for_nov2
# exit
# col -b < typescript > Demo_for_nov2_2016
# Step 8: Submit all your *java files and all your *data* files and also
# the file Demo_for_nov2_2016 to the folder for you team for
# the top-level logic project due on Friday, where you submit
# all those files during the lab on Wed, Nov 2, to show the
# current state of your iterative enhancement.
#!/bin/csh
echo "<UNIX-PROMPT> pwd"
pwd
echo "<UNIX-PROMPT> ls -tl *java"
ls -tl *java
echo "<UNIX-PROMPT> wc *java"
wc *java
echo "<UNIX-PROMPT> cat TrafficTesterView.java"
cat TrafficTesterView.java
# NOTE1 On Wed, Nov 2, replace File2, File3, etc., by appropriate
# Java class names to demonstrate the current stage of
# your iterative enhancement. Use as many or as few such
# classes as you decide would best demonstrate the
# current stage of your iterative enhancement.
echo "<UNIX-PROMPT> cat TrafficTesterModel.java"
cat TrafficTesterModel.java
echo "<UNIX-PROMPT> cat Grid.java"
cat Grid.java
echo "<UNIX-PROMPT> cat Intersection.java"
cat Intersection.java
echo "<UNIX-PROMPT> cat Lane.java"
cat Lane.java
echo "<UNIX-PROMPT> cat Car.java"
cat Car.java
echo "<UNIX-PROMPT> /bin/rm *class"
/bin/rm *class
echo "<UNIX-PROMPT> javac TrafficTesterView.java"
javac TrafficTesterView.java
echo "<UNIX-PROMPT> ls -tl *class"
ls -tl *class
# NOTE2 On Wed, Nov 2, replace "data1" below by one of "data2" (or
# "data3", etc.), if you wish, according to your decision on
# what would best demonstrate the current stage of your
# iterative enhancement.
echo "<UNIX-PROMPT> cat data2_for_TrafficTesterView"
cat data2_for_TrafficTesterView
echo "<UNIX-PROMPT> java TrafficTesterView < data2_for_TrafficTesterView"
java TrafficTesterView < data2_for_TrafficTesterView
_for_demoactop: ~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-projdavid@mactop:~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-proj$ ./unix_commands
<UNIX-PROMPT> pwd
/Users/david/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-proj
<UNIX-PROMPT> ls -tl *java
-rwxr-xr-x@ 1 david staff 6396 Nov 4 13:34 Grid.java
-rwxr-xr-x@ 1 david staff 1218 Nov 4 13:26 Car.java
-rwxr-xr-x@ 1 david staff 1359 Nov 4 13:25 Lane.java
-rwxr-xr-x@ 1 david staff 8719 Nov 4 13:24 Intersection.java
-rwxr-xr-x@ 1 david staff 983 Nov 4 13:21 TrafficTesterModel.java
-rwxr-xr-x@ 1 david staff 6821 Nov 4 13:19 TrafficTesterView.java
<UNIX-PROMPT> wc *java
42 125 1218 Car.java
166 505 6396 Grid.java
171 646 8719 Intersection.java
49 155 1359 Lane.java
31 86 983 TrafficTesterModel.java
165 795 6821 TrafficTesterView.java
624 2312 25496 total
<UNIX-PROMPT> cat TrafficTesterView.java
// Programmer: Arthur Charlesworth (c) Copyright 2016
// Modified by GOATS team: Alex David Rex Tom Zihao
// *****************************************************************************
// *****************************************************************************
// **** TrafficTesterView
// *****************************************************************************
// *****************************************************************************
// Latest Enhancement: Corrected comment format
// NOTE: A lane has the same coordinates as the intersection the lane aims at.
// Throughout the rest of the semester, make sure that
// intersections of a grid are processed in exactly the following order:
// row 1 intersections, from left to right
// row 2 intersections, from left to right, etc, for all rows.
// In an actual simulation, that process should be repeated (in that specific
// order) as many times as necessary for the desired length of the simulation.
// But, for simplicity, the initial tester software students build should
// only show the first iteration of the above process, that is, only one
// processing of each of the intersections.
//
// The format of the data to be used with the program in this file
// is show, via an example, at the end of this file.
import java.util.*;
public class TrafficTesterView {
// NOTE: Just to keep the current tester program really simple,
// the following constants are not encapsulated within two classes, Direction
// and Turn, according to appropriate Information Hiding:
static final int SOUTHWARD = 0;
static final int EASTWARD = 1;
static final int NORTHWARD = 2;
static final int WESTWARD = 3;
static final int NEVER_TURN = 0;
static final int TURN_RIGHTWARD = 1;
static final int TURN_LEFTWARD = -1;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
console.nextLine();
int numIntersectionsInOneDirection = console.nextInt();
//System.out.print("The number of intersections in one direction ");
//System.out.println("is: " + numIntersectionsInOneDirection);
console.nextLine();
console.nextLine();
int numberOfCars = console.nextInt();
//System.out.println("The number of cars is: " + numberOfCars);
int carID;
int row;
int col;
int laneDirectionCode;
int numBlocksBeforeTurning;
int turnDirectionCode;
ArrayList< ArrayList<Integer> > carParameters;
carParameters = new ArrayList< ArrayList<Integer> >();
for (int i = 1; i <= numberOfCars; i++) {
console.nextLine();
console.nextLine();
carID = console.nextInt();
//System.out.println("Car #" + carID);
console.nextLine();
console.nextLine();
col = console.nextInt();
console.nextLine();
console.nextLine();
row = console.nextInt();
console.nextLine();
console.nextLine();
laneDirectionCode = console.nextInt();
console.nextLine();
console.nextLine();
numBlocksBeforeTurning = console.nextInt();
console.nextLine();
console.nextLine();
turnDirectionCode = console.nextInt();
ArrayList<Integer> currentParams = new ArrayList<Integer>();
currentParams.add(carID);
currentParams.add(row);
currentParams.add(col);
currentParams.add(laneDirectionCode);
currentParams.add(numBlocksBeforeTurning);
currentParams.add(turnDirectionCode);
carParameters.add(currentParams);
//System.out.println(" is born in the lane located at col " + col +
// " and row " + row + ", that aims " +
// convertToLaneDirection(laneDirectionCode) + ",");
//System.out.println(" and has " + numBlocksBeforeTurning +
// " block(s) to go before turning");
//System.out.println(" and plans to " +
// convertToTurnDirection(turnDirectionCode));
} // end for
//System.out.println("In TrafficTesterView: constructing a Traffic"+
//"TesterModel");
TrafficTesterModel sim = new TrafficTesterModel(
numIntersectionsInOneDirection,numIntersectionsInOneDirection,
carParameters);
//System.out.println("In TrafficTesterView: running the TrafficTesterModel");
sim.run();
} // main
public static String convertToLaneDirection(int laneDirectionCode) {
if (laneDirectionCode == SOUTHWARD) return "SOUTHWARD";
if (laneDirectionCode == EASTWARD) return "EASTWARD";
if (laneDirectionCode == NORTHWARD) return "NORTHWARD";
if (laneDirectionCode == WESTWARD) return "WESTWARD";
return "ILLEGAL laneDirectionCode!!!";
} // convertToLaneDirection
public static String convertToTurnDirection(int turnDirectionCode) {
if (turnDirectionCode == NEVER_TURN) return "NEVER_TURN";
if (turnDirectionCode == TURN_RIGHTWARD) return "TURN_RIGHTWARD";
if (turnDirectionCode == TURN_LEFTWARD) return "TURN_LEFTWARD";
return "ILLEGAL turnDirectionCode!!!";
} // convertToTurnDirection
} // TrafficTesterView
////////////////// Example of sample use of the above program //////////////////
// except for the fact that all the following lines have been made comments, //
// and also the actual phrase "additional blocks to travel" in the data //
// shown below has been shortened to the phrase "additional blocks" to fit //
// the lines below into the required 80-columns per line. //
////////////////////////////////////////////////////////////////////////////////
//numIntersectionsInOneDirection:
//1
//number of cars created for the test:
//2
//car number:
//1
//born in the lane that is positioned at col:
//1
//born in the lane that is positioned at row:
//1
//born in the lane that is oriented in direction:
//0
//additional blocks prior to turning (-1 means the car will never turn):
//0
//direction the car will turn, when the car turns:
//1
//car number:
//2
//born in the lane that is positioned at col:
//1
//born in the lane that is positioned at row:
//1
//born in the lane that is oriented in direction:
//3
//additional blocks prior to turning (-1 means the car will never turn):
//-1
//direction the car will turn, when the car turns:
//0
//hopper2{acharles}2484: java TrafficTesterView < data1_for_TrafficTesterView
//The number of intersections in one direction is: 1
//The number of cars is: 2
//Car #1
// is born in the lane located at col 1 and row 1, that aims SOUTHWARD,
// and has 0 block(s) to go before turning
// and plans to TURN_RIGHTWARD
//Car #2
// is born in the lane located at col 1 and row 1, that aims WESTWARD,
// and has -1 block(s) to go before turning
// and plans to NEVER_TURN
//
<UNIX-PROMPT> cat TrafficTesterModel.java
// Programmers: GOATS team: Alex David Rex Tom Zihao
// TrafficTesterModel.java
// Latest Enhancement: Corrected comments
//////////////// 80 characters /////////////////////////////////////////////////
import java.util.ArrayList;
public class TrafficTesterModel{
Grid g;
//int t;
//************Constructor************
public TrafficTesterModel(int numRows, int numCols,
ArrayList< ArrayList<Integer> > carParameters){
//System.out.println("In TrafficTesterModel(): ");
//********Creating a new grid********
//System.out.println(" Constructing a Grid...");
g = new Grid(numRows, numCols);
//********Teling the grid to make and insert cars********
//System.out.println(" Telling the grid to make and insert cars...");
g.insertCars(carParameters);
//System.out.println("Exiting TrafficTesterModel()");
}
public void run(){
//System.out.println("In TrafficTesterModel: running simulation");
g.update();
}
}
<UNIX-PROMPT> cat Grid.java
// Programmers: GOATS team: Alex David Rex Tom Zihao
// Grid.java
// Latest Enhancement: Fixed blank lines in output
//////////////// 80 characters /////////////////////////////////////////////////
import java.util.*;
import java.io.*;
public class Grid
{
public static final int SOUTHWARD = 0;
public static final int EASTWARD = 1;
public static final int NORTHWARD = 2;
public static final int WESTWARD = 3;
// Store Intersections in a 2D array:
// Since Java is 0-indexed, Intersection (1,2) is i[0][1]
private Intersection i[][];
//Constuctor
public Grid(int numRows, int numCols)
{
// Allocate space for numRows*numCols intersections
// In nested for loops, construct lanes and intersections appropriately
i = new Intersection[numRows][numCols];
Lane inLanes[];
Lane outLanes[];
for(int rowNum = 0; rowNum < numRows; rowNum++)
{
for(int colNum=0; colNum<numCols; colNum++)
{
//System.out.print("Constructing Intersection ("+(rowNum+1)+
// ","+(colNum+1)+")...");
// Construct the right amount of lanes for each intersection
// There's a pattern:
// 3 2 2 2
// 3 2 2 2
// 3 2 2 2
// 4 3 3 3
inLanes = new Lane[4];
outLanes = new Lane[4];
if(rowNum==0){
if(colNum==0){
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
inLanes[cardinalDir] = new Lane();
outLanes[cardinalDir] = new Lane();
}
} else { //rowNum==0, colNum > 0
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==WESTWARD){
outLanes[WESTWARD] =
i[rowNum][colNum-1].getInLane(WESTWARD);
} else {
outLanes[cardinalDir] = new Lane();
}
}
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==EASTWARD){
inLanes[EASTWARD] =
i[rowNum][colNum-1].getOutLane(EASTWARD);
} else {
inLanes[cardinalDir] = new Lane();
}
}
}
} else { //rowNum > 0
if(colNum==0){
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==SOUTHWARD){
outLanes[SOUTHWARD] =
i[rowNum-1][colNum].getInLane(SOUTHWARD);
} else {
outLanes[cardinalDir] = new Lane();
}
}
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==NORTHWARD){
inLanes[NORTHWARD] =
i[rowNum-1][colNum].getOutLane(NORTHWARD);
} else {
inLanes[cardinalDir] = new Lane();
}
}
} else { //rowNum > 0, colNum > 0
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==SOUTHWARD){
outLanes[SOUTHWARD] =
i[rowNum-1][colNum].getInLane(SOUTHWARD);
} else if(cardinalDir==WESTWARD){
outLanes[WESTWARD] =
i[rowNum][colNum-1].getInLane(SOUTHWARD);
} else {
outLanes[cardinalDir] = new Lane();
}
}
for(int cardinalDir=0; cardinalDir<4; cardinalDir++){
if(cardinalDir==NORTHWARD){
inLanes[NORTHWARD] =
i[rowNum-1][colNum].getOutLane(NORTHWARD);
} else if(cardinalDir==EASTWARD){
inLanes[EASTWARD] =
i[rowNum][colNum-1].getOutLane(EASTWARD);
} else {
inLanes[cardinalDir] = new Lane();
}
}
}
}
//end lane construction
i[rowNum][colNum] = new Intersection(inLanes,outLanes);
//System.out.println("Success!");
}
}
// The above code constructs lanes and intersections, populates grid */
for(int colNum=0; colNum < numCols; colNum++)
{
i[0][colNum].getOutLane(SOUTHWARD).setIsBoundary();
//bottom row exits SOUTH
i[numRows-1][colNum].getOutLane(NORTHWARD).setIsBoundary();
//top row exits NORTH
}
for(int rowNum=0; rowNum < numRows; rowNum++)
{
i[rowNum][0].getOutLane(WESTWARD).setIsBoundary();
//leftmost col exits WEST
i[rowNum][numCols-1].getOutLane(EASTWARD).setIsBoundary();
//rightmost col exits WEST
}
}
public void insertCars(ArrayList< ArrayList<Integer> > carParameters)
{
//Constructs cars w/ carParameters
for( ArrayList<Integer> d:carParameters )
{
int [] temp = new int[6];
for(int i = 0; i < d.size(); i++)
{
temp[i] = d.get(i);
}
Car c = new Car(temp[4], temp[5], temp[0]);
this.insertCar(temp[1], temp[2], temp[3], c);
//ArrayList carParameters looks like this:
//[0]int carID, [1]int row, [2]int col,
//[3]int laneDirectionCode, [4]int numBlocksBeforeTurning,
//[5]int turnDirectionCode
}
}
public void update()
{
for (int rowNum = 0; rowNum < i.length; rowNum++){
for (int colNum = 0; colNum < i[0].length; colNum++){
System.out.println("");
System.out.println("At the intersection located at col " +
(colNum+1) + " and row " + (rowNum+1));
i[rowNum][colNum].visit();
}
}
}
private void insertCar(int row, int col, int laneDir, Car c)
{
(i[row-1][col-1].getInLane(laneDir)).add(c);
}
}
<UNIX-PROMPT> cat Intersection.java
// Programmers: GOATS team: Alex David Rex Tom Zihao
// Intersection.java
// Latest Enhancement: Fixed comments
//////////////// 80 characters /////////////////////////////////////////////////
//Intersection Class
//An intersection will contain 2 Lane[]'s of size 4, for a total of 8 lanes.
//4 are for inflowing lanes, the other 4 are for outflowing lanes.
//Every car within the inLane array will be evaluated and moved to the proper
//lane within the outLane[], which will put it in the inLane[] for a different
//intersection.
public class Intersection{
//**************Instance Variables**************
public Lane[] inLane;
public Lane[] outLane;
//**************Constructor**************
public Intersection(Lane[] inLanes, Lane[] outLanes){
this.inLane = inLanes;
this.outLane = outLanes;
}
//**************Instance Methods**************
// Getter methods to get references to incoming, outgoing lanes
public Lane getInLane(int laneDir){ return inLane[laneDir]; }
public Lane getOutLane(int laneDir){ return outLane[laneDir]; }
//Looks at each lane in inLane and moves the car in that lane
//(if it has one) into the appropriate outLane spot
// Print the empty/nonempty status of incoming lanes, and when cars moved
public void visit(){
for (int i = 0; i <= 3; i++){
Car c = inLane[i].get();
if (c != null){
System.out.println(" incoming lane having direction " +
TrafficTesterView.convertToLaneDirection(i) +
" is nonempty and");
int dir = c.getCurrentDirection();
c.update();
//If the car is coming from the North entry point
if (i == 0){
if (dir == 0){//Straight
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(0));
outLane[0].add(c);
}
if (dir == 1){//right
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(3));
outLane[3].add(c);
}
if (dir == -1){//left
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(1));
outLane[1].add(c);
}
}
//If the car is coming from the West entry point
if (i == 1){
if (dir == 0){//Straight
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(1));
outLane[1].add(c);
}
if (dir == 1){//right
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(0));
outLane[0].add(c);
}
if (dir == -1){//left
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(2));
outLane[2].add(c);
}
}
//If the car is coming from the South entry point
if (i == 2){
if (dir == 0){//Straight
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(2));
outLane[2].add(c);
}
if (dir == 1){//right
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(1));
outLane[1].add(c);
}
if (dir == -1){//left
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(3));
outLane[3].add(c);
}
}
//If the car is coming from the East entry point
if (i == 3){
if (dir == 0){//Straight
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(3));
outLane[3].add(c);
}
if (dir == 1){//right
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(2));
outLane[2].add(c);
}
if (dir == -1){//left
System.out.println(" car#" +
c.getID() +
" is removed and placed into " +
"outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(0));
outLane[0].add(c);
}
}
} else {
System.out.println(" incoming lane having direction " +
TrafficTesterView.convertToLaneDirection(i) +
" is empty");
}
}
// Now all the incoming cars have been moved to outLanes
// Iterate through the outgoing lanes, printing empty/nonempty
for(int l=0; l<4; l++){
if( outLane[l].isEmpty() ){
System.out.println(" outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(l) +
" is empty");
} else {
System.out.println(" outgoing lane having direction " +
TrafficTesterView.convertToLaneDirection(l) +
" is nonempty");
}
}
}
}
<UNIX-PROMPT> cat Lane.java
// Programmers: GOATS team: Alex David Rex Tom Zihao
// Lane.java
// Latest Enhancement: Fixed comments
//////////////// 80 characters /////////////////////////////////////////////////
import java.util.Queue;
import java.util.LinkedList;
//Lane Class
//This class defines a "Lane" object, which will hold cars.
//Lanes can be either incoming or outgoing depending on their direction in
//in relation to intersections.
//Each lane will belong to 2 intersections: as an incoming lane for one
//and an outgoing lane for another
public class Lane{
//**************Instance Variables**************
public Queue<Car> q;
public boolean boundary;
//**************Constructor**************
public Lane(){
q = new LinkedList<Car>();
boundary = false;
}
//**************Instance Methods**************
//Removes a car from a lane and returns it
public Car get(){
return q.poll();
}
//Adds a provided car c to the lane
public boolean add(Car c){
if (this.boundary == true){
System.out.println(" car#" + c.getID() + " leaves the grid");
}
return q.add(c);
}
public boolean isEmpty(){
return (q.peek() == null);
}
public void setIsBoundary(){
this.boundary = true;
}
}
<UNIX-PROMPT> cat Car.java
// Programmers: GOATS team: Alex David Rex Tom Zihao
// Car.java
// Latest Enhancement: Fixed comments
//////////////// 80 characters /////////////////////////////////////////////////
public class Car{
//**************Instance Variables**************
int numBlocksBeforeTurning;
int turnDirectionCode;
int carID;
//**************Constructor**************
public Car(int initialNumBlocks, int turnDirectionCode, int carID){
this.numBlocksBeforeTurning = initialNumBlocks;
this.turnDirectionCode = turnDirectionCode;
this.carID = carID;
}
//**************Instance Methods**************
//After a car moves from one lane to another, update() is called to
//decrement the numBlocksBeforeTurning
public void update(){
this.numBlocksBeforeTurning--;
}
//Determines which direction a car will be moving.
//If its numBlocksBeforeTurning is 0, the it will return the car's
//turnDirectinoCode.
//Otherwise, it will return -1, which indicates it is moving straight
public int getCurrentDirection(){
if (this.numBlocksBeforeTurning == 0){
return this.turnDirectionCode;
}
else{
return 0;
}
}
public int getID(){
return this.carID;
}
}
<UNIX-PROMPT> /bin/rm *class
<UNIX-PROMPT> javac TrafficTesterView.java
<UNIX-PROMPT> ls -tl *class
-rw-r--r-- 1 david staff 557 Nov 4 13:40 Car.class
-rw-r--r-- 1 david staff 2953 Nov 4 13:40 Grid.class
-rw-r--r-- 1 david staff 2679 Nov 4 13:40 Intersection.class
-rw-r--r-- 1 david staff 1214 Nov 4 13:40 Lane.class
-rw-r--r-- 1 david staff 549 Nov 4 13:40 TrafficTesterModel.class
-rw-r--r-- 1 david staff 1774 Nov 4 13:40 TrafficTesterView.class
<UNIX-PROMPT> cat data2_for_TrafficTesterView
numIntersectionsInOneDirection:
2
number of cars created for the test:
6
car number:
1
born in the lane that is positioned at col:
2
born in the lane that is positioned at row:
2
born in the lane that is oriented in direction:
0
additional blocks to travel prior to turning (-1 means the car will never turn):
-1
direction the car will turn, when the car turns:
0
car number:
2
born in the lane that is positioned at col:
1
born in the lane that is positioned at row:
2
born in the lane that is oriented in direction:
3
additional blocks to travel prior to turning (-1 means the car will never turn):
-1
direction the car will turn, when the car turns:
0
car number:
3
born in the lane that is positioned at col:
1
born in the lane that is positioned at row:
2
born in the lane that is oriented in direction:
2
additional blocks to travel prior to turning (-1 means the car will never turn):
0
direction the car will turn, when the car turns:
1
car number:
4
born in the lane that is positioned at col:
1
born in the lane that is positioned at row:
1
born in the lane that is oriented in direction:
1
additional blocks to travel prior to turning (-1 means the car will never turn):
1
direction the car will turn, when the car turns:
1
car number:
5
born in the lane that is positioned at col:
2
born in the lane that is positioned at row:
1
born in the lane that is oriented in direction:
2
additional blocks to travel prior to turning (-1 means the car will never turn):
1
direction the car will turn, when the car turns:
-1
car number:
6
born in the lane that is positioned at col:
2
born in the lane that is positioned at row:
1
born in the lane that is oriented in direction:
0
additional blocks to travel prior to turning (-1 means the car will never turn):
0
direction the car will turn, when the car turns:
-1
<UNIX-PROMPT> java TrafficTesterView < data2_for_TrafficTesterView
At the intersection located at col 1 and row 1
incoming lane having direction SOUTHWARD is empty
incoming lane having direction EASTWARD is nonempty and
car#4 is removed and placed into outgoing lane having direction EASTWARD
incoming lane having direction NORTHWARD is empty
incoming lane having direction WESTWARD is empty
outgoing lane having direction SOUTHWARD is empty
outgoing lane having direction EASTWARD is nonempty
outgoing lane having direction NORTHWARD is nonempty
outgoing lane having direction WESTWARD is empty
At the intersection located at col 2 and row 1
incoming lane having direction SOUTHWARD is nonempty and
car#6 is removed and placed into outgoing lane having direction EASTWARD
car#6 leaves the grid
incoming lane having direction EASTWARD is nonempty and
car#4 is removed and placed into outgoing lane having direction SOUTHWARD
car#4 leaves the grid
incoming lane having direction NORTHWARD is nonempty and
car#5 is removed and placed into outgoing lane having direction NORTHWARD
incoming lane having direction WESTWARD is empty
outgoing lane having direction SOUTHWARD is nonempty
outgoing lane having direction EASTWARD is nonempty
outgoing lane having direction NORTHWARD is nonempty
outgoing lane having direction WESTWARD is empty
At the intersection located at col 1 and row 2
incoming lane having direction SOUTHWARD is empty
incoming lane having direction EASTWARD is empty
incoming lane having direction NORTHWARD is nonempty and
car#3 is removed and placed into outgoing lane having direction EASTWARD
incoming lane having direction WESTWARD is nonempty and
car#2 is removed and placed into outgoing lane having direction WESTWARD
car#2 leaves the grid
outgoing lane having direction SOUTHWARD is empty
outgoing lane having direction EASTWARD is nonempty
outgoing lane having direction NORTHWARD is empty
outgoing lane having direction WESTWARD is nonempty
At the intersection located at col 2 and row 2
incoming lane having direction SOUTHWARD is nonempty and
car#1 is removed and placed into outgoing lane having direction SOUTHWARD
incoming lane having direction EASTWARD is nonempty and
car#3 is removed and placed into outgoing lane having direction EASTWARD
car#3 leaves the grid
incoming lane having direction NORTHWARD is nonempty and
car#5 is removed and placed into outgoing lane having direction WESTWARD
incoming lane having direction WESTWARD is empty
outgoing lane having direction SOUTHWARD is nonempty
outgoing lane having direction EASTWARD is nonempty
outgoing lane having direction NORTHWARD is empty
outgoing lane having direction WESTWARD is nonempty
0;david@mactop: ~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-projdavid@mactop:~/Documents/UR/y3s1/cs322/proj/code/cs322-traffic-proj$ exit
exit
Script done on Fri Nov 4 13:41:24 2016