Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.18 KB | None | 0 0
  1.  
  2. //-- Scott Munro
  3. //--
  4. import java.util.*;
  5.  
  6. //create class myArray
  7. class myArray
  8. {
  9.    
  10.     //two private instance variables (size, len)
  11.     private int size;
  12.     private int len;
  13.    
  14.     //third instance variable
  15.     private double a[];
  16.    
  17.     //constructor, takes parameter named size
  18.     myArray(int size)
  19.     {
  20.         this.size = size;
  21.        
  22.         //allocate array
  23.         a = new double[this.size];
  24.         len = 0;
  25.     }
  26.    
  27.     //3 methods (display, reset, set)
  28.    
  29.     void display()
  30.     {
  31.         //traverse array
  32.         for(int i = 0; i < len; i++)
  33.         {
  34.             //print each element of array
  35.             System.out.print("a["+ i + "] = "+ a[i] + " ");
  36.         }
  37.         System.out.println("");
  38.     }
  39.    
  40.     void reset()
  41.     {
  42.         //traverses array
  43.         for(int i = 0; i < size; i++)
  44.         {
  45.             a[i] = -1;
  46.             len = 0;
  47.         }
  48.     }
  49.    
  50.     int len()
  51.     {
  52.         return len;
  53.     }
  54.    
  55.     //adds value to end of array
  56.     boolean add(double value)
  57.     {
  58.         boolean success=false;
  59.         //Return false if array is full
  60.         if(len<size)
  61.         {
  62.             success=true;
  63.             a[len]=value;
  64.             len++;
  65.         }
  66.        
  67.         return success;
  68.     }
  69.    
  70.     //adds value to specified location
  71.     boolean add(int element, double value)
  72.     {
  73.         boolean success = false;
  74.         //return false if array is full
  75.         if(len<size)
  76.         {
  77.             success=true;
  78.             //shift items to make room for insert
  79.             for(int i = len; i>element; i--)
  80.                 a[i]=a[i-1]; //shift
  81.             a[element] = value;
  82.             len++; 
  83.         }
  84.        
  85.         return success;
  86.     }
  87.    
  88.     double get(int element)
  89.     {
  90.         double value = -1;
  91.        
  92.         if (element<len)
  93.         {
  94.             value = a[element];
  95.         }
  96.        
  97.         return value;
  98.     }
  99.    
  100.     double set(int element, double value)
  101.     {
  102.         double old = -1;
  103.        
  104.         if(element<len)
  105.         {
  106.             old = a[element];
  107.             a[element] = value;
  108.         }
  109.        
  110.         return old;
  111.     }
  112.    
  113.     double remove(int element)
  114.     {
  115.         int success= -1;
  116.        
  117.         //check for valid index first
  118.         if(element<len)
  119.         {
  120.             success=0;
  121.             //shift list up starting at remove point
  122.             for(int i = element; i < len-1;i++)
  123.                 a[i]=a[i+1];
  124.             len--; 
  125.         }
  126.         return success;
  127.     }  
  128. }
  129.  
  130. class Array4
  131. {
  132.     public static void main( String args[])
  133.     {
  134.         Scanner scan = new Scanner (System.in);
  135.         System.out.print("Enter array size: ");
  136.         int size = scan.nextInt();
  137.         String flush = scan.nextLine();
  138.        
  139.         myArray A = new myArray(size);
  140.        
  141.         double val;
  142.         int ele;
  143.        
  144.         //Command interface
  145.         char nChar;
  146.         do
  147.         {
  148.             System.out.println("Choose Command: ");
  149.             System.out.println("<d>isplay, <r>eset,<l>en, <s>et, <a>dd, <i>nsert, r<e>move, <g>et, <q>uit");
  150.             String sCommand = scan.nextLine();
  151.             System.out.println("");
  152.             nChar = sCommand.charAt(0);
  153.             switch (nChar)
  154.             {
  155.                 case 'd':
  156.                     A.display();
  157.                     break;
  158.                
  159.                 case 'r':
  160.                     A.reset();
  161.                     break;
  162.                
  163.                 case 's':
  164.                     System.out.print("Enter element: ");
  165.                     ele = scan.nextInt();
  166.                     System.out.print("Enter value: ");
  167.                     val = scan.nextDouble();
  168.                     flush = scan.nextLine();
  169.                     double old = A.set(ele, val);
  170.                     if(old == -1)
  171.                         System.out.println("**SET OPERATION FAILED**");
  172.                     else
  173.                         System.out.println("Previous value was " + old);   
  174.                     break;
  175.                    
  176.                 case 'e':
  177.                     System.out.print("Enter element: ");
  178.                     ele = scan.nextInt();
  179.                     flush = scan.nextLine();
  180.                     if(A.remove(ele) == -1)
  181.                         System.out.println("**SET OPERATION FAILED**");
  182.                     break;
  183.                    
  184.                 case 'l':
  185.                     System.out.println("Length is " + A.len());
  186.                     break;
  187.                    
  188.                 case 'a':
  189.                     System.out.print("Enter value: ");
  190.                     val = scan.nextDouble();
  191.                     flush = scan.nextLine();
  192.                     if(!A.add(val))
  193.                         System.out.println("**ADD FAILED**");
  194.                     break;
  195.                    
  196.                 case 'i':              
  197.                     System.out.print("Enter element: ");
  198.                     ele = scan.nextInt();
  199.                     System.out.print("Enter value: ");
  200.                     val = scan.nextDouble();
  201.                     flush = scan.nextLine();
  202.                     if(!A.add(ele,val))
  203.                         System.out.println("**ADD FAILED**");
  204.                     break;
  205.                    
  206.                 case 'g':
  207.                     System.out.print("Enter element: ");
  208.                     ele = scan.nextInt();
  209.                     flush = scan.nextLine();
  210.                     val = A.get(ele);
  211.                     if(val == -1)
  212.                         System.out.println("**INVALID INDEX**");
  213.                     else
  214.                         System.out.println("Get value is "+ val);          
  215.                    
  216.                 case 'q':
  217.                     break;
  218.                    
  219.                 default:
  220.                     System.out.println("**INVALID COMMAND**"); 
  221.             }
  222.         }
  223.         while(nChar != 'q');
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement