You are on page 1of 8

Question 1:

A class that has been defined to keep the books from bottom to top one by

one till the space is available. The details of the class Rack are given below:

Class name: Rack

Data data members/instance variables:

rack[ ] : string to hold a maximum of 100 books.

bkNam : string to store book name

size : story the capacity of the array

point: to print the current TOP Index in the rack

Member functions/methods:

Rack(int cc): constructor to initialise data members size=cc , point=-1 and to

create the string array.

voidkeepBook(): to input the name of a book in bkNam and keep it in the rack

at the current point index if space is available, otherwise display the message

rack is full.

voidtakeBook():to remove a book from the rack and print if rack is not empty

otherwise print a message rack is empty

Void printBooks():display the book names present in the rack, if rack is empty

then print "rack is empty".


Algorithm:

class Rack

Step 1: start

Step 2: an array rack of String type of size 10is declared

Step 3: instance variable bkNam of String type is declared

Step 4: instance variables i, size , point is declared

public static void main (String args [])

Step 1: start

Step 2: size of the rack is taken input and store in s

Step 3: object of the class is created

Step 4: methods are called

Step 5: end

Rack (int cc)

Step 1: start

Step 2: initialise size=cc , point=-1, and create a string array

Step 3: end

voidkeepBook ()

Step 1: start

Step 2: input name of the book in bkNam and keep in rack if space is available

otherwise display a

message "rack is full"

Step 3: end

voidtakeBook ()

Step 1: start

Step 2: local variable b of string type is declared


Step 3: remove a book from the rack and print if rack is not empty otherwise print a

message rack is empty

Step 4: end

voidprintBooks ()

Step 1: Start

Step:2Display the book names present in the rack,if rack is empty then print “rack s

empty”

Class Ended
import java.util.*;

class Rack

String rack[]=new String [10];

String bkNam;

inti, size,point;

Rack(int cc)

size=cc;

point =-1;

for(i=0;i<size;i++)

rack[i]="";

voidkeepBook()

Scanner sc=new Scanner(System.in);

System.out.println("enter the name of the book");

bkNam=sc.nextLine();

if(point==(size-1))

System.out.println("Rack is full");

else
{

point++;

rack[point]=bkNam;

voidtakeBook()

String b;

if(point==-1)

System.out.println("Rack is empty")

else

b=rack[point];

System.out.println("Removed Book="+b);

point--;

voidprintBook()

if(point==-1)

System.out.println("Rack is empty");

}
else

System.out.println("Book Name:");

for(i=(point-1);i>=0;i--)

System.out.println(rack[i]);

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of rack");

int s=sc.nextInt();

Rack ob=new Rack(s);

ob.keepBook();

ob.keepBook();

ob.keepBook();

ob.takeBook();

ob.printBook();

}
OUTPUT:

Enter the size of rack

Enter the name of book

computer

Enter the name of book

economics

Enter the name of book

language

Removed Book = language

Book Name: computer


Variable Description Table:

Variable Data Type Description

rack[] String String to hold maximum of 100


book

bkname String String to store book name

size int Integers to store the capacity of


the array

point int Integers to point the Top index of


the rack

b String String to Store element

You might also like