JAVA ASSIGNMENT
1. METHOD OVERLOADING IN CALCULATOR
public class polymorphism {
//add
public int add(int a,int b){
return a+b;
}
public int add(int a,int b,int c){
return a+b+c;
}
public int sub(int a,int b){
return a-b;
}
public int sub(int a,int b,int c){
return a-b-c;
}
public int mul(int a,int b){
return a*b;
}
public int mul(int a,int b,int c){
return a*b;
}
public int div(int a,int b){
return a/b;
}
public double div(double a,double b){
return a/b;
}
public static void main(String[] args){
polymorphism p=new polymorphism();
System.out.println(p.add(2,4));
System.out.println(p.add(2,4,5));
System.out.println(p.sub(4,5));
System.out.println( p.sub(10,5,6));
System.out.println( p.mul(4,5));
System.out.println(p.mul(4,5,6));
System.out.println(p.div(10,2));
System.out.println( p.div(5.6,3.2));
}
}
OUTPUT:
6
11
-1
-1
20
20
1.7499999999999998
2.METHOD OVERRIDING IN ANIMALS
class Animal1 {
public void sound(){
System.out.println("Animals can create a sound");
}
}
class Dog extends Animal1{
@Override
public void sound(){
System.out.println("Dog is Barking");
}
}
class Cat extends Animal1{
@Override
public void sound(){
System.out.println("Cat is Meowing");
}
}
class Cow extends Animal1{
@Override
public void sound(){
System.out.println("Cow is Bowing");
}
}
public class Animal{
public static void main(String[] args){
Animal1 ob=new Animal1();
ob.sound();
Animal1 obj1=new Dog();
obj1.sound();
Animal1 obj2=new Cat();
obj2.sound();
Animal1 obj3=new Cow();
obj3.sound();
}
}
OUTPUT:
Animals can create a sound
Dog is Barking
Cat is Meowing
Cow is Bowing
3. USE SUPER FOR PARENT CLASS METHOD ACCESS
class Vehicle{
public void getspeed() {
System.out.println("");
}
}
class carr extends Vehicle{
public void getspeed() {
System.out.println("Car: 100km speed");
}
}
class bikee extends Vehicle{
public void getspeed() {
System.out.println("Bike: 120km speed");
}
}
class truck extends Vehicle{
public void getspeed() {
System.out.println("Truck: 80km speed");
}
}
public class happy {
public static void main(String[] args) {
Vehicle o;
o=new Vehicle();
o.getspeed();
o=new carr();
o.getspeed();
o=new bikee();
o.getspeed();
o=new truck();
o.getspeed();
}
}
OUTPUT
Car: 100km speed
Bike: 120km speed
Truck: 80km speed
4. DYNAMIC METHOD DISPATCHING WITH SHAPE
package JavaTraining2024;
class shape{
double r=2;
double pi=22/7;
double l=3,w=4,b=5,h=6;
public void area(){
System.out.println("area");
class circle extends shape{
@Override
public void area() {
double c=Math.pow(pi, r);
System.out.println("Area of Circle: "+c);
class rectangle extends shape{
@Override
public void area() {
System.out.println("Area of Rectangle: "+l*w);
class Triangle extends shape{
@Override
public void area() {
System.out.println("Area of Triangle: "+(0.5)*(b*h));
public class shapesPolymorphismOverRiding {
public static void main(String[] args) {
shape s;
s=new circle();
s.area();
s=new rectangle();
s.area();
s=new Triangle();
s.area();
OUTPUT
Area of Circle: 9.0
Area of Rectangle: 12.0
Area of Triangle: 15.0
5. BANK INTEREST CALCULATION
class Bank1{
public void getInterestRate(){
System.out.println("Interest Rate");
}
}
class SBI extends Bank1{
public void getInterestRate(){
int p=1000;
int n=4;
int r=3;
int num=p*n*r;
System.out.println("Interest Rate SBI: "+num);
}
}
class HDFC extends Bank1{
public void getInterestRate(){
int p=1000;
int n=4;
int r=6;
int num=p*n*r;
System.out.println("Interest Rate HDFC: "+num);
}
}
class ICICI extends Bank1{
public void getInterestRate(){
int p=1000;
int n=4;
int r=8;
int num=p*n*r;
System.out.println("Interest Rate of ICICI: "+num);
}
}
public class Bank {
public static void main(String[] arg){
Bank1 b;
b=new Bank1();
b.getInterestRate();
b=new HDFC();
b.getInterestRate();
b=new SBI();
b.getInterestRate();
b=new ICICI();
b.getInterestRate();
}
}
OUTPUT:
Interest Rate
Interest Rate HDFC: 24000
Interest Rate SBI: 12000
Interest Rate of ICICI: 32000
6. USE SUPER VARIABLE ACCESS
package JavaTraining2024;
class employee{
int basesalary;
class Manager extends employee{
void totals(int bonus,int basesalary) {
System.out.print("Total Salary: "+(basesalary+bonus));}
public class empolyeePolymorphism {
public static void main(String[] args) {
Manager m1=new Manager();
m1.totals(1000,5000);
OUTPUT:
Total Salary: 6000
7. RUNTIME POLYMORPHISM FOR E-COMMERCE ORDER
package JavaTraining2024;
class order{
public void processOrder() {
System.out.println("Order Processing");
class onlineorder extends order{
public void processOrder() {
System.out.println("Order Placed through online");
class storepickuporder extends order{
public void processOrder() {
System.out.println("Order Processed pickup the order");
public class ecommercePolymorphism {
public static void main(String[] args) {
order o;
o=new order();
o.processOrder();
o=new onlineorder();
o.processOrder();
o=new storepickuporder();
o.processOrder();
OUTPUT
Order Processing
Order Placed through online
Order Processed pickup the order
8. EMPLOYEE HIERARCHY
package JavaTraining2024;
class empl{
public void calculatesalary() {
System.out.print("");
class fulltime extends empl{
public void calculatesalary() {
int bonus=10000;
int ctc=800000;
System.out.println("FullTimeEmployee");
System.out.println("Bonus: "+bonus);
System.out.println("ctc: "+ctc);
class partime extends empl{
public void calculatesalary() {
int bonus=10000;
int ctc=600000;
System.out.println("PartTimeEmployee");
System.out.println("Bonus: "+bonus);
System.out.println("ctc: "+ctc);
class intern extends empl{
public void calculatesalary() {
int bonus=10000;
int ctc=200000;
System.out.println("Intern");
System.out.println("Bonus: "+bonus);
System.out.println("ctc: "+ctc);
class empolyeeHierarchy {
public static void main(String[] args) {
empl e1=new empl();
e1.calculatesalary();
fulltime f1=new fulltime();
f1.calculatesalary();
System.out.println("");
partime p1=new partime();
p1.calculatesalary();
System.out.println("");
intern i1=new intern();
i1.calculatesalary();
OUTPUT
FullTimeEmployee
Bonus: 10000
ctc: 800000
PartTimeEmployee
Bonus: 10000
ctc: 600000
Intern
Bonus: 10000
ctc: 200000
9. VEHICLE SPEED ANALYSIS
class Vehicle{
public void getspeed() {
System.out.println("");
}
}
class carr extends Vehicle{
public void getspeed() {
System.out.println("Car: 100km speed");
}
}
class bikee extends Vehicle{
public void getspeed() {
System.out.println("Bike: 120km speed");
}
}
class truck extends Vehicle{
public void getspeed() {
System.out.println("Truck: 80km speed");
}
}
public class happy {
public static void main(String[] args) {
Vehicle o;
o=new Vehicle();
o.getspeed();
o=new carr();
o.getspeed();
o=new bikee();
o.getspeed();
o=new truck();
o.getspeed();
}
}
OUTPUT:
Car: 100km speed
Bike: 120km speed
Truck: 80km speed
10. POLYMORPHISM WITH ABSTRACT CLASS
abstract class Application1{
public abstract void powerConsumption();
}
class WashingMachine extends Application1{
int unit=50;
public void powerConsumption(){
System.out.println("Washing Machine consumes about "+unit+"units of current");
}
}
class Refrigerator extends Application1{
int unit=100;
public void powerConsumption(){
System.out.println("Refrigerator consumes about "+unit+"units of current");
}
}
class AirConditioner extends Application1{
int unit=150;
public void powerConsumption(){
System.out.println("AirConditioner consumes about "+unit+"units of current");
}
}
public class Application {
public static void main(String[] args){
Application1 a=new WashingMachine();
a.powerConsumption();
Application1 b=new Refrigerator();
b.powerConsumption();
Application1 c=new AirConditioner();
c.powerConsumption();
}
}
OUTPUT:
Washing Machine consumes about 50units of current
Refrigerator consumes about 100units of current
AirConditioner consumes about 150units of current
11. POLYMORPHISM IN THE PAYMENT SYSTEM
class Payment1{
public void pay(){
System.out.println("Payment process based on different type of Payment");
}
}
class CreditCardPayment extends Payment1{
public void pay(){
System.out.println("Credit Card Payment process consist of details like Credit Card number");
}
}
class UPIPayment extends Payment1{
public void pay(){
System.out.println("UPI Payment process consist of details like UPI address ");
}
}
class NetBankingPayment extends Payment1{
public void pay(){
System.out.println("NetBanking Payment process consist of details like loginID and Password for
net banking account");
}
}
public class Payment {
public static void main(String[] args){
Payment1 p;
p=new Payment1();
p.pay();
p=new CreditCardPayment();
p.pay();
p=new UPIPayment();
p.pay();
p=new NetBankingPayment();
p.pay();
}
}
OUTPUT:
Payment process based on different type of Payment
Credit Card Payment process consist of details like Credit Card number
UPI Payment process consist of details like UPI address
NetBanking Payment process consist of details like loginID and Password for net banking account