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 named Player inside Main.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 a Player object.It assigns the given name and score values to the instance variables.

  • public void display(): Method to print the player’s name and score.Uses System.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 a Player object named p1 with name "Aria" and score 50.

  • Player p2 = new Player("Luca", 70);: Creates another Player object named p2 with name "Luca" and score 70.

  • p1.display(); and p2.display();: Call the display() 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 named Player inside Main.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 a Player object.It assigns the given name and score values to the instance variables.

  • public void display(): Method to print the player’s name and score.Uses System.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 a Player object named p1 with name "Aria" and score 50.

  • Player p2 = new Player("Luca", 70);: Creates another Player object named p2 with name "Luca" and score 70.

  • p1.display(); and p2.display();: Call the display() method for each player to print their details.