You are on page 1of 6

Experiment-5

Class OOPs

Type Lab

Name: Muditt Sharmma


Roll No.: R2142210506
SAP ID: 500091725
Branch: Btech. CSE spl (CSF)

AIM:
1. Write a Java program to show that private member of a super class cannot be accessed from derived classes.

2. Write a program in Java to create a Player class. Inherit the classes Cricket _Player, Football _Player and Hockey_
Player from Player class.

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

4. Consider the trunk calls of a telephone exchange. A trunk call can be ordinary, urgent or lightning. The charges
depend on the duration and the type of the call. Write a program using the concept of polymorphism in Java to
calculate the charges.

1)Write a Java program to show that private member of a super class cannot
be accessed from derived classes.

class Student1{
String name;
private int age,rollno;
void setage(int age) {
this.age=age;
}
int getage() {
return age;
}
void setroll(int rollno) {
this.rollno=rollno;
}
int getroll() {
return rollno;
}
void print() {
System.out.println(name+" "+age+" "+rollno);
}
}
class derived extends Student1 {
public static void main(String[] args) {
Student1 obj= new Student1();
obj.name="Muditt";
obj.setage(17);
obj.setroll(9);
System.out.println(obj.name+" "+obj.getage()+" "+obj.getroll());
obj.print();
}
}

Algorithm:
1)Create a class Student1

Experiment-5 1
2)Make a String variable name

3) Make private int variables age and rollno


4) Create a setage() function to set the age

5) Create a getage() function to get the age


6) Similarly create functions for rollno

7) Create a function to display


8) Create a class derived and inherit the properties of Student1 in it

9) Create the main method


10) Create a new object obj from Student1

11) Set the values of age and rollno


12) Set the name to Akshat

13) Use the print function to display

2) Write a program in Java to create a Player class. Inherit the classes Cricket
_Player, Football _Player and Hockey_ Player from Player class.

class Player {
String name;
int age;
Player(String n,int a) {
name=n; age=a;
}
void show() {
System.out.println("");
System.out.println("Player name : "+name);
System.out.println("Age : "+age);
}
}
class criket_player extends Player {
String type;
criket_player(String n,String t,int a) {
super(n,a);
type=t;
}
public void show() {
super.show();
System.out.println("Player type : "+type);
}
}
class football_player extends Player {

Experiment-5 2
String type;
football_player(String n,String t,int a) {
super(n,a);
type=t;
}
public void show() {
super.show();
System.out.println("Player type : "+type);
}
}
class hockey_player extends Player {
String type;
hockey_player(String n,String t,int a) {
super(n,a);
type=t;
}
public void show() {
super.show();
System.out.println("Player type : "+type);
}
}
class name {
public static void main(String args[]) {
criket_player c=new criket_player("Virat","Criket",36);
football_player f=new football_player("Ronaldo","Football",40);
hockey_player h=new hockey_player("Mandeep","Hockey",43);
c.show();
f.show();
h.show();
}
}

Algorithm:
1. Create a class Player

2. Create a parametrized constructor Player with parameters String n and int a

3. Create a function to display name and age of the player

4. Create classes for cricket_player, football_player and hockey player and within them create functions that will show
the player type stored in the String variable type.

5. Create a class name

6. Create the main method

7. Create objects for cricket_player, football_player and hockey player classes and enter the values that will be displayed

8. Display the output

Experiment-5 3
3) Write a class Worker and derive classes DailyWorker and SalariedWorker
from it. Every worker has a name and a salary rate. Write method ComPay
(int hours) to compute the week pay of every worker. A Daily Worker is paid
on the basis of the number of days he/she works. The Salaried Worker gets
paid the wage for 40 hours a week no matter what the actual hours are. Test
this program to calculate the pay of workers. You are expected to use the
concept of polymorphism to write this program.

class worker{
String empname;
worker (String s) {
empname=s;
}
void display() {
System.out.println("Employee name is:"+empname);
}
}
class dailyworker extends worker{
int salary_rate;
dailyworker(String name,int rate){
super(name);
salary_rate=rate;
}
void compay(int pay) {
super.display();
System.out.println( "Salary is:"+pay*salary_rate);
}

}
class salariedworker extends worker{
int salary_rate;
int pay=40;
salariedworker(String name,int rate){
super(name);
salary_rate=rate;
}
void compay() {
super.display();
System.out.println( "Salary is:"+pay*salary_rate);
}
}

class WorkerPay {
public static void main(String[] args) {
dailyworker obj1 = new dailyworker("amit",250);
salariedworker obj2 = new salariedworker("hari",300);
obj1.compay(60);
obj2.compay();

Algorithm:
1. Create a class worker

2. Create a String variable empname which will store Employee Name

3. Create a void function to display the employee name

4. Create a class dailyworker and salariedworker and inherit the worker class

5. Create a parametrized constructor within those classes with parameters String name and int pay

6. Create a function compay with the first compay having parameter intpay, to calculate and display the salary

7. Create a WorkerPay class

8. Create the main method

9. Create objects for dailyworker and salariedworker and input the values for their name and salary pay

10. Display using compay

Experiment-5 4
4) Consider the trunk calls of a telephone exchange. A trunk call can be
ordinary, urgent or lightning. The charges depend on the duration and the
type of the call. Write a program using the concept of polymorphism in Java
to calculate the charges.

class TrunkCall{
String type;
float duration;
TrunkCall( String type,float duration){
this.type=type;
this.duration=duration;
}
void display() {
System.out.println("Call type is "+type);
System.out.println("Call duration is "+duration);
}
}
class Normal extends TrunkCall{
float rate=2.0f;
Normal(String type,float duration){
super(type,duration);
}
float charges(){
return duration*rate;
}
void display() {
super.display();
System.out.println("total cost is:"+charges());
}
}
class Urgent extends TrunkCall{
float rate=3.5f;
Urgent(String type,float duration){
super(type,duration);
}
float charges(){
return duration*rate;
}
void display() {
super.display();
System.out.println("total cost is:"+charges());
} }
class Lightning extends TrunkCall{
float rate=4.5f;
Lightning(String type,float duration){
super(type,duration);
}
float charges(){
return duration*rate;
}
void display() {
super.display();
System.out.println("total cost is:"+charges());
}}

Experiment-5 5
class Call {
public static void main(String[] args) {
Normal obj1 =new Normal("normal",10.5f);
obj1.display();
Urgent obj2 =new Urgent("urgent",11.2f);
obj2.display();
Lightning obj3 =new Lightning("Lightning",8.4f);
obj3.display();
}

Algorithm:
1. Create a class TrunkCall

2. Create a String variable type and float variable duration

3. Create a function inside the variable to display the type and duration

4. Create a class Normal, Urgent and Lightning and inherit the properties of TrunkCall, set the value of the float variable
rate, create a float function chargest that returns duration * rate, create a function to display.

5. Create a class Call

6. Create a main method and create objects for every class and display.

Experiment-5 6

You might also like