Solved programming problem

Basic Inheritance Problem in Java.

Source code

QUESTION:

There is a magical world of Narnia, where time is different from the

time in this world and where animals can speak.

The path to Narnia is through a cupboard. A very special cupboard.

In the magical world of Harry Potter, there is a room called Room of

Requirement. This is like a cupboard too- it has a lot of items stored

 in it. But the door to the Room of Requirement is hidden. To open the

door, a special password should be uttered.

Make a class called Cupboard. It has a variable called

items that tell you the number of items stored in the cupboard.

 Write down the constructors. There are two types of constructor.

If no parameter is passes to the constructor,

it sets the value of items to 0: If a value is passed as parameter,

 that value is set as the value of items. Write another method called addltems.

This method will also have two versions. If no parameter is passed to it,

it increases the value of items by 1. If a value x is passed as parameter,

it increases the value of items by x. There is another method called void open(). This method simply prints a line "Cupboard open".

 Another class CupboardNarnia inherits the Cupboard class. It has no additional variables. Write down the necessary constructors for CupboardNarnia. Override the open method here. This time, it will print - "Open Narnia"

The class RoomOfRequirement inherits the Cupboard class.

It has a new private variable called password.

Write down the necessary constructors. Override the open method here.

 This time, the method will ask for a password input from the user.

 If the password matches the password in the class, then it will print

 "Open room of requirement". If the password is wrong, it will print

"There is no room of requirement".

Write a class Runner with a main method.

In the main method, write the following lines to check your methods:

Cupboard c = new Cupboard(5);

c.open(); c.addltems();

CupboardNarnia n = new CupboardNarnia();

RoomOfRequirement r = new RoomOfRequirement();

 n.addltems(3); r.addltems(100); Cupboard ref = n;

ref.open(); ref = r; ref.open();

Main Class:

package online_1;

public class Runner

{

    public static void main(String[] args)

    {

        Cupboard c = new Cupboard(5);

        c.open();

        c.addItems();

        CupboardNarnia n  = new  CupboardNarnia();

        RoomOfRequirement r = new RoomOfRequirement();

        n.addItems(3);

        r.addItems(100);

        Cupboard ref =n;

        ref.open();

        ref=r;

        ref.open();

             

    }

}

Cupboard Class:

 

 

 

package online_1;

 

public class Cupboard 

{

    public int items;

    

    public Cupboard()

    {

        this.items=0;

    }

    

    public Cupboard(int i)

    {

        this.items=i;

    }

    

    public void addItems()

    {

        this.items++;

    }

    

    public void addItems(int x)

    {

        this.items +=x;

    }

    

    public void open()

    {

        System.out.println("Cupboard Open");

    }

}

 

 

CupboardNarnia Class:

 

package online_1;

 

public class CupboardNarnia extends Cupboard 

{

   public int items;

   

   public CupboardNarnia()

   {

     

   }

    public void open()

    {

        System.out.println("Cupboard Narnia");

    }

          

}

 

RoomOfRequirement Class:

 

 

package online_1;

import java.util.Scanner;

 

 

public class RoomOfRequirement extends Cupboard

{

    private int password;

    

    public RoomOfRequirement(int x)

    {

        this.password =x;

    }

    public RoomOfRequirement()

    {

        this.password =1234;

    }

    

    public void open()

    {

        System.out.println("Password Please: ");

        Scanner s = new Scanner(System.in);

        int checkpassword = s.nextInt();

        

        if(checkpassword==password)

        {

            System.out.println("Open room of requirment");

        }

        else

        {

             System.out.println("This is no room of requirment");

        }

        

    }

}

Output

Cupboard Open

CupboardNarnia

Password Please:

1234

Open room of requirment

Join the discussion

Add your comment