0% found this document useful (0 votes)
70 views

JAVA OOP Questions (Getter Setter Functions)

The document describes two tasks for an Object Oriented Programming lab. Task 1 involves creating a Person class with attributes for name, birthYear, and deathYear. It should include parameterized and copy constructors, getter methods, and a print method. Task 2 involves creating a CricketPlayer class with attributes for player details and stats. It should include a parameterized constructor, getter methods, and methods to calculate strike rate and boundary percentage, with a print method to output player information. Sample test code and output are provided.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

JAVA OOP Questions (Getter Setter Functions)

The document describes two tasks for an Object Oriented Programming lab. Task 1 involves creating a Person class with attributes for name, birthYear, and deathYear. It should include parameterized and copy constructors, getter methods, and a print method. Task 2 involves creating a CricketPlayer class with attributes for player details and stats. It should include a parameterized constructor, getter methods, and methods to calculate strike rate and boundary percentage, with a print method to output player information. Sample test code and output are provided.

Uploaded by

Muhammad Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Object Oriented Programming (OOP) Lab Tasks 01 E1

Task 1:
You are required to implement a `Person` class in Java, which represents a human being. The class should
have the following attributes:
- `name` (String): Represents the name of the person.
- `birthYear` (int): Represents the year of birth of the person.
- `deathYear` (int): Represents the year of death of the person. If the person is still alive, the value should
be set to -1.
The Person class should provide the following functionalities:
1. Parameterized Constructor: A constructor that initializes the `name`, `birthYear`, and
`deathYear` attributes of the person.
2. Copy Constructor: A constructor that creates a copy of an existing `Person` object.
3. Accessor Methods: Getter methods to access the `name`, `birthYear`, and `deathYear` attributes of
the person.
4. Print Method: A method that displays the information of the person, including their name, birth
year, and death year.

In the `main` function, you need to test the functionality of the `Person` class by performing the following
actions:
1. Create a `Person` object `p1` with the following details: name - " Abdul Sattar Edhi", birthYear -
1928, deathYear - 2016.
2. Create a `Person` object `p2` using the copy constructor, with `p1` as the source object.
3. Create a `Person` object `p3` using the parametrized constuctor
4. Call the `print` method on `p1`, `p2`, and `p3` to display their respective information.
5. Create a `Person` object `p4` with no death information and print it.
Sample Output:
Name: Abdul Sattar Edhi Name: Abdul Sattar Edhi
Birth Year: 1928 Birth Year: 1928
Death Year: 2016 Death Year: 2016

Name: Abdul Sattar Edhi Name: Babar Azam


Birth Year: 1928 Birth Year: 1994
Death Year: 2016 Death Year: -
Task 2
You are required to implement a `CricketPlayer` class in Java that represents a cricket player. The class
should have the following attributes:

- `playerName` (String): Represents the name of the player.


- `score` (int): Represents the total score of the player.
- `ballsPlayed` (int): Represents the total number of balls played by the player.
- `numFours` (int): Represents the number of fours hit by the player.
- `numSixes` (int): Represents the number of sixes hit by the player.

The `CricketPlayer` class should provide the following functionalities:


1. Parameterized Constructor: A constructor that initializes the `playerName`, `score`, `ballsPlayed`,
`numFours`, and `numSixes` attributes of the player.
2. Accessor Methods: Getter methods to access the `playerName`, `score`, `ballsPlayed`, `numFours`,
and `numSixes` attributes of the player.
3. Get Strike Rate: A private method that calculates and returns the strike rate of the player. The strike
rate is calculated as (score / ballsPlayed) * 100.
4. Get Boundary Percentage: A private method that calculates and returns the percentage of
boundaries (fours and sixes) scored by the player. The boundary percentage is calculated as
((numFours + numSixes) / ballsPlayed) * 100.
5. A public print method that displays the information of Player

Sample Output:
Player Name: Babar Azam
Total Score: 78
Balls Played: 54
Number of Fours: 8
Number of Sixes: 3
Strike Rate: 144.44
Boundary Percentage: 25.92

You might also like