Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-- Scott Munro
- //--
- import java.util.*;
- //create class myArray
- class myArray
- {
- //two private instance variables (size, len)
- private int size;
- private int len;
- //third instance variable
- private double a[];
- //constructor, takes parameter named size
- myArray(int size)
- {
- this.size = size;
- //allocate array
- a = new double[this.size];
- len = 0;
- }
- //3 methods (display, reset, set)
- void display()
- {
- //traverse array
- for(int i = 0; i < len; i++)
- {
- //print each element of array
- System.out.print("a["+ i + "] = "+ a[i] + " ");
- }
- System.out.println("");
- }
- void reset()
- {
- //traverses array
- for(int i = 0; i < size; i++)
- {
- a[i] = -1;
- len = 0;
- }
- }
- int len()
- {
- return len;
- }
- //adds value to end of array
- boolean add(double value)
- {
- boolean success=false;
- //Return false if array is full
- if(len<size)
- {
- success=true;
- a[len]=value;
- len++;
- }
- return success;
- }
- //adds value to specified location
- boolean add(int element, double value)
- {
- boolean success = false;
- //return false if array is full
- if(len<size)
- {
- success=true;
- //shift items to make room for insert
- for(int i = len; i>element; i--)
- a[i]=a[i-1]; //shift
- a[element] = value;
- len++;
- }
- return success;
- }
- double get(int element)
- {
- double value = -1;
- if (element<len)
- {
- value = a[element];
- }
- return value;
- }
- double set(int element, double value)
- {
- double old = -1;
- if(element<len)
- {
- old = a[element];
- a[element] = value;
- }
- return old;
- }
- double remove(int element)
- {
- int success= -1;
- //check for valid index first
- if(element<len)
- {
- success=0;
- //shift list up starting at remove point
- for(int i = element; i < len-1;i++)
- a[i]=a[i+1];
- len--;
- }
- return success;
- }
- }
- class Array4
- {
- public static void main( String args[])
- {
- Scanner scan = new Scanner (System.in);
- System.out.print("Enter array size: ");
- int size = scan.nextInt();
- String flush = scan.nextLine();
- myArray A = new myArray(size);
- double val;
- int ele;
- //Command interface
- char nChar;
- do
- {
- System.out.println("Choose Command: ");
- System.out.println("<d>isplay, <r>eset,<l>en, <s>et, <a>dd, <i>nsert, r<e>move, <g>et, <q>uit");
- String sCommand = scan.nextLine();
- System.out.println("");
- nChar = sCommand.charAt(0);
- switch (nChar)
- {
- case 'd':
- A.display();
- break;
- case 'r':
- A.reset();
- break;
- case 's':
- System.out.print("Enter element: ");
- ele = scan.nextInt();
- System.out.print("Enter value: ");
- val = scan.nextDouble();
- flush = scan.nextLine();
- double old = A.set(ele, val);
- if(old == -1)
- System.out.println("**SET OPERATION FAILED**");
- else
- System.out.println("Previous value was " + old);
- break;
- case 'e':
- System.out.print("Enter element: ");
- ele = scan.nextInt();
- flush = scan.nextLine();
- if(A.remove(ele) == -1)
- System.out.println("**SET OPERATION FAILED**");
- break;
- case 'l':
- System.out.println("Length is " + A.len());
- break;
- case 'a':
- System.out.print("Enter value: ");
- val = scan.nextDouble();
- flush = scan.nextLine();
- if(!A.add(val))
- System.out.println("**ADD FAILED**");
- break;
- case 'i':
- System.out.print("Enter element: ");
- ele = scan.nextInt();
- System.out.print("Enter value: ");
- val = scan.nextDouble();
- flush = scan.nextLine();
- if(!A.add(ele,val))
- System.out.println("**ADD FAILED**");
- break;
- case 'g':
- System.out.print("Enter element: ");
- ele = scan.nextInt();
- flush = scan.nextLine();
- val = A.get(ele);
- if(val == -1)
- System.out.println("**INVALID INDEX**");
- else
- System.out.println("Get value is "+ val);
- case 'q':
- break;
- default:
- System.out.println("**INVALID COMMAND**");
- }
- }
- while(nChar != 'q');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement