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

need help about Arrays in JAVA.THX. Q:Write a method to determine how many numbe

ID: 3804827 • Letter: N

Question

need help about Arrays in JAVA.THX.

Q:Write a method to determine how many numbers in one array are in another.

1.The method is passed an array that holds the winning lottery numbers and an array that holds some person's lottery ticket numbers. The method figures out how many winning numbers the person has on their ticket.

2.the method header is: public int countWinningNumbersOnTicket(int[] winningNumbers, int[] lotteryTicket)

Note: If you want to test this method by invoking it from main, you will need to add "static" to the method header (public static double...)

you can assume the arrays have the same length

the arrays are not sorted

there are no duplicate numbers in either array

examples:

winning numbers [61, 51, 91, 3, 24], lotteryTicket [24, 14, 61, 52, 92] return 2 (because the lottery ticket has two winning numbers- 24 and 61)

winning numbers [61, 51, 91, 3, 24], lotteryTicket [34, 14, 68, 52, 92] return 0 (because the lottery ticket has no winning numbers)

winning numbers [61, 51, 91, 3, 24], lotteryTicket [24, 3, 61, 91, 51] returns 5 (because the lottery ticket has tall five numbers- they hit the jackpot!)

Explanation / Answer


// Lottery.java
import java.io.*;
class Lottery
{
   public static int countWinningNumbersOnTicket(int[] winningNumbers, int[] lotteryTicket)
   {
      int count = 0;

      // iterate over winnign numbers
      for (int i = 0 ;i < winningNumbers.length; i++ )
      {
          // check if winning number are in lottery
          for (int j = 0; j < lotteryTicket.length; j++ )
          {
              if(winningNumbers[i] == lotteryTicket[j])
              {
                  count++;
                  // brewak if any one instance of winner number is found in lottery
                  break;
              }
          }
      }

      return count;
   }
   public static void main(String args[])throws IOException
   {
       int[] winningNumbers1 = {61, 51, 91, 3, 24};
       int[] lotteryTicket1 = {24, 14, 61, 52, 92};
       System.out.println(countWinningNumbersOnTicket(winningNumbers1,lotteryTicket1));
       //output: 2
     
      int[] winningNumbers2 = {61, 51, 91, 3, 24};
       int[] lotteryTicket2 = {34, 14, 68, 52, 92};
       System.out.println(countWinningNumbersOnTicket(winningNumbers2,lotteryTicket2));
       //output: 0

       int[] winningNumbers3 = {61, 51, 91, 3, 24};
       int[] lotteryTicket3 = {24, 3, 61, 91, 51};
       System.out.println(countWinningNumbersOnTicket(winningNumbers3,lotteryTicket3));
       //output: 5
   }
}