Solution: Add a Method to Your Class
public class Main: Defines the main class that contains the program’s entry point.public static class Player: Defines a static inner class namedPlayerinsideMain.This class represents a player with a name and a score.String name; int score;: Declare two instance variables:name→ stores the player's name.score→ stores the player's score.public Player(String name, int score): Constructor for initializing aPlayerobject.It assigns the givennameandscorevalues to the instance variables.public void display(): Method to print the player’s name and score.UsesSystem.out.println(name + " scored " + score);to display the message.public static void main(String[] args): The entry point of the program.Player p1 = new Player("Aria", 50);: Creates aPlayerobject namedp1with name"Aria"and score50.Player p2 = new Player("Luca", 70);: Creates anotherPlayerobject namedp2with name"Luca"and score70.p1.display();andp2.display();: Call thedisplay()method for each player to print their details.
Solution: Add a Method to Your Class
public class Main: Defines the main class that contains the program’s entry point.public static class Player: Defines a static inner class namedPlayerinsideMain.This class represents a player with a name and a score.String name; int score;: Declare two instance variables:name→ stores the player's name.score→ stores the player's score.public Player(String name, int score): Constructor for initializing aPlayerobject.It assigns the givennameandscorevalues to the instance variables.public void display(): Method to print the player’s name and score.UsesSystem.out.println(name + " scored " + score);to display the message.public static void main(String[] args): The entry point of the program.Player p1 = new Player("Aria", 50);: Creates aPlayerobject namedp1with name"Aria"and score50.Player p2 = new Player("Luca", 70);: Creates anotherPlayerobject namedp2with name"Luca"and score70.p1.display();andp2.display();: Call thedisplay()method for each player to print their details.