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

You invited a friend to play console games. You have two joysticks and only one

ID: 3583806 • Letter: Y

Question

You invited a friend to play console games. You have two joysticks and only one charger for

them. Initially, the first joystick is charged at a1 percent and the second one is charged

at a2 percent. You can connect a charger to a joystick only at the beginning of each minute. In

one minute joystick either discharges by 2 percent (if not connected to a charger) or charges

by 1 percent (if connected to a charger).

Your can play the game while both joysticks have a positive charge. Hence, if at the

beginning of a minute some joystick is charged by 1 percent, it has to be connected to a

charger, otherwise the game stops.

If some joystick completely discharges (its charge turns to 0), the game also stops. It is

prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is

allowed for the joystick to be charged by more than 100 percent.

Determine the maximum number of minutes that game can last via dynamic programming

approach. Derive the pseudo-code of your algorithm and implement it in Java. (Dynamic

programming for Knapsack may be used for help.)

Input:

Two positive integers a1 and a2 (1!!a1,!a2!!100), the initial charge level of first and second

joystick respectively.

Output:

Output the only integer, the maximum number of minutes that the game can last. The game

continues until some joystick is discharged.

Examples:

input:

3 5

output:

6

input:

4 4

output:

5

Explanation:

In the first sample game lasts for 6 minutes by using the following algorithm:

• at the beginning of the first minute connect the first joystick to the charger, by the end of this

the minute the first joystick is at 4%, the second is at 3%;

• continue the game without changing charger, by the end of the second minute the first

the joystick is at 5%, the second is at 1%;

• at the beginning of the third minute connect the second joystick to the charger, after this

the minute the first joystick is at 3%, the second one is at 2%;

• continue the game without changing charger, by the end of the fourth minute first

the joystick is at 1%, the second one is at 3%;

• at the beginning of the fifth minute connect the first joystick to the charger, after this minute

the first joystick is at 2%, the second one is at 1%;

• at the beginning of the sixth minute connect the second joystick to the charger, after this

the minute the first joystick is at 0%, the second one is at 2%.

After that, the first joystick is completely discharged and the game is stopped.

Explanation / Answer

Answer:

import java.util.Scanner;
//Joy Stick Game For charging the two JoySticks using single charger
public class JoyStickGame {
  
       int s1,s2; //s1,s2 are joy sticks to be charged.
       int m; //minuetes for charging the joy sticks
      
       public void computeMinutes(int a1,int a2){
           m=0;
           s1=a1;
           s2=a2;
           while(true){
                   while(s2>1) firstCharge(s1,s2); //charging the first joy stick
                   while(s1>1) secondCharge(s1,s2);//charging the second joy stick.
              
                   //checking whether the charging of joystick came to zero. if yes game over
                   if(s1<=0 ||s2<=0){
                       System.out.println("Game Over and Total Minutes "+m);
                       return;
                   }
           }
       }
      
       //charging the first joy stick
       public void firstCharge(int c1,int c2){
               s1=c1+1;
               s2=c2-2;
               m++;
          
       }
      
       //charging the second joystick
       public void secondCharge(int c1,int c2){
                   s1=c1-2;
                   s2=c2+1;
                   m++;
       }
      
       //main method
   public static void main (String[] args){
   //accessing the console to get charger inputs
       Scanner scan = new Scanner(System.in);
  
       //creating the object for the joystick game
       JoyStickGame js = new JoyStickGame();
     
   //getting the first joy stick charger value
       System.out.println("Enter First Charger Level: ");
       int a1 = scan.nextInt();
      
       //validating the first joy stick charger value
       if(a1<1 && a1>100){
           System.out.println("Charger Level Not Matched(1 to 100) ");
       System.out.println("Re-Enter First Charger Level: ");
       a1 = scan.nextInt();
       }  
      
       //getting the second joy stick charger value
           System.out.println("Enter Second Charger Level: ");
       int a2 = scan.nextInt();
      
       //validating the second joystick charger value
           if(a2<1 && a2>100){
           System.out.println("Charger Level Not Matched(1 to 100) ");
       System.out.println("Re-Enter Second Charger Level: ");
       a2 = scan.nextInt();
       }  
          
           //calling computing the minutes for the game
       js.computeMinutes(a1, a2);
     
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote