A game stores each player’s tag and score. Two players form a team when the 3-digit number in their tags are the same.
A tag has the form: NAME-NUMBER
For example: PRO-007
The name contains uppercase letters and the number contains exactly 3 digits.
Design a class PlayerAnalyzer to accept N players (5 ≤ N ≤ 15).
- Check each tag and display:
“Valid Tag” if it follows the given format. “Invalid Tag” otherwise. - Find Teammates: Players with the same 3-digit number form a team. Display teams having exactly two players, along with their combined score. Consider only valid tags.
Players without a matching teammate need not be displayed. If no valid pair of players forms a team, display NO COMPLETE TEAMS FOUND.
Find the Top Scorer: Among players with valid tags, find the highest score and display the player(s) who achieved it. If there are no valid tags, display: NO VALID TAGS FOUND.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 5
tag = {“PRO-117”, “ACE-112”, “MAX-117”, “ZEN-115”, “BAD-12”}
score = {850, 720, 910, 800, 950}
OUTPUT:
Valid Tag: PRO-117
Valid Tag: ACE-112
Valid Tag: MAX-117
Valid Tag: ZEN-115
Invalid Tag: BAD-12
TEAMS: PRO-117, MAX-117
Combined Score: 1760
TOP-SCORER: MAX-117 – 910
Example 2
INPUT:
N = 5
tag = {“PRO-117”, “MAX-910”, “ACE-115”, “ZEN-120”, “RAY-225”}
score = {850, 910, 720, 800, 650}
OUTPUT: TEAMS: NO COMPLETE TEAMS FOUND
Example 3
INPUT: N = 3
OUTPUT: INVALID INPUT
import java.util.*;
class PlayerAnalyzer{
int n;
String tag[];
int score[];
public PlayerAnalyzer(int nn){
n = nn;
tag = new String[n];
score = new int[n];
}
public void accept(){
Scanner sc = new Scanner(System.in);
for(int i = 0; i < n; i++){
System.out.print("Enter tag: ");
tag[i] = sc.nextLine();
System.out.print("Enter score: ");
score[i] = Integer.parseInt(sc.nextLine());
}
}
public boolean isValid(String s){
int p = s.indexOf('-');
if(p <= 0 || p != s.lastIndexOf('-'))
return false;
String name = s.substring(0, p);
String number = s.substring(p + 1);
for(int i = 0; i < name.length(); i++){
char ch = name.charAt(i);
if(ch < 'A' || ch > 'Z')
return false;
}
if(number.length() != 3)
return false;
for(int i = 0; i < number.length(); i++) {
if(!Character.isDigit(number.charAt(i)))
return false;
}
return true;
}
public void displayValidity(){
for (int i = 0; i < n; i++){
if (isValid(tag[i]))
System.out.println("Valid Tag: " + tag[i]);
else
System.out.println("Invalid Tag: " + tag[i]);
}
}
public void findTeams(){
boolean found = false;
System.out.println("TEAMS:");
for(int i = 0; i < n; i++){
if(!isValid(tag[i]))
continue;
int count = 0;
int partner = -1;
String num1 = tag[i].substring(tag[i].indexOf('-') + 1);
for(int j = 0; j < n; j++) {
if (i != j && isValid(tag[j])){
String num2 = tag[j].substring(tag[j].indexOf('-') + 1);
if(num1.equals(num2)){
count++;
partner = j;
}
}
}
if (count == 1 && i < partner){
found = true;
System.out.println(tag[i] + ", " + tag[partner]);
System.out.println("Combined Score: " + (score[i] + score[partner]));
}
}
if(!found)
System.out.println("NO COMPLETE TEAMS FOUND");
}
public void findTopScorer(){
int highest = Integer.MIN_VALUE;
boolean validFound = false;
for(int i = 0; i < n; i++){
if (isValid(tag[i])){
validFound = true;
if (score[i] > highest)
highest = score[i];
}
}
if (!validFound){
System.out.println("NO VALID TAGS FOUND");
return;
}
System.out.print("TOP-SCORER:");
for (int i = 0; i < n; i++){
if (isValid(tag[i]) && score[i] == highest)
System.out.println(tag[i] + " - " + score[i]);
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter N: ");
int n = sc.nextInt();
if (n < 5 || n > 15) {
System.out.println("INVALID INPUT");
return;
}
PlayerAnalyzer obj = new PlayerAnalyzer(n);
obj.accept();
obj.displayValidity();
obj.findTeams();
obj.findTopScorer();
}
}