Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
my project of array in java
i wanna to use random number in the code
how can do it
 /*
* This is the sorted class
*/


 

import java.util.*;

public class SortedArray
{
    static Scanner key = new Scanner(System.in);
    static int initialSize = 1;
    static int[] array = new int[initialSize];
    static int index = 1;
    static int getElement;
    static int delElement;


    public static void getArrayElement()
    {

        try
        {
            if(initialSize==1)
            {
                //Get the user input
                System.out.print("Enter the element: ");
                getElement = key.nextInt();

                //Assign the user input to the array
                for(int i=0; i<index; i++)
                {
                    array[i] = getElement;
                }

            }

            //If the size of the array is not 1 use this
            else
            {
                //Gets the user input
                System.out.print("Enter the element: ");
                getElement = key.nextInt();

                //Create a new empty array with a new size
                int[] temp = new int[index];

                //Assign the old array into the new array
                for(int j = 0; j < index-1; j++)
                {
                    temp[j] = array[j];
                }

                //Change the size of the old array
                array = new int [index];

                //Assign the temporary array into the new array with its new size
                for(int aSize = 0; aSize< array.length; aSize++)
                {
                    array[aSize] = temp[aSize];
                    int k = array.length;
                    array[k-1] = getElement;
                }

                //Pass the array into sortArray method for sorting
                sortArray(array, index);

             }

                //Increment the index and initialSize
               index++;
               initialSize++;
          }
          catch(InputMismatchException e)
         {
                System.out.println("Invalid Input");
                System.exit(0);
         }

      }

    //This is a bubble sort that sorts the array
    public static void sortArray(int a[], int size)
    {

        for(int sortSize = 0; sortSize < size-1; sortSize++)
        {
            int temp_number;

            for(int secondSize = (sortSize+1); secondSize < size; secondSize++)
            {
                if(a[sortSize] > a[secondSize])
                {
                    temp_number = a[sortSize];
                    a[sortSize] = a[secondSize];
                    a[secondSize] = temp_number;
                }
            }

        }

    }

    //Gets the sorted array
    public static int[] getArray()
    {
        return array;
    }

    public static void printArray()
    {
        int getSize = 0;
        //Outputs the sorted array
        for(int get =0; get < getArray().length; get++)
        {
            int[] tempArray = getArray();
            System.out.println(tempArray[get]);
            getSize++;

        }
            System.out.print("Size: " + getSize + "\n");
    }

    //Empty the array
    public static void clear()
    {

        index = 1;
        initialSize = 1;
        array = new int[initialSize];
        System.out.println("The array is empty now");
    }

    /*
    * This is the find method
    * If the target is found then delete the target in the array
    * else let the user know that target is not found
    */
    public static void search(int dE)
    {
        int target = dE;
        Boolean found = false;

        for(int fValue = 0; fValue < array.length; fValue++)
        {

                if(array[fValue]==target)
                {
                        for(int rElement = fValue; rElement < array.length-1; rElement++ )
                        {
                                array[rElement] = array[rElement+1];
                        }

                        int[] temp_Dele = new int[index-1];

                        for(int tempD = 0; tempD < index-1; tempD++)
                        {
                                temp_Dele[tempD] = array[tempD];
                        }

                        index--;
                        array = new int[temp_Dele.length-1];

                        for(int aDele = 0; aDele < array.length; aDele++)
                        {
                                array[aDele] = temp_Dele[aDele];
                        }

                    found = true;

                }


        }


        if(!found)
        {
                System.out.println("Element Not Found");
         }


    }

    //Delete the elements inside the array
    public static void deleteArray()
    {

            System.out.print("Enter the element to be delete:");
            delElement = key.nextInt();

            search(delElement);

    }

    //Printing the the smallest element of the array
    public static void smallest()
    {
            System.out.println(array[0]);
    }

    //Printing the largest element of the array
    public static void largest()
    {
            int lastIndex = array.length;

            //Printing the last element of the array
            System.out.println(array[lastIndex-1]);
    }

}

import java.util.*;

public class IntDriver extends SortedArray
{
    public static void main(String[] args) 
    {

        // Assigning Scanner to key
        Scanner key = new Scanner(System.in);

        //Holds the user selection
        int getSelection;

        /*
        * Asking the user for an input selection
        */
        try
        {
            do
            {

                  System.out.print("\n1) Insert 2) Delete 3) Clear 4) Smallest ");
                  System.out.print("5) Largest 6) Exit");

                  System.out.print("\nPlease enter your selection: ");
                  getSelection = key.nextInt();

                  if(getSelection == 1)
                 {
                    getArrayElement();
                    printArray();
                }

                if(getSelection == 2)
                {
                    deleteArray();
                    printArray();
                }
                if(getSelection == 3)
               {
                    clear();
                }
                if(getSelection == 4)
                {
                    smallest();
                }
                if(getSelection == 5)
                {
                    largest();
                }

            }while (getSelection != 6);
        }
      catch(InputMismatchException ae)
     {
         System.out.println("Invalid Input");
     }
  }

}

and how can calculate the running time ?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900