Can anybody crack this..........?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Can anybody crack this..........?

  1. #1
    Join Date
    Feb 2004
    Posts
    3

    Can anybody crack this..........?

    Write, compile, and run a program to do the following. Add comments to each class and each method in the standard form (javadoc-compatible). Remember to define all variables at the beginning of each method. Submit the source files as a Reply to this posting.

    1. Write a static method displayArrayAsTable which takes three parameters: an array of integers, the number of elements in the array, and the number of integers to be displayed per line. The method should display the elements of the array in a table of the given number of integers per line (the last line may have to be shorter).

    2. In the main method:
    Prompt the user for the number of integer values to sort.
    Create an array of the entered size.
    Randomly generate the given number of integers and store the integers in the array.
    Display the array of integers in a table of 10 integers per line using displayArrayAsTable.
    Copy the array into another array. Sort the second array using selection sort. Display the name of the sort and its running time. Display the sorted array in a table of 8 integers per line using displayArrayAsTable.
    Copy the array into yet another array using insertion sort. Display the name of the sort and its running time. Display the sorted array in a table of 7 integers per line using displayArrayAsTable.
    Copy the array into yet another array. Sort the second array using bubble sort. Display the name of the sort and its running time. Display the sorted array in a table of 9 integers per line using displayArrayAsTable.

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    I'm sure there are many people who can "crack" that, but I seriously doubt you'll find any who'll post you the answer! lol

  3. #3
    Join Date
    Jan 2004
    Posts
    20
    Have you tried asking your Tutor/Lecturer for help on this marked assessment?


  4. #4
    Join Date
    Feb 2004
    Posts
    3

    yes i have......

    He has helped me all he said he would.........what I do not understand is the sort methods. I was never taught them and this guys a big *** and expects me to be able to learn them somewhere. This is the code I have got together so far...
    public class LabFour
    {
    public static void main(String[] args)
    {
    // instance fields
    String input;
    int number, numbers, i;
    Random generator;
    int []intialArray;


    // gets the input from the user
    input = JOptionPane.showInputDialog
    ("How many elements do you wish to have? ");
    number = Integer.parseInt(input);

    // generates random numbers rolled for the amount the user input
    intialArray = new int [number];
    generator = new Random();
    for (i = 0; i < number; i++)
    {
    numbers = 1 + generator.nextInt(number);
    intialArray[i] = numbers;
    }



    }

    public static void displayArrayAsTable(int[] a, int numE, int numD)
    {
    int i;
    for (i = 0; i < numE; i++)
    {
    System.out.print(a[i]);
    if ( i % numD == 0 )
    System.out.println();
    }
    }


    However when I try to show the first table of 10 numbers I get an error trying to output the static method displayArrayAsTable. Please someone help me out I would greatly appreciate it. Thank you.

  5. #5
    Join Date
    Jan 2004
    Posts
    20
    I fiddled with your code for a bit to get it working, but i had to comment out some of your code as i didn't have the Random class to work with, so i used some number literals.
    Code:
    public class LabFour
    {
        public static void main(String[] args)
        {
            // instance fields
            String input;
            int number, numbers = 0, i;
            //Random generator;
            int []intialArray;
    
            /* gets the input from the user
            input = JOptionPane.showInputDialog
                    ("How many elements do you wish to have? "); */
            number = 5;  //Integer.parseInt(input);
    
            // generates random numbers rolled for the amount the user input
            intialArray = new int [number];
            //generator = new Random();
            
            for (i = 0; i < number; i++)
            {
                numbers = 1 + 1; //generator.nextInt(number);
                intialArray[i] = numbers;
            }
            
            displayArrayAsTable(intialArray, number, numbers);
        }
        
        public static void displayArrayAsTable(int[] a, int numE, int numD)
        {
            int i;
            
            for (i = 0; i < numE; i++)
            {
                System.out.print(a[i]);
                if ( i % numD == 0 )
                System.out.println();
            }
        }
    }
    Hope that helps, you should be able to tweak it from there!

    Cheers, DJDaveMark

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by DJDaveMark
    Code:
            for (i = 0; i < numE; i++)
            {
                System.out.print(a[i]);
                if ( i % numD == 0 )
                System.out.println();
            }
    recommend:

    Code:
            for (i = 0; i < numE; i++)
            {
                System.out.print( (a[i]>9?a[i]:" "+a[i])  + ", ");
                if ( i % numD == 0 )
                System.out.println();
            }
    for array {1, 17, 5, 99, 2, 13, 67, 7, 97, 34, 22, 35, 41, 68, 12, 8}

    output:
    Code:
    old: 11759921367797
         3422354168128
    new:  1, 17,  5, 99,  2, 13, 67,  7, 97, 
         34, 22, 35, 41, 68, 12,  8
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Feb 2004
    Posts
    3

    heres what i got........

    thanks for the code but I have figured out most of it.......can somebody tell me if this looks ok...?



    import javax.swing.JOptionPane;
    import java.util.Random;


    /*
    Name: Devin Howton
    Date: February 17th, 2004
    Assignment: Create a program that will fill an array with random
    numbers which we will then place by value in a new
    array.
    */


    public class LabFour
    {

    public static void main(String[] args)
    {
    // instance fields
    String input;
    int number, numbers, i, temp, j;
    Random generator;
    int []intialArray;
    int []selectionArray;
    int []insertionArray;
    int []bubbleArray;


    // gets the input from the user
    input = JOptionPane.showInputDialog
    ("How many elements do you wish to have? ");
    number = Integer.parseInt(input);

    // generates random numbers rolled for the amount the user input
    intialArray = new int [number];
    generator = new Random();
    for (i = 0; i < number; i++)
    {
    numbers = generator.nextInt(number);
    intialArray[i] = numbers;
    }

    // copies the array into another array which will
    // sort using the selection sort method.
    selectionArray = new int [number];
    for (i = 0; i < number; i++)
    {
    selectionArray[i] = intialArray[i];
    }

    for (i = 0; i < selectionArray.length - 1; i++)
    {
    for (j = i+1; j < selectionArray.length; j++)
    {
    if (selectionArray[i] > selectionArray[j])
    {
    temp = selectionArray[i];
    selectionArray[i] = selectionArray[j];
    selectionArray[j] = temp;
    }
    }
    }

    // copies the array into another array which will
    // sort using the insertion sort method.
    insertionArray = new int [number];
    for (i = 0; i < number; i++)
    {
    insertionArray[i] = selectionArray[i];
    }

    for (i = 1; i < insertionArray.length; i++)
    {
    boolean inserted = false;
    j = i;
    while ((j >= 1) && (inserted == false))
    {
    if (insertionArray[j] < insertionArray[j-1])
    {
    insertionArray[j] = insertionArray[j-1];
    insertionArray[j-1] = insertionArray[j];
    }
    else
    {
    inserted = true;
    j--;
    }
    }
    }

    // copies the array into another array which will
    // sort using the bubble sort method.
    bubbleArray = new int [number];
    for (i = 0; i < number; i++)
    {
    bubbleArray[i] = insertionArray[i];
    }

    int n = bubbleArray.length;
    boolean notDone = true;
    while (notDone)
    {
    n--;
    notDone = false;
    for (i=0; i < n; i++)
    {
    if (bubbleArray[i] > bubbleArray[i+1])
    {
    temp = bubbleArray[i];
    bubbleArray[i] = bubbleArray[i+1];
    bubbleArray[i+1] = temp;
    notDone = true;
    }
    }
    }




    // displays first array of numbers not sorted
    displayArrayAsTable(intialArray, number, 10);
    System.out.println();
    displayArrayAsTable(selectionArray, number, 8);
    System.out.println();
    displayArrayAsTable(insertionArray, number, 7);
    System.out.println();
    displayArrayAsTable(bubbleArray, number, 9);



    }

    public static void displayArrayAsTable(int[] a, int numE, int numD)
    {
    int i;
    for (i = 0; i < numE; i++)
    {
    if ( i % numD == 0 )
    System.out.println();
    System.out.print(a[i] + " ");
    }
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center