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

Intro to JAVA Simple Programing 1. Please write the code according to the instru

ID: 3734432 • Letter: I

Question

Intro to JAVA Simple Programing 1.

Please write the code according to the instruction, such as line of code required etc, in bold.

import java.util.Stack ;

/**

Practices using the Stack from the Java API.

*/

public class StackApiTester

{

public static void main(String[] args)

{

Stack<String> stack = new Stack<String>() ;

stack.push("horse") ;

stack.push("cat") ;

stack.push("dog") ;

stack.push("cow") ;

stack.push("mouse") ;

System.out.println(stack.pop()) ;

System.out.println(stack.pop()) ;

System.out.println(stack.pop()) ;

System.out.println(stack.peek()) ;

System.out.println("stack is empty? " + stack.isEmpty()) ;

System.out.println(stack.pop()) ;

System.out.println("stack is empty? " + stack.isEmpty()) ;

System.out.println(stack.pop()) ;

System.out.println("stack is empty? " + stack.isEmpty()) ;

//-----------Start below here. To do: approximate lines of code = 8

// 1. make a stack that holds integers ;

//2. push in the integers from 1 to 100 that are divisible by 3 ;

//3. print the size ;

//4. while stack not empty ;

//5. let value hold pop ;

//6. if value divisible by 9 then print it.

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

}

Explanation / Answer

/*Modified java program that implements
* the comments provided in the program.*/
//StackApiTester.java
import java.util.Stack ;
public class StackApiTester
{
   public static void main(String[] args)
   {
       Stack<String> stack = new Stack<String>() ;
       stack.push("horse") ;
       stack.push("cat") ;
       stack.push("dog") ;
       stack.push("cow") ;
       stack.push("mouse") ;
       System.out.println(stack.pop()) ;
       System.out.println(stack.pop()) ;
       System.out.println(stack.pop()) ;
       System.out.println(stack.peek()) ;
       System.out.println("stack is empty? " + stack.isEmpty()) ;
       System.out.println(stack.pop()) ;
       System.out.println("stack is empty? " + stack.isEmpty()) ;
       System.out.println(stack.pop()) ;
       System.out.println("stack is empty? " + stack.isEmpty()) ;
      //-----------Start below here.
       // 1. Create stack that holds integers
       Stack<Integer> intStack = new Stack<Integer>() ;
       //2. push in the integers from 1 to 100 that are divisible by 3
       for (int i = 0; i < 100; i++)
           intStack.push(i+1) ;
       //3. print the size
       System.out.println("Size of int stack : "+intStack.size());
       //4. while stack not empty
       //5. let value hold pop
       //6. if value divisible by 9 then print it
       System.out.println("Divisible by 9");
       while(!intStack.isEmpty())
       {
           //Pop element from the intStack
           //store the element in num
           int num=intStack.pop();
           //check if num is divisible by 9
           if(num%9==0)
               System.out.println(num);
       }
       //-----------------End here. Please do not remove this comment.

   }
}//end of the class StackApiTester

Sample Output:

mouse
cow
dog
cat
stack is empty? false
cat
stack is empty? false
horse
stack is empty? true
Size of int stack : 100
Divisible by 9
99
90
81
72
63
54
45
36
27
18
9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote