Question

Predict the output from the following program. Select all lines of output that are produced by...

Predict the output from the following program. Select all lines of output that are produced by this program. If there are computed values in an output line, you must select the line with the correct value as computed by the program.

public class WalkToSchool20 {

        public static void main (String[] args) {

                Pedestrian p1 = new Pedestrian ( 1 );

                Pedestrian p2 = new Pedestrian ( 2 );

                Pedestrian p3 = new Pedestrian ( 3 );

                p1.start();

                p2.start();

                p3.start();

                p2.walk (64);

                p3.walk (16);

                p1.walk (8);

                p3.getTicket (45);

                p2.takeDetour ();

                p3.takeDetour ();

                p2.getTicket (128);

                p1.walk (4);

                p2.walk (16);

                p3.walk (96);

                p2.walk (14);

                p1.interrupt ();

                p1.walk (75);

                p3.stopWalking();

                p2.stopWalking();

                p1.stopWalking();

                try {

                        p1.join();

                        p2.join();

                        p3.join();

                }

                catch (InterruptedException e) {}

                System.out.println("Total time = " +

                        (p1.gettime() + p2.gettime() + p3.gettime()));

        }

} // WalkToSchool20  

class Pedestrian extends Thread {

        private static final int DETOUR_DELAY = 48;

        private int id;

        private int time;

        private boolean walking;

        Pedestrian (int id) {

                this.id = id;

                this.time = 0;

                this.walking = true;

        }

        public void walk (int t) {

                time = time + id*t;

        }

        public void takeDetour () {

                time = time + id * DETOUR_DELAY;

        }

        public void stopWalking () {

                walking = false;

        }

        public void getTicket (int t) {

                time = time + t;

        }

        public int gettime () {

                return time;

        }

        public void run () {

                while (walking) {

                        Thread.yield();

                }

                if (interrupted())

                        System.out.println ("Pedestrian " + id +

                                ": did not complete the trip");

                else

                        System.out.println ("Pedestrian " + id +

                                ": walking time = " + time);

        }

} // Pedestrian

Question 6 options:

Pedestrian 3: did not complete the trip

Pedestrian 2: walking time = 384

Pedestrian 3: walking time = 525

Pedestrian 1: did not complete the trip

Pedestrian 2: walking time = 412

Pedestrian 3: walking time = 445

Pedestrian 2: did not complete the trip

Total time = 1024

Total time = 841

Please help due within 30 mins

Homework Answers

Answer #1

The output will be

Pedestrian 3: walking time = 525

Pedestrian 1: did not complete the trip

Pedestrian 2: walking time = 412

Total time = 1024

because for p1 in the program we have p1.interrupt()This will be the output for the given program

when we have interrupt we are just displaying that the pedestrain did not complete the trip

remaining pedestrain 2 and 3 completes within 412 and 525

the order may be changed because of threads

i.e. pedestrain order will be changed because of parallel running of threads

but the time will be same

hope this is helpful

Please please upvote it helps me alot

Please dont downvote

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document)...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document) in the 'if' statement if (targets.indexOf(shot) > -1) { ADD a NEW ArrayList of points called 'misses' ADD code to add a point to the 'misses' list on a miss (if a SHOT does NOT hit a TARGET) ADD code to place an 'M' in the final output map for all shots that were MISSES. ADD code to place an H on the target...
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class...
Mention the concepts analyzed from the below given program and write the output. [0.5 M] class Test { int a, b; Test(int i, int j) { a = i; b = j; } void meth(Test s) { s.a *= 2; s.b /= 2; } } class demo { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("Before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("After call: " + ob.a + " " +...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?-----...
int a =12; a += 3; System.out.println(a); // What is the output of the above program?----- --------------- the Java keyword "new" accomplishes what? starts a new program gets input creates an object in memory refresh variable values ------------------------ Every Java Class MUST contain the main method: 'public static void main(String[]args)'   true/false
What is the output of the following Java program? interface Food {     public void printFlavor();...
What is the output of the following Java program? interface Food {     public void printFlavor(); } class Pepper implements Food {     public void printFlavor() { System.out.println("spicy"); } } public class Lunch {     public static void main(String[] args) {        Food pepper = new Pepper();         pepper.printFlavor();     } } Select one: a. spicy b. no output c. the program does not compile d. bland e. bland spicy
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT