Home > Java > javaTutorial > Scenario-,3

Scenario-,3

Barbara Streisand
Release: 2025-01-27 22:05:11
Original
535 people have browsed it

Scenario-,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;
    }
}

Copy after login
Copy after login

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);
    }

}

Copy after login
Copy after login

output

cse111 payilagam
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 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;
    }
}

Copy after login
Copy after login
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);
    }

}

Copy after login
Copy after login

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 interface  Actor {
    boolean makeupRequired = true;
    String address = "chennai";


    void act();

    void dance();

    void sing();

}
Copy after login
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");

    }

}

Copy after login
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");
    }
}

Copy after login

Output:

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

The above is the detailed content of Scenario-,3. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template