[Go to site: main page, start]

0% found this document useful (0 votes)
5 views8 pages

Java Lab Manual

The document is a lab manual for the Java Programming Skills Lab at G H Raisoni International Skill Tech University, detailing the course structure, objectives, and a specific experiment on inheritance in Java. It includes the creation of a base class 'Player' and derived classes 'CricketPlayer', 'FootballPlayer', and 'HockeyPlayer' to demonstrate object-oriented programming concepts. The manual also outlines the program's outcomes, applications of inheritance, and includes viva questions for assessment.

Uploaded by

kompalwarashish9
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)
5 views8 pages

Java Lab Manual

The document is a lab manual for the Java Programming Skills Lab at G H Raisoni International Skill Tech University, detailing the course structure, objectives, and a specific experiment on inheritance in Java. It includes the creation of a base class 'Player' and derived classes 'CricketPlayer', 'FootballPlayer', and 'HockeyPlayer' to demonstrate object-oriented programming concepts. The manual also outlines the program's outcomes, applications of inheritance, and includes viva questions for assessment.

Uploaded by

kompalwarashish9
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

G H RAISONI INTERNATIONAL SKILL TECH

UNIVERSITY, PUNE

SCHOOL OF ENGINEERING AND TECHNOLOGY

Lab Manual (2025-2026)

Class: SY B. Tech. SA1 & DW1


Term: IV
Java Programming Skills Lab
(25BTPCP405)

BY:
Prof. DASHARATH WAGHAMARE
ASSISTANT PROFESSOR
SOET
GHRISTU, PUNE
MOB. 845 9009 185
School: SOET
Course Details
Course: Java Programming Skills Lab (25BTPCP405)
Class: S Y B. Tech Division: SA1 & DW1
Internal Marks: 25 External marks: 25
Credits: 2 Pattern: 2025

COURSE OUTCOME
CO1: Understand and use an integrated development environment to execute Java
programs on basic concepts of object-oriented programming.
CO2: Design and build programs using the concepts of Inheritance, Polymorphism,
interfaces and packages.
CO3: Apply the concepts of Multithreading and Exception handling to develop efficient
and error free codes.
CO4: Implement the java program using networking concepts, AWT and SWING.
EXPERIMENT NO. 5

AIM:

To write a Java program to create a base class Player and derive subclasses CricketPlayer,
FootballPlayer, and HockeyPlayer using inheritance.

OBJECTIVE:
To demonstrate the concept of inheritance by creating a base class Player and deriving multiple
subclasses — CricketPlayer, FootballPlayer, and HockeyPlayer — to model different types of
players in sports.

THEORY:

In object-oriented programming, inheritance allows one class (subclass) to inherit the properties
and methods of another class (superclass). The superclass represents the general concept, and
the subclass represents specialized versions of the general concept. In this assignment, the base
class Player contains common attributes for all types of players, while each sport-specific class
(CricketPlayer, FootballPlayer, and HockeyPlayer) inherits these common attributes and adds
their own specific properties.

JAVA CONCEPTS USED:

• Inheritance: This allows one class to inherit the fields and methods of another class.
• Constructor Overloading: The process of creating multiple constructors with different
parameters.
• Method Overriding: Redefining methods of the superclass in the subclass to implement
sport-specific behavior.

PROGRAM EXPLANATION:

• Player Class: The base class with common properties such as name, age, and
• teamName, along with a method displayPlayerInfo to display player details.
• CricketPlayer Class: Inherits from Player and adds specific methods for cricket, such as
battingAverage.
• FootballPlayer Class: Inherits from Player and adds specific methods for football, such
as goalsScored.
• HockeyPlayer Class: Inherits from Player and adds specific methods for hockey, such as
matchesPlayed.
ALGORITHM:
Step 1: Start the program.
Step 2: Define the Player class with common properties like name, age, and teamName.
Step 3: Define constructors for initializing the properties.
Step 4: Create methods to display player information.
Step 5: Create subclasses CricketPlayer, FootballPlayer, and HockeyPlayer, each extending
Player and adding sport-specific properties.
Step 6: Instantiate objects of each subclass and display their information.
Step 7: End the program.

PSEUDOCODE:

BEGIN
DEFINE Player class
DEFINE attributes: name, age, teamName
DEFINE constructor to initialize attributes
DEFINE method to display player information

DEFINE CricketPlayer class extending Player DEFINE additional attribute: battingAverage


DEFINE constructor to initialize attributes
DEFINE method to display cricket player information

DEFINE FootballPlayer class extending Player DEFINE additional attribute: goalsScored


DEFINE constructor to initialize attributes
DEFINE method to display football player information

DEFINE HockeyPlayer class extending Player DEFINE additional attribute: matchesPlayed


DEFINE constructor to initialize attributes
DEFINE method to display hockey player information

INSTANTIATE CricketPlayer, FootballPlayer, and HockeyPlayer objects CALL display


method for each object
END

