Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the subject is data structure i need the answer in java code please ASAP Given t

ID: 3912664 • Letter: T

Question

the subject is data structure i need the answer in java code please ASAP Given the following classes and implementation for the stack
  Write a java method named displayOdd. The method displays all the odd number one by one. At the end the method should also display the number of odd elements and the number of even elements. Test the method in the main program.



import java.util.*;
public class ArrayStack {
private int[] data;         
private int t = -1;    // index of the top element in stack
public ArrayStack(int capacity) {       
    data = new int[capacity];    
}
public int size() {
return (t + 1);
}
public void push(int e){
    ++t;
    data[t] = e;                          
}

public int top() {
      return data[t];
}

public int pop() {
    int answer = data[t];
    t--;
    return answer;
}
  
public String toString() {
    String str = "";
    for (int j = t; j >= 0; j--) {
      str=str+" "+ data[j];
    }
    return str;
}
}// end of ArrayStack class
  
   
//Class Implementation
import java.util.*;
import java.util.Scanner;

//Implementation
public class TestStack {
     static Scanner console = new Scanner(System.in);
     public static void main(String[] args) {
     Scanner in = new Scanner(System.in);
       int element;
       int oddS;
      ArrayStack myStack = new ArrayStack(100);
      for(int i=0; i<5; ++i){
         System.out.println("Enter an Element: ");
         element = in.nextInt();
          myStack.push(element);
      }
     //call the method displayOdd here
   }
}

Explanation / Answer

//ArrayStack.java file
import java.util.*;
public class ArrayStack {
private int[] data;
private int t = -1; // index of the top element in stack
public ArrayStack(int capacity) {
data = new int[capacity];   
}
public int size() {
return (t + 1);
}
public void push(int e){
++t;
data[t] = e;   
}

public int top() {
return data[t];
}

public int pop() {
int answer = data[t];
t--;
return answer;
}
  
public String toString() {
String str = "";
for (int j = t; j >= 0; j--) {
str=str+" "+ data[j];
}
return str;
}
//definition of method displayOdd
public void displayOdd(){
int oddCount=0,evenCount=0;
System.out.printf("Displaying odd numbers: ");
//System.out.printf("Number of eements : %d ",t);
for(int i = 0; i <= t; i++)
{
//System.out.printf("%d ",data[i]);
if(data[i]%2 == 1) //element is odd
{
System.out.printf("%d ",data[i]);
oddCount++;
}
else
{
evenCount++;
}
}
System.out.printf("Count of odd numbers: %d ",oddCount);
System.out.printf("Count of even numbers: %d ",evenCount);
}

}// end of ArrayStack class

---------------------------------------------------------------
//TestStack.java
import java.util.Scanner;
import java.io.*;
public class Main
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int element;
int oddS;
ArrayStack myStack = new ArrayStack(100);
for(int i=0; i<5; ++i){
System.out.println("Enter an Element: ");
element = in.nextInt();
myStack.push(element);
}
myStack.toString();
//call the method displayOdd here
myStack.displayOdd();
}
}

-------------------------------------------------------------------------------------
//output
Enter an Element:
12   
Enter an Element:
3
Enter an Element:
9
Enter an Element:
10   
Enter an Element:
22   
Displaying odd numbers:
3
9
Count of odd numbers: 2
Count of even numbers: 3