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

Write A Program in Java To Create A Player Class

The document contains Java code that defines a base class 'Player' and derived classes for different sports players (Cricket, Football, Hockey) with methods to display player information. It also includes a 'Worker' class with derived classes for daily and salaried workers, demonstrating polymorphism through a method to compute weekly pay. Additionally, it features a 'TrunkCall' class with derived classes for different types of calls, calculating charges based on duration and call type, again showcasing polymorphism.
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)
28 views8 pages

Write A Program in Java To Create A Player Class

The document contains Java code that defines a base class 'Player' and derived classes for different sports players (Cricket, Football, Hockey) with methods to display player information. It also includes a 'Worker' class with derived classes for daily and salaried workers, demonstrating polymorphism through a method to compute weekly pay. Additionally, it features a 'TrunkCall' class with derived classes for different types of calls, calculating charges based on duration and call type, again showcasing polymorphism.
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/ 8

Write a program in Java to create a Player class.

Intermit classes
Cricket _Player, Football _Player and Hockey_ Player from Player
class.

// Base class

class Player {

String name;

int age;

// Constructor for Player

Player(String name, int age) {


this.name = name;

this.age = age;

// Method to display player details

void displayInfo() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Derived class: Cricket_Player

class Cricket_Player extends Player {

String role; // e.g., batsman, bowler, all-rounder

Cricket_Player(String name, int age, String role) {

super(name, age); // calling Player constructor

this.role = role;

}
void displayInfo() {

super.displayInfo();

System.out.println("Game: Cricket");

System.out.println("Role: " + role);

System.out.println();

// Derived class: Football_Player

class Football_Player extends Player {

String position; // e.g., forward, goalkeeper

Football_Player(String name, int age, String position) {

super(name, age);

this.position = position;

void displayInfo() {

super.displayInfo();

System.out.println("Game: Football");

System.out.println("Position: " + position);

System.out.println();

// Derived class: Hockey_Player

class Hockey_Player extends Player {

String teamName;

Hockey_Player(String name, int age, String teamName) {


super(name, age);

this.teamName = teamName;

void displayInfo() {

super.displayInfo();

System.out.println("Game: Hockey");

System.out.println("Team: " + teamName);

System.out.println();

// Main class to test

public class Main {

public static void main(String[] args) {

Cricket_Player c1 = new Cricket_Player("Virat Kohli", 35, "Batsman");

Football_Player f1 = new Football_Player("Sunil Chhetri", 40, "Forward");

Hockey_Player h1 = new Hockey_Player("Manpreet Singh", 33, "India");

// Display details

c1.displayInfo();

f1.displayInfo();

h1.displayInfo();

}
Write a class Worker and derive classes DailyWorker and
SalariedWorker from it. Every worker is has a name and a salary
rate. Write method ComPay( int hours) to computethe week pay of
every worker.A DailyWorker is paid on the basis of number of days
s/he work.The SalariedWorker g ets paid the wage for 40 hours a
week no matter what actual hours is. Test this program to calculate
the pay of workers. You are expected to use concept of
polymorphism to write this program.

// Base class Worker

class Worker {

String name;

double salaryRate;

// Constructor

Worker(String name, double salaryRate) {

this.name = name;

this.salaryRate = salaryRate;

// Method to compute pay — will be overridden

double computePay(int hours) {

return 0.0; // default (abstract behavior)

// Method to display details

void displayPay(int hours) {

System.out.println("Worker Name: " + name);

System.out.println("Weekly Pay: " + computePay(hours));

System.out.println("-----------------------------");

}
}

// Derived class DailyWorker

class DailyWorker extends Worker {

DailyWorker(String name, double salaryRate) {

super(name, salaryRate);

// Overridden method — pay based on number of days worked (assuming 8 hours/day)

double computePay(int hours) {

int days = hours / 8; // assuming 8 working hours per day

return days * salaryRate;

// Derived class SalariedWorker

class SalariedWorker extends Worker {

SalariedWorker(String name, double salaryRate) {

super(name, salaryRate);

// Overridden method — fixed pay for 40 hours/week

double computePay(int hours) {

return 40 * salaryRate;

// Test class

public class WorkerTest {


public static void main(String[] args) {

// Create objects using base class reference (polymorphism)

Worker w1 = new DailyWorker("Ravi", 1500); // ₹1500 per day

Worker w2 = new SalariedWorker("Anita", 200); // ₹200 per hour

System.out.println("----- Weekly Payment Report -----");

w1.displayPay(48); // 48 hours worked → 6 days

w2.displayPay(30); // only 30 hours, but gets paid for 40 hours

Consider trunk calls of a telephone exchange. A trunk call can be


ordinary, urgent or lightning call. The charges depend on the
duration and the type of the call. Writ a program-using concept of
polymorphism in Java to calculate the chases.

// Base class

class TrunkCall {

protected double duration; // in minutes

TrunkCall(double duration) {

this.duration = duration;

// Method to compute charge — overridden in subclasses

double computeCharge() {

return 0.0;

// Display call details


void displayCharge() {

System.out.println("Call Duration: " + duration + " minutes");

System.out.println("Total Charge: ₹" + computeCharge());

System.out.println("-----------------------------");

// Derived class for Ordinary Call

class OrdinaryCall extends TrunkCall {

OrdinaryCall(double duration) {

super(duration);

// Ordinary call rate = ₹2 per minute

double computeCharge() {

return duration * 2;

// Derived class for Urgent Call

class UrgentCall extends TrunkCall {

UrgentCall(double duration) {

super(duration);

// Urgent call rate = ₹3.50 per minute

double computeCharge() {

return duration * 3.5;

}
// Derived class for Lightning Call

class LightningCall extends TrunkCall {

LightningCall(double duration) {

super(duration);

// Lightning call rate = ₹5 per minute

double computeCharge() {

return duration * 5;

// Main class to test

public class TrunkCallTest {

public static void main(String[] args) {

// Using polymorphism

TrunkCall call1 = new OrdinaryCall(10); // 10 minutes

TrunkCall call2 = new UrgentCall(5); // 5 minutes

TrunkCall call3 = new LightningCall(3); // 3 minutes

System.out.println("----- Trunk Call Charges -----");

call1.displayCharge();

call2.displayCharge();

call3.displayCharge();

You might also like