FLOWCHART: (IT IS EXPECTED FROM STUDENT TO DRAW.)


Please draw flowchart as per the algorithm given above. Explain all required symbols of
flowcharts and their meaning.
JAVA PROGRAM:
// Base class Player
class Player
{
String Pname;
int Page;
String Pteam;

// Constructor to initialize common properties


public Player (String Pname, int Page, String Pteam)
{
[Link] = Pname;
[Link] = Page;
[Link] = Pteam;
}

// Method to display player information


void displayPlayerInfo()
{
[Link]("Player Name: " + Pname);
[Link]("Player Age: " + Page);
[Link]("Player Team: " + Pteam);
}
}

// Derived class CricketPlayer


class CricketPlayer extends Player
{
double BatAvg;
// Constructor to initialize CricketPlayer-specific properties
CricketPlayer (String Pname, int Page, String Pteam, double BatAvg)
{
// Call Player class constructor
super(Pname, Page, Pteam);
[Link] = BatAvg;
}
// Method to display CricketPlayer information
void displayCricketPlayerInfo()
{
// Call the base class method
displayPlayerInfo();
[Link]("Batting Average: " + BatAvg);
}
}
// Derived class FootballPlayer
class FootballPlayer extends Player
{
int Goals;
// Constructor to initialize FootballPlayer-specific properties
public FootballPlayer (String Pname, int Page, String Pteam, int Goals)
{
// Call Player class constructor
super (Pname, Page, Pteam);
[Link] = Goals;
}
// Method to display FootballPlayer information
void displayFootballPlayerInfo()
{
// Call the base class method
displayPlayerInfo();
[Link]("Goals Scored: " + Goals);
}
}

// Derived class HockeyPlayer


class HockeyPlayer extends Player
{
int matchesPlayed;
// Constructor to initialize HockeyPlayer-specific properties
HockeyPlayer (String Pname, int Page, String Pteam, int matchesPlayed)
{
// Call Player class constructor
super (Pname, Page, Pteam);
[Link] = matchesPlayed;
}
// Method to display HockeyPlayer information
void displayHockeyPlayerInfo()
{
// Call the base class method
displayPlayerInfo();
[Link]("Matches Played: " + matchesPlayed);
}
}
public class DemoPlayer
{
public static void main (String [] args)
{
// Create objects of each subclass and display their information
CricketPlayer CP = new CricketPlayer ("DHONI", 44, "India", 50.57);
FootballPlayer FP = new FootballPlayer ("PELE", 82, "Brazil", 1279);
HockeyPlayer HP = new HockeyPlayer ("DHYAN CHAND", 74, "India", 185);
[Link]("Cricket Player Info:");
[Link]();
[Link]("Football Player Info:");
[Link]();
[Link]("Hockey Player Info:");
[Link]();
}
}

EXPLANATION:
1. Player Class: The Player class contains common attributes like Pname, Page, and
Pteam, and has a method displayPlayerInfo to display these attributes.
2. CricketPlayer Class: This class extends the Player class and adds the attribute BatAvg. It
also has a method displayCricketPlayerInfo to display cricket-specific information.
3. FootballPlayer Class: This class extends Player and adds the attribute Goals. It includes
a method displayFootballPlayerInfo to display football-specific information.
4. HockeyPlayer Class: Inherits from Player and adds matchesPlayed as an attribute. It has
the method displayHockeyPlayerInfo for displaying hockey-specific details.
5. PlayerDemo Class: The main method creates objects of CricketPlayer, FootballPlayer,
and HockeyPlayer, and calls the display methods to print the details of each player.

Output
Cricket Player Info:
Player Name: DHONI
Age: 44
Team: India
Batting Average: 50.57
Football Player Info:
Player Name: PELE
Age: 82
Team: Brazil
Goals Scored: 1279
Hockey Player Info:
Player Name: DHYAN CHAND
Age: 74
Team: India
Played: 185
Applications of Inheritance in Java:

1. Code Reusability: Inheritance allows us to reuse code from the superclass in the
subclass.
2. Hierarchical Classification: It helps in creating a hierarchical classification of classes,
making the code more organized.
3. Polymorphism: Methods can be overridden in subclasses to provide specific behavior,
which is useful for object-specific actions.

PROGRAM OUTCOMES:

1. Understand the concept of inheritance and how it simplifies code.


2. Learn how to use constructors and methods in the context of inheritance.
3. Gain proficiency in creating and using subclasses in Java.

CONCLUSION:

The program demonstrates how inheritance in Java can be used to create specialized player
classes (CricketPlayer, FootballPlayer, HockeyPlayer) from a common base class (Player). This
approach reduces redundancy and promotes code reuse.

VIVA QUESTIONS:

1. What is the purpose of the super () keyword in the constructors of derived classes?
2. How does method overriding work in Java?
3. Explain how inheritance enhances code reusability in Java.
4. What are the advantages of using inheritance in object-oriented programming?
5. What would happen if a class did not inherit from the Player class in this program?

You might also like