Run the following Java code and explain what it does. Give the resulting image from the code using inputs 3,960 1,234 222,222 13,780
Need it asap please !!!
Will rate your answer!!!
Run the following Java code and explain what it does. Give the resulting image from the code using inputs 3,960 1,234 222,222 13,780
interface ArrayStackADT
{
public void pop();
public void push(Object a);
public void display();
public boolean isEmptyStack();
public boolean isFullStack();
}
class ArrayStackDataStrucClass<T> implements ArrayStackADT
{
// An object of type T is declared
final static int n=100;
T stack[];
int top=-1;
// Default constructor
ArrayStackDataStrucClass() {
this.stack = (T[]) new Integer[100];
}
//Overloaded Constructor
ArrayStackDataStrucClass(T obj[]) {
this.stack = obj;
}
@Override
public void push(Object obj)
{
T t1= (T) obj;
try
{
if(!isFullStack())
{
stack[++top]=t1;
}
}
catch(Exception e)
{
System.out.println("e");
}
}
@Override
public void pop()
{
T popper = null;
if(top<0)
{
System.out.println("Stack underflow");
return;
}
else
{
popper=stack[top];
top--;
//System.out.println("Popped element:" +popper);
}
}
@Override
public void display()
{
if(!isEmptyStack())
{
String str=" ";
for(int i=0; i<=top; i++)
str=str+" "+stack[i]+" <--";
System.out.println("Elements are:"+str);
}
}
@Override
public boolean isEmptyStack() {
if(top<0)
{
System.out.println("Stack is empty");
return true;
}
return false;
}
@Override
public boolean isFullStack() {
if(top==(n-1))
{
System.out.println(" Stack Overflow");
return true;
}
return false;
}
T peek()
{
if(!isEmptyStack())
{
return stack[top];
}
return null;
}
}
public class PrimeFactorizationDemoClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr={2960,1234,222222,13780};
for(int ii=0;ii<arr.length;ii++)
{
int inputNum = arr[ii];
int num = inputNum;
Integer intObj1[] = new Integer[100];
try {
ArrayStackDataStrucClass<Integer> obj1 = new ArrayStackDataStrucClass(intObj1);
int top=-1, i;
// Print the number of 2s that divide n
while (num % 2 == 0) {
obj1.push(2);
num /= 2;
top++;
}
// num must be odd at this point. So we can
// skip one element (Note i = i +2)
for ( i = 3; i <= Math.sqrt(num); i += 2) {
// While i divides n, print i and divide n
while (num % i == 0) {
obj1.push(i);
num /= i;
top++;
}
}
// This condition is to handle the case whien
// num is a prime number greater than 2
if (num > 2)
{
obj1.push(num);
top++;
}
System.out.print("Prime Factor for "+inputNum+" is : ");
while(top != -1)
{
System.out.print(obj1.peek() );
if(top != 0)
System.out.print(" * " );
obj1.pop();
top--;
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("");
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images