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

There are Four players numbered 0, 1, 2, 3 Four batteries numbered 0, 1, 2, 3 Fo

ID: 3538534 • Letter: T

Question

There are

Four players numbered 0, 1, 2, 3

Four batteries numbered 0, 1, 2, 3

Four game consoles numbered 0, 1, 2, 3

For each i=0, 1, 2, 3, player i has access to game console i as well as batteries i and (i+1) mod 4


For each i=0, 1, 2, 3, player i may

install batteries i and (i+1) mod 4 into game console i

Play on game console i, thus discharging each of batteries i and(i+1) mod 4 once

Put batteries i and (i+1) mod 4 back on the table

A battery cannot be simultaneously installed into two distinct game consoles


For each i=0, 1, 2, 3, player i quits precisely when at least one of batteries i and (i+1) mod 4 is completely discharged



Requirements

1.Do NOT modify TestPlay.java

2.I will do the following several times:

javac TestPlay.java Homework2.java

java TestPlay

3.If every run takes less than 5 seconds on my computer and there are no error messages, then your program passes the test

4.I may modify the arguments passed by your program%u2019s invocations to Thread.sleep

5.I may add to your program a few try-catch blocks containing invocations to Thread.sleep

Each such invocation will pass small arguments

6.Then I will retest your program in the aforementioned way




//please modify only class Homework2 implements Runnable


import java.util.Random;


final class Battery

{

  private int capacity;

  private final int number;

  private boolean occupied;


  public Battery(int capacity, int number)

{

  this.capacity = capacity;

  this.number = number;

}

  

  public int getCapacity()

{

  return capacity;

}

  

  public int getNumber()

{

  return number;

}

  

  public boolean isOccupied()

{

  return occupied;

}

  

  public void setOccupied(boolean occupied)

{

  this.occupied = occupied;

}


  public void discharge()

{

  try

{

  int temp = capacity;

Thread.sleep(0, new Random().nextInt(1) + 1);

  capacity = temp - 1;

} catch (InterruptedException e)

{

System.err.println("Error: Interrupted");

}

}

}


final class GameConsole

{

  private int timesPlayed;

  

  public int getTimesPlayed()

{

  return timesPlayed;

}


  public void play(Battery batt1, Battery batt2)

{

batt1.discharge();

batt2.discharge();

  timesPlayed++;

}

}


public class TestPlay

{

  public static final int BATTERY_CAPACITY = 6;

  public static final int NUM = 4;

  private final Battery[] batts = new Battery[NUM];

  private final GameConsole[] cons = new GameConsole[NUM];

  private final Thread[] thrs = new Thread[NUM];

  

  

  public TestPlay() throws InterruptedException

{

  int usedCapacity = 0;

  int totalTimesPlayed = 0;

  

  for (int i = 0; i < NUM; ++i)

{

  batts[i] = new Battery(BATTERY_CAPACITY, i);

  cons[i] = new GameConsole();

}

  for (int i = 0; i < NUM; ++i)

{

  thrs[i] = new Thread((Runnable)new Homework2(batts[i],batts[(i+1)%NUM], cons[i]));

  thrs[i].start();

}

  for (int i = 0; i < NUM; ++i)

{

  thrs[i].join();

}

  for (int i = 0; i < NUM; ++i)

{

usedCapacity += BATTERY_CAPACITY - batts[i].getCapacity();

totalTimesPlayed += cons[i].getTimesPlayed();

  if (batts[i].getCapacity() < 0)

System.err.println("Error: Battery " + i + " has negative capacity");

  if (batts[i].getCapacity() > 0 && batts[(i+1) %NUM].getCapacity() > 0)

System.err.println("Error: Battery power remain for player " + i);

}

  if (usedCapacity != 2 * totalTimesPlayed)

System.err.println("Error: Remaining battery power incorrect");

}

  

  public static void main(String[] args) throws InterruptedException

{

System.out.println("Starting the test...");

  new TestPlay();

System.out.println("Tested!");

System.out.println("");

}

}


//please modify codes below

class Homework2 implements Runnable

{

Battery batt1;

Battery batt2;

GameConsole cons;


  public Homework2(Battery batt1, Battery batt2, GameConsole cons)

{

  this.batt1 = batt1;

  this.batt2 = batt2;

  this.cons = cons;

}

  public void run()

{

  boolean flag = true;

while( flag )

{

  synchronized (batt1)

{

  synchronized (batt2)

{

  if (batt1.getCapacity() > 0 && batt2.getCapacity() > 0)

  cons.play(batt1, batt2);

  else

flag = false;

}

}

}

}

}

Explanation / Answer

import java.util.Random;


class Homework2 implements Runnable

{

//for( int i=0; i<100; i++ )

Battery batt1;

Battery batt2;

GameConsole cons;


public Homework2(Battery batt1, Battery batt2, GameConsole cons)

{

this.batt1 = batt1;

this.batt2 = batt2;

this.cons = cons;

}

public void run()

{

boolean flag = true;

if( !batt1.isOccupied() && !batt2.isOccupied() )

while( flag )

{

batt1.setOccupied(true);

batt2.setOccupied(true);

synchronized (batt1)

{

synchronized (batt2)

{

if (batt1.getCapacity() > 0 && batt2.getCapacity() > 0)

cons.play(batt1, batt2);

else

flag = false;

batt1.setOccupied(false);

batt2.setOccupied(false);

}

}

}

}

}