DEV Community

Neelakandan R
Neelakandan R

Posted on

Scenario-1,2,3

Note: Add main method wherever necessary.

Each and every scenario presented here are for getting good understanding on OOP(Object Oriented Programming) through Java.

Scenario #1:

Expected Understanding: Access Modifiers, Single Inheritance, getter methods, Constructor Overloading
1) Create a Class named “Trainer”.
– Have default instance variables String dept, institute
– Assign values – “Java”, “Payilagam” to them
– Have private instance variable int salary
– Assign 10000 as value for salary.
– Create getter method for salary.
– Have instance method training() with void as return data type
– Add a print statement inside training() method

  • Add main method [public static void main(String[] args)] – Have instance named as ‘trainerKumar’ and pass “CSE”, “payilagam” as arguments to it. – Handle above line with matching Constructor.

2) Create a sub class “SQLTrainer” under “Trainer”.
– Have main method in it.
– Create instance ram for this class
– Handle with proper super class constructor
– Access parent class instance variables
– Call parent class instance method training()
– Access salary using getter method in parent class

package B15;

public class Trainer {
    String dept = "java";
    String institute = "payilagam";
    private int salary = 10000;

    Trainer(String dept, String intitute) {
        this.dept = dept;
        this.institute = institute;
    }

    public static void main(String[] args) {
        Trainer trainerkumar = new Trainer("cse", "payilagam");
        String a = trainerkumar.traing();
        trainerkumar.salary();
        System.out.println(a);
    }

    public void salary() {

        System.out.println("salary = " + salary);

    }

    public String traing() {
        return dept + " " + institute;
    }
}

Enter fullscreen mode Exit fullscreen mode

output:
salary = 10000
cse payilagam

package B15;

public class SQLtrainer extends Trainer {
    SQLtrainer(String dept, String intitute) {
        super(dept, intitute);

    }

    public static void main(String[] args) {
        SQLtrainer ram = new SQLtrainer("cse111", "srit");
        String a = ram.traing();
        System.out.println(a);
        ram.salary();

        System.out.println(ram.dept);
        System.out.println(ram.institute);
    }

}

Enter fullscreen mode Exit fullscreen mode

output

cse111 srit
salary = 10000
cse111
payilagam

Scenario #2:

Expected Understanding: Interface, Class, static variables, dynamic binding
1) Create an interface called ‘Actor’
– Have variables boolean makeUpRequired
– Assign ‘true’ value for ‘makeUpRequired’
– Have variable String address.
– Assign value as “Chennai” to address.
– Have void methods act(), dance(), sing()

2) Create class named as ActorSivakumar with main method
– implement interface ‘Actor’ to this class.
– Give your own definitions for methods from interface
– Have static variable String address.
– Assign value to address as “Coimbatore”.
– Have instance method ‘speaking()’ with void return data type.
– Create instance for ActorSivakumar as below
ActorSivakumar as = new ActorSivakumar(65, “Audi Car”)
– Handle with proper Constructor
– Access all the methods from ActorSivakumar class
– Access variable address and print the value
– Create another instance of interface ‘Actor’ using dynamic binding approach
Actor ac = new Sivakumar();
– Handle with proper Constructor
– Access methods in ActorSivakumar class.
– Access variable address using ‘ac’ intance and print the value
– Observe and note down the difference between two instances

package B15;

public interface  Actor {
    boolean makeupRequired = true;
    String address = "chennai";


    void act();

    void dance();

    void sing();

}
Enter fullscreen mode Exit fullscreen mode
package B15;

public class ActorSivakumar implements Actor {
    static String address = "coimbatore";
    int num;
    String car;

    public ActorSivakumar(int num, String car) {
        this.num = num;
        this.car = car;

    }

    public static void main(String[] args) {
        ActorSivakumar as = new ActorSivakumar(65, "Audi car");
        Actor ac = new ActorSivakumar(55, "benz car");// dynamic binding
        as.act();
        as.speaking();
        as.dance();
        as.sing();
        as.sell();
        // ac.speaking();//dynamic binding
        ac.act();
        ac.dance();
        ac.sing();
        // ac.sell();//dynamic binding
        System.out.println(ActorSivakumar.address);
        System.out.println(Actor.address);
        System.out.println(as.makeupRequired);
    }

    public void sell() {

        System.out.println(num + "\n" + car);
    }

    public void speaking() {
        System.out.println("sivakumar is speaking");

    }

    public void act() {
        System.out.println("sivakumar is acting");

    }

    public void dance() {
        System.out.println("sivakumar is dancing");

    }

    public void sing() {
        System.out.println("sivakumar is singing");

    }

}

Enter fullscreen mode Exit fullscreen mode

Output:
sivakumar is acting
sivakumar is speaking
sivakumar is dancing
sivakumar is singing
65
Audi car
sivakumar is acting
sivakumar is dancing
sivakumar is singing
coimbatore
chennai
true

Scenario #3:

Expected Understanding: Abstraction, Inheritance, return keyword, Method Arguments, Constructor
1) Create an abstract class named ‘SmartPhone’
– Add the below abstract methods
– int call(int seconds)
– void sendMessage()
– void receiveCall()
– Add non abstract method void browse()
– print ‘SmartPhone browsing’ inside browse() method definition
– Have constructor as below.
public SmartPhone()
{
System.out.println(“Smartphone under development”);
}
2) Create class called ‘FactoryDemo’ as abstract subclass of SmartPhone
– Add the below abstract methods
– void verifyFingerPrint()
– void providePattern()
– Add non abstract method void browse()
– print ‘Factory Demo browsing’ inside browse() method definition
– Add variable boolean isOriginalPiece and assign ‘false’ as value.
– Add static variable int price and set value as 0.
3) Create class called ‘Samsung’ with main method as sub class of FactoryDemo.
– Add unimplemented methods
– Add static variable int price and set value as 5000.
– Create instance for Samsung class called sam
– Access browse() method using sam instance.
– Access price variable using sam instance.
– Observe which method is called and note down.

package B15;

public abstract class Smartphone {

    public Smartphone()// constructor
    {
        System.out.println("Smartphone under development");
    }

    public abstract int call(int second);

    public abstract void sendMessage();

    public abstract void receivecall();

    public void browse()

    {
        System.out.println("smartphone browsing");
    }
}

Enter fullscreen mode Exit fullscreen mode
package B15;

public abstract class FactoryDemo extends Smartphone {

    public abstract void fingerprint();

    public abstract void providepattern();

    public void browse()//over ridding

    {
        boolean originalpiece = false;// local variable//access within method only
        int price = 0;// local variable//access within method only
        System.out.println("FactoryDemo browsing");

    }
}
Enter fullscreen mode Exit fullscreen mode
package B15;

public class Samsung extends FactoryDemo {
    static int price = 5000;

    public static void main(String[] args) {
        Samsung sam = new Samsung();
        sam.browse();// over ridded method only taken
        sam.fingerprint();
        sam.providepattern();
        int b = sam.call(100);//return
        sam.receivecall();
        System.out.println("b = " + b);
        System.out.println("pric = "+sam.price);

    }

    public void fingerprint() {
        System.out.println("fingerprint");

    }

    public void providepattern() {
        System.out.println("providepattern");

    }

    public int call(int second) {

        return second;//understanding return keyword
    }

    public void sendMessage()//understanding abstraction
    {

    }

    public void receivecall() {
        System.out.println("receivecall");

    }

}

Enter fullscreen mode Exit fullscreen mode

Output:

Smartphone under development
FactoryDemo browsing
fingerprint
providepattern
receivecall
b = 100
pric = 5000

Top comments (0)