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

Answer To Question No:3: STCK Tos Maxsize Tos Maxsize STCK Maxsize

The document defines a Stack class with methods like Push, Pop, isEmpty and isFull that implement a basic stack data structure. It then includes a main method that demonstrates using the Stack by taking user input to populate a stack, printing messages as it is filled and emptied, and handling overflow and underflow exceptions. The output shows the items being deleted from the stack as it is emptied through calls to the Pop method.

Uploaded by

Nisha Baruwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Answer To Question No:3: STCK Tos Maxsize Tos Maxsize STCK Maxsize

The document defines a Stack class with methods like Push, Pop, isEmpty and isFull that implement a basic stack data structure. It then includes a main method that demonstrates using the Stack by taking user input to populate a stack, printing messages as it is filled and emptied, and handling overflow and underflow exceptions. The output shows the items being deleted from the stack as it is emptied through calls to the Pop method.

Uploaded by

Nisha Baruwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Answer to Question no:3

import java.util.Scanner;
public class Stack{
int[] stck;
int tos;
int maxsize;
Stack(int size){
tos = -1;
this.maxsize = size;
stck = new int[maxsize];
}
void Push(int item){
if(tos ==maxsize-1){
System.out.println("STACK OVERFLOW"); }
stck[++tos] = item; }
int Pop() {
if (tos<0)
{
throw new RuntimeException("STACK UNDERFLOW");
}
else {
return stck[tos--];
}
}
boolean Isempty(){
return (tos<0);
}
boolean isfull(){
return (tos == maxsize-1);
}
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of stack:");
int s = sc.nextInt();
System.out.println("Enter "+s+" elements to stack:");
Stack s1 = new Stack(s);
for(int i =0;i<s;i++){
int num = sc.nextInt();
s1.Push(num);
}
System.out.println("Stack is full");
for(int j = 0;j<s;j++)
{
System.out.println("Item deleted:"+s1.Pop());
}
System.out.println("stack underflow");
System.out.println("stack is empty");
}
}
Output:

Answer to Question no: 1


package com.jetrbrains.Baruwal.COVIDUpdate.Stack;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class CeaserCypher {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("src\\com\\jetrbrains\\Baruwal\\COVIDUpdate\\Stack\\word.txt"));
PrintStream output = new PrintStream(new
File("src\\com\\jetrbrains\\Baruwal\\COVIDUpdate\\Stack\\output.txt"));
String teksti = input.nextLine();
ceaserdecrypt(teksti, output);
}
public static void ceaserdecrypt(String text, PrintStream output){
int shift = 6;
String decryptMessage = "";
for(int i=0; i < text.length();i++)
{
char alphabet = text.charAt(i);
if(alphabet >= 'a' && alphabet <= 'z')
{
alphabet = (char) (alphabet - shift);
if(alphabet < 'a') {
alphabet = (char) (alphabet-'a'+'z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
else if(alphabet >= 'A' && alphabet <= 'Z')
{
alphabet = (char) (alphabet - shift);
if (alphabet < 'A') {
alphabet = (char) (alphabet-'A'+'Z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
else
{
decryptMessage = decryptMessage + alphabet;
}
}
System.out.println(" decrypt message : " + decryptMessage);
}
}

Output:

Answer to Question no: 2


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static int calcMean(int[] numbers,int length) throws FileNotFoundException {
Scanner sf = new Scanner(new File("src\\com\\jetrbrains\\Baruwal\\COVIDUpdate\\Stack\\word.txt"));
int maxIndx = -1;
String text[] = new String[1000];
while(sf.hasNext())
{
maxIndx++;
text[maxIndx]=sf.nextLine();
}
sf.close();
String answer="";
int sum=0;
int average=0;
int n=0;
String a="";
for(int j=0;j<=maxIndx; j++)
{
Scanner sc=new Scanner(text[j]);
sum=0;
String s="";
n=0;
while(sc.hasNext())
{
int i=sc.nextInt();
sum=sum+i;
n=n+1;
}
average=sum/n;
System.out.println(average);
}
return average;
}
public static void main(String[] args) throws FileNotFoundException {
Main m = new Main();
int numbers[] = new int[0];
int length = 0;
calcMean(numbers,length);
}
}

You might also like