Home Java javaTutorial Scenario-,3

Scenario-,3

Jan 27, 2025 pm 10:05 PM

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles