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

PLEASE SOLVE IN JAVA Problem 1 Consider a solitaire matching game in which you h

ID: 3802754 • Letter: P

Question

PLEASE SOLVE IN JAVA

Problem 1

Consider a solitaire matching game in which you have a list of random integer values between 10 and 99. You remove from the list any pair of consecutive integers whose first or second digits match. If all values are removed, then you win.

For example, consider the following sequence of 10 integers:

10 82 43 23 89 12 43 84 23 32

The integers in the pair 10 and 82 do not match in either digit and so cannot be removed. However, the integers in the pair 43 and 23 match in the second digit and are removed, leaving the following sequence:

10 82 89 12 43 84 23 32

Continue checking for pairs from 89, the value after the removed pair. No other pairs have matching integers. Now return to the beginning of the list and check the pairs. The integers in the pair 82 and 89 match in the first digit and can be removed:

10 12 43 84 23 32

No other pairs can be removed, so we lose.

Write a program that simulates this game.

Implement the method

public static void initializeList(ArrayListWithListIterator theList)

which generates 40 random two-digit integers and place them in an instance of ArraListWithListIterator, using an instance of ListIterator. (20 points)

Implement the method

public static boolean scanAndRemovePairs(ArrayListWithListIterator theList)

which, using an iterator, scans the list and removes matching pairs of values (20 points).

To check whether two integers can be removed, implement the method

public static boolean removable(Integer x, Integer y)

which returns true if the 2-digit integers x and y share at least one digit in common (20 points).

Implement the method

public static void displayList(ArrayListWithListIterator theList)

which displays the contents of theList using an iterator (20 points).

Implement the method

public static void main(String args[])

to create and initialize a ArraListWithListIterator by calling initializeList and repeatedly callingscanAndRemovePairs until either the list is empty (print that the list is empty), or we cannot remove more pairs (print that no more pairs can be removed). After each pair is removed, call displayList to show the contents of the list. (20 points).

View comments (1)

Explanation / Answer

import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;

public class Game {

   public static void main(String[] args) {
       ArrayList<Integer> theList = new ArrayList<>();
       initializeList(theList);
       displayList(theList);
       scanAndRemovePairs(theList);
       displayList(theList);
      
   }
   public static void initializeList(ArrayList<Integer> theList){
       Random rand = new Random();
       int minRand=10;
       int maxRand=99;
       int rang = maxRand-minRand;
       for(int i=0;i<40;i++){
           int randNum = Math.abs(rand.nextInt(rang))+minRand;
           theList.add(randNum);
       }
   }
   public static boolean scanAndRemovePairs(ArrayList<Integer> theList){
      
       while(true){
           ListIterator<Integer> lItr = theList.listIterator();
           int num1;// = lItr.next();
           int num2;
           boolean isRemoved = false;
           while(lItr.hasNext()){
               num1 = lItr.next();
               if(!lItr.hasNext()){
                   break;
               }
               num2 = lItr.next();
               if(removable(num1, num2)){
                   System.out.println("Removed n1="+num1+" n2="+num2);
                   lItr.remove();
                   lItr.previous();
                   lItr.remove();
                   isRemoved = true;
                   continue;
               }
           }
           if(!isRemoved){
               break;
           }
       }
       if(theList.size()==0)
           return true;
       return false;
   }
   public static boolean removable(Integer x, Integer y){
       if(x/10 == y/10 || x%10 ==y%10){
           return true;
       }
       return false;
   }
   public static void displayList(ArrayList<Integer> theList){
       ListIterator<Integer> lItr = theList.listIterator();
       System.out.print(" [ ");
       while(lItr.hasNext()){
           System.out.print(" "+lItr.next()+" ");
       }
       System.out.print(" ] ");
   }
}

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