*The Program
class CricketPlayer {
String name;
int age;
String role;
String team;
public void getInformation() {
System.out.println(name + " " + age + " " + role + " " + team);
}
public static void main(String[] args) {
// First player
CricketPlayer player1 = new CricketPlayer();
player1.name = "Ms Dhoni";
player1.age = 43;
player1.role = "Batsman";
player1.team = "India";
player1.getInformation();
// Second player
CricketPlayer player2 = new CricketPlayer();
player2.name = "Virat Kohli";
player2.age = 36;
player2.role = "Batsman";
player2.team = "India";
player2.getInformation();
}
}
****Explanation
- Class Creation We create a class CricketPlayer with four attributes:
name → Player’s name
age → Player’s age
role → Batsman, Bowler, All-rounder, or Wicketkeeper
team → Which team they play for
- Method
getInformation() prints the details of each player.
3.Object Creation
In the main method, we create two objects (player1 and player2) and assign values.
****Output
Ms Dhoni 43 Batsman India
Virat Kohli 36 Batsman India
Top comments (0)