Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Medication of ArrayDemo.java to include iterative menu

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Medication of ArrayDemo.java to include iterative menu

    Activity 1-1
    Modify ArrayDemo.java to include this iterative menu:

    [1] Display contents of the array using for loop (by index, ascending)
    [2] Display contents of the array using do loop (by index, ascending)
    [3] Display contents of the array using do while loop (by index, ascending)
    [4] Display contents of the array using value (by index, descending)
    [5] Exit

    class ArrayDemo {
         public static void main(String[] args) {
     
              int[] anArray;              // declares an array of integers
     
              anArray = new int[10];      // allocates memory for 10 integers
     
              anArray[0] = 100; // initialize first element
              anArray[1] = 200; // initialize second element
              anArray[2] = 300; // etc.
              anArray[3] = 400;
              anArray[4] = 500;
              anArray[5] = 600;
              anArray[6] = 700;
              anArray[7] = 800;
              anArray[8] = 900;
              anArray[9] = 1000;
     
     
              System.out.println("Element at index 0: " + anArray[0]);
              System.out.println("Element at index 1: " + anArray[1]);
              System.out.println("Element at index 2: " + anArray[2]);
              System.out.println("Element at index 3: " + anArray[3]);
              System.out.println("Element at index 4: " + anArray[4]);
              System.out.println("Element at index 5: " + anArray[5]);
              System.out.println("Element at index 6: " + anArray[6]);
              System.out.println("Element at index 7: " + anArray[7]);
              System.out.println("Element at index 8: " + anArray[8]);
              System.out.println("Element at index 9: " + anArray[9]);
         }
    }


  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: another activity that i need some guidance

    can someone help me or assist me please.....

  3. #3
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: another activity that i need some guidance

    Quote Originally Posted by xyldon27 View Post
    can someone help me or assist me please.....
    okay, just wait for my code.

    Cheers,
    Truffy

  4. The Following User Says Thank You to Truffy For This Useful Post:

    xyldon27 (June 30th, 2009)

  5. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: another activity that i need some guidance

    thanks.. i really need it.. because my heads hurts today because of the heat of the sun.. but still im trying to figure it out..

  6. #5
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: another activity that i need some guidance

    Quote Originally Posted by xyldon27 View Post
    thanks.. i really need it.. because my heads hurts today because of the heat of the sun.. but still im trying to figure it out..
    Hi xyldon27,
    Here's the code for you.

    Hope it helps.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
     
    /**
     *
     * @authorTruffy
     */
    class ArrayDemo {
            public void forsortIntArray() {
     
            int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
     
            Arrays.sort(arrayToSort);
     
            for (int i = 0; i < arrayToSort.length; i++)
                System.out.println(arrayToSort[i]);
        }
             public void doLoopsortIntArray() {
     
            int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
     
            Arrays.sort(arrayToSort);
     
     
             int loopvar = 0; // Declare and initialize the loop counter
    do {
    System.out.println(arrayToSort[loopvar]); // Print the variable
    loopvar = loopvar + 1; // Increment the loop counter
    } while (loopvar < 9); // Test and loop 
     
        }
     
             public void whileLoopsortIntArray() {
     
            int[] arrayToSort = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
     
            Arrays.sort(arrayToSort);
     
     
             int loopvar = 0; // Declare and initialize the loop counter
     
     
    while (loopvar < 9){
    System.out.println(arrayToSort[loopvar]); 
    loopvar = loopvar + 1;     
    }
     
        }
     
               public void sortIntArray() {
     
            int[] intArray = new int[] {100, 200,300, 400, 500, 600, 900, 800, 700};
     
    //        Arrays.sort(intArray);
     
                  // translate int array over to an Integer Array     
             Integer[] integerArray = new Integer[intArray.length];  
         for (int i = 0; i < intArray.length; i++)  
         {  
          integerArray[i] = new Integer(intArray[i]);  
         }  
        // sort with an anonymous Comparator object  
         Arrays.sort(integerArray, new Comparator<Integer>()  
         {  
           public int compare(Integer o1, Integer o2)  
           {  
             return o2.intValue() - o1.intValue();  
           }  
         });  
         System.out.println(Arrays.toString(integerArray));  
        }
     
     
     
          public static void main(String[] args) {
              ArrayDemo arr = new ArrayDemo();
              System.out.println("[1] Display contents of the array using for loop (by index, ascending)");
              System.out.println("[2] Display contents of the array using do loop (by index, ascending)");
              System.out.println("[3] Display contents of the array using while loop (by index, ascending)");
              System.out.println("[4] Display contents of the array using value (by index, descending)");
              System.out.println("[5] Exit\n");
     
     
              Scanner sc = new Scanner(System.in);
              while(sc.hasNext()){
     
            String input = sc.next();
     
            if(input.equals("5")){
                System.out.println("Bye! Bye!");
                System.exit(0);
            }else if(input.equals("1")){
                arr.forsortIntArray();
     
            }else if(input.equals("2")){
                arr.doLoopsortIntArray();
            }else if(input.equals("3")){
                arr.whileLoopsortIntArray();
            }else if(input.equals("4")){
                arr.sortIntArray();
     
            }
            System.out.println("Enter cmd: ");
            }
     
     
        }
     
     
     
    }


    output
    compile-single:
    run-single:
    [1] Display contents of the array using for loop (by index, ascending)
    [2] Display contents of the array using do loop (by index, ascending)
    [3] Display contents of the array using while loop (by index, ascending)
    [4] Display contents of the array using value (by index, descending)
    [5] Exit

    1
    100
    200
    300
    400
    500
    600
    700
    800
    900
    Enter cmd:
    2
    100
    200
    300
    400
    500
    600
    700
    800
    900
    Enter cmd:
    3
    100
    200
    300
    400
    500
    600
    700
    800
    900
    Enter cmd:
    4
    [900, 800, 700, 600, 500, 400, 300, 200, 100]
    Enter cmd:
    5
    Bye! Bye!

    Cheers,
    Truffy.

  7. The Following 2 Users Say Thank You to Truffy For This Useful Post:

    JavaPF (July 3rd, 2009), xyldon27 (June 30th, 2009)

  8. #6
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: another activity that i need some guidance

    I'LL TEST IT RIGHT AWAY ^_^ THANK YOU SO MUCH

  9. #7
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: another activity that i need some guidance

    Quote Originally Posted by xyldon27 View Post
    I'LL TEST IT RIGHT AWAY ^_^ THANK YOU SO MUCH
    if you are satisfied and that is correct, please mark as solved.

    Cheers,
    Truffy.

  10. #8
    Junior Member
    Join Date
    Jun 2009
    Posts
    21
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: another activity that i need some guidance

    it work thank u so much.. cheers for Truffy

  11. #9
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: another activity that i need some guidance

    Quote Originally Posted by xyldon27 View Post
    it work thank u so much.. cheers for Truffy
    please mark it solved then.