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
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...
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
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...
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...
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 + " " +...
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....
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 statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program....
question : Take the recursive Java program Levenshtein.java and convert it to the equivalent C program. Tip: You have explicit permission to copy/paste the main method for this question, replacing instances of System.out.println with printf, where appropriate. You can get the assert function from assert.h. Try running the Java program on the CS macOS machines. You can compile and run the program following the instructions discussed in class. Assertions are disabled by default. You can enable assertions by running java...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT