idastan97

CSCI152 L10 P2

Feb 13th, 2017
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. public class testClass {
  2.    
  3.     public static boolean isIncreasing(Stack<Double> st){
  4.         Stack<Double> tmp=new LinkedListStack();
  5.         boolean res=true;
  6.         while (st.getSize()>0){
  7.             try{
  8.                 tmp.push(st.pop());
  9.             } catch(Exception ex){
  10.                 System.out.println(ex.getMessage());
  11.             }
  12.         }
  13.        
  14.         if (tmp.getSize()>0){
  15.             double prev;
  16.             try{
  17.                 prev=tmp.pop();
  18.                 st.push(prev);
  19.             } catch(Exception ex){
  20.                 System.out.println(ex.getMessage());
  21.                 prev=0.0;
  22.             }
  23.             while (tmp.getSize()>0){
  24.                 try{
  25.                     double newDouble=tmp.pop();
  26.                     if (res && newDouble<prev){
  27.                         res=false;
  28.                     }
  29.                     prev=newDouble;
  30.                     st.push(prev);
  31.                 } catch(Exception ex){
  32.                     System.out.println(ex.getMessage());
  33.                 }
  34.             }
  35.         }
  36.         return res;
  37.     }
  38.    
  39.     public static void main(String[] args){
  40.        
  41.         Stack<Double> st=new LinkedListStack();    
  42.         for (double i=1.0; i<=12.0; i=i+1.0){
  43.             st.push(i);
  44.         }
  45.         System.out.println(isIncreasing(st));
  46.         System.out.println(st+"\n");
  47.        
  48.         st.push(1.0);
  49.         System.out.println(isIncreasing(st));
  50.         System.out.println(st);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment