Oop Lab 4
Oop Lab 4
Oop Lab 4
BATCH: 4
SAP ID: 500083747
CODE:
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="Akshat";
obj.setage(17);
obj.setroll(9);
System.out.println(obj.name+" "+obj.getage()+" "+obj.getroll());
obj.print();
}
OUTPUT:
2. Write a program in Java to create a Player class. Inherit the
classes Cricket _Player, Football _Player and Hockey_ Player from
Player class.
CODE:
package com.company;
class Player
{
String name;
int age;
Player(String n,int a)
{ name=n; age=a; }
void show()
{
System.out.println("Player name: "+name);
System.out.println("Age: "+age);
}
}
class cricket_player extends Player
{
String type;
cricket_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
{
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 Demo
{
public static void main(String args[])
{
cricket_player c=new cricket_player("Ankit","Cricket",20);
football_player f=new football_player("Namrata","Football",21);
hockey_player h=new hockey_player("Aditya","Hockey",22);
c.show();
f.show();
h.show();
}
}
OUTPUT
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.
CODE:
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 DemoWorker {
public static void main(String[] args) {
dailyworker obj1 = new dailyworker("amit",250);
salariedworker obj2 = new salariedworker("hari",300);
obj1.compay(60);
obj2.compay();
}
OUTPUT:
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.
CODE:
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());
}}
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();
}
}
OUTPUT:
5. Design a class employee of an organization. An employee has a
name, empid, and salary. Write the default constructor, a constructor
with parameters (name, empid, and salary) and methods to return
name and salary. Also write a method increaseSalary that raises the
employee’s salary by a certain user specified percentage. Derive a
subclass Manager from employee. Add an instance variable named
department to the manager class. Supply a test program that uses
theses classes and methods.
CODE:
class Employee{
String name;
int empid;
double salary;
Employee(){
salary=100000;
}
Employee(String name,int empid,double salary) {
this.name=name;
this.empid=empid;
this.salary=salary;
}
String getname() {
return name;}
double getsalary() {
return salary;
}
double IncreaseSalary(int inc) {
salary=salary+0.01*inc*salary;
return salary;
}
}
class Manager extends Employee{
String department;
Manager(String name,int empid,String dept){
this.name=name;
this.empid=empid;
this.department=dept;
}
void PrintDetails() {
System.out.println("\nName: "+super.getname());
System.out.println("Employee ID :"+empid);
System.out.println("Department:"+department);
System.out.println("Salary:"+super.getsalary());
}
}
class EmployeeDemo {
public static void main(String[] args) {
Employee obj1=new Employee("Jadaish",9867,150000.0);
System.out.println("Name:"+obj1.getname()+"\nSalary:"+obj1.getsalary());
System.out.println("Increased salary is:"+obj1.IncreaseSalary(15));
Manager obj2= new Manager("Pushpander",9879,"Electrical");
obj2.PrintDetails();
}
}
OUTPUT: