0% found this document useful (0 votes)
12 views

Assignment 28

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment 28

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ASSIGNMENT 28: (27.09.

2024)
A class Repeat is which allows the user to add elements from one end (rear) and
remove elements from the other end (front) only. The following details of the
class Repeat are given below:

Class name: Repeat

Data Members:

st[]: an array to hold a maximum of 100 integer elements.

cap: stores the capacity of the array.

f: to point to the index of the front.

r: to point to the index of the rear.

Member functions:

Repeat(int m): constructor to initialize the data members cap=m, f=0,r=0 and to
create the integer array.

void pushvalue(int v): to add integers from the rear index if possible else display
the message “overflow”.

int popvalue(): to remove and return element from the front, if array is empty
then return -9999.

void disp(): displays the elements present in the list.


Algorithm:
1. Class Defini on and Data Members
 Repeat Class:

o Data Members:

 int[] st: An array to store the elements of the queue.

 int cap: The capacity of the queue (maximum number of

elements it can hold).


 int f: The front index (keeps track of where to remove an

element).
 int r: The rear index (keeps track of where to add an element).

2. Constructor (Repeat)
 Ini alizes the queue with a specified capacity:

o cap = m: Sets the capacity of the queue.

o f = 0: Ini alizes the front index to 0 (the start of the queue).

o r = 0: Ini alizes the rear index to 0 (the end of the queue).

o st = new int[cap]: Creates an integer array st with a size equal to the

specified capacity m.
3. Method: pushvalue(int v)
 Adds a value to the rear of the queue:

o Checks if the queue is full (if r == cap), prints "Overflow" if true.

o Otherwise, stores the value v at the rear index r, then increments the

rear index (r++).


4. Method: popvalue()
 Removes and returns the element at the front of the queue:

o Checks if the queue is empty (if f == r), and returns -9999 to indicate

that the queue is empty.


o Otherwise, stores the front element (st[f]), increments the front

index (f++), and returns the stored value.


5. Method: disp()
 Displays the current elements of the queue:

o Checks if the queue is empty (if f == r), and prints "The list is empty."

if true.
o Otherwise, iterates over the queue from index f to r, prin ng each

element, and then prints a newline at the end.


6. Main Method
 Input and Ini aliza on:
o Creates a Scanner object (sc) to read input from the user.
o Asks the user to enter the capacity of the queue, and ini alizes the

Repeat object repeatQueue with this capacity.


 Menu and Loop:
o The program enters a do-while loop that con nuously displays a

menu and processes user input un l the user selects "Exit".


o Inside the loop:

 Displays a menu with four op ons: push a value, pop a value,

display the queue, and exit.


 Reads the user's choice and performs the corresponding ac on

using a switch statement.


o Case 1: Push Value:

 Prompts the user to enter a value to push.

 Calls pushvalue(value) to add the value to the queue.

o Case 2: Pop Value:

 Calls popvalue() to remove and retrieve the front element of

the queue.
 If the queue is empty (returns -9999), prints "The queue is

empty." Otherwise, prints the popped value.


o Case 3: Display Queue:

 Calls disp() to display all the elements currently in the queue.

o Case 4: Exit:

 Prints "Exi ng..." and breaks out of the loop to terminate the

program.
o Default Case:

 If the user enters an invalid choice, the program prints an error

message and prompts the user to try again.


 Loop Termina on:
o The loop con nues running un l the user selects op on 4 ("Exit").

 Closing the Scanner:


o Once the loop ends (a er selec ng "Exit"), the Scanner object sc is

closed to free up resources.


Variable Listing Table:

Variable Type Scope Purpose


st int[] Repeat class Array to store
integer elements
for the queue.
cap int Repeat class Holds the capacity of
the queue.
f int Repeat class Tracks the front
index for removing
elements.
r int Repeat class Tracks the rear
index for adding
elements.
sc Scanner main method Used to read input
values from the
user.
repeatQueue Repeat main method Instance of Repeat
to perform queue
operations.
choice int main method loop Stores the user's
menu choice.
value int main method loop Stores the integer
to be added to the
queue.
poppedValue int main method loop Stores the integer
removed from the
front of the queue.
Method Listing Table:

Method Prototype Purpose

Repeat public Repeat(int Constructor that initializes


m) the queue’s capacity and
indices.

pushvalue void Adds an integer v to the


pushvalue(int v) queue if there is space (at r
index).

popvalue int popvalue() Removes and returns the


integer at the front (f) if the
queue isn’t empty.

disp void disp() Displays all elements from


the front (f) to the rear (r).

main public static void Drives the program by


main(String[] providing a menu for queue
args) operations.
Class Listing Table:

Class Description
Repeat Represents a queue-like structure with
operations for adding, removing, and
displaying elements.
main Contains the main method to test the
functionality of the Repeat class.
OUTPUT:

You might also like