Challenge: Add a Method to Your Class

In this challenge, you’ll build a class from scratch and give it behavior.

Your tasks

  1. Create a class named Player inside Main.

  2. Inside Player, add:

    1. a String called name

    2. an int called score

  3. Write a constructor that sets name and score when a Player is created.

  4. Add a method called display() that prints:
    name scored score

  5. In main():

    1. Create two Player objects

    2. Call display() on each one

Example output

Aria scored 50
Luca scored 70

Hint

  • Constructors have the same name as the class

  • Use this.name and this.score to store values inside the object

  • Call methods with: objectName.methodName()

Challenge: Add a Method to Your Class

In this challenge, you’ll build a class from scratch and give it behavior.

Your tasks

  1. Create a class named Player inside Main.

  2. Inside Player, add:

    1. a String called name

    2. an int called score

  3. Write a constructor that sets name and score when a Player is created.

  4. Add a method called display() that prints:
    name scored score

  5. In main():

    1. Create two Player objects

    2. Call display() on each one

Example output

Aria scored 50
Luca scored 70

Hint

  • Constructors have the same name as the class

  • Use this.name and this.score to store values inside the object

  • Call methods with: objectName.methodName()

Java
public class Main {
public static void main(String[] args) {
// Write your code here:
}
}