DEV Community

Saravanan
Saravanan

Posted on

Day-4 Task-2

Task:

  1. Create a class Called PlayGround
  2. Create non-static variables as below. int score, balls, catches; String player_name;
  3. Create main method.
  4. Inside main method, create two instances as below. PlayGround player1 = new PlayGround("dhoni", 100, 3); PlayGround player2 = new PlayGround("jadeja", 56, 2, 30);
  5. Create appropriate constructors for handling above objects.
  6. Using player1 object, call batting() method. Print - score, player_name.
  7. Using player2 object, call allrounder() method. Print - score, player_name, balls, catches
class PlayGround
{
    int score,balls,catches;
    String player_name;

    public PlayGround(String player_name,int score,int catches)
    {
        this.score = score;
        this.player_name = player_name;
        this.catches = catches;
    }
    public PlayGround(String player_name,int score,int catches,int balls)
    {
        this.score = score;
        this.balls = balls;
        this.catches = catches;
        this.player_name = player_name;
    }

    public static void main(String [] args)
    {
        PlayGround player1 = new PlayGround("dhoni",100,3);
        PlayGround player2 = new PlayGround("jadeja",56,2,30);

        player1.batting();
        player2.allrounder();
    }
    public void batting()
    {
    System.out.println(player_name +" "+ score);
    }
    public void allrounder()
    {
    System.out.println(player_name + " " + balls + " " + score + " " + catches);
    }
}


Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)