Beginners Java Assignment Help

Discussion in 'Mac Programming' started by zechmann, Nov 22, 2007.

  1. macrumors member

    Joined:
    May 8, 2007
  2. macrumors 6502a

    Joined:
    Sep 7, 2007
    Location:
    Boston, MA USA
    #2
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    The simplest way to sort an array in Java is to use one of the sort methods in the Arrays class. If your book does not tell you that then there is something seriously wrong.
     
  4. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
  5. macrumors 6502

    Joined:
    Dec 6, 2006
  6. thread starter macrumors member

    Joined:
    May 8, 2007
  7. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #7
    Your array isn't even in the loop. Create a for loop using I as a counter then put order=(int)(Math.random()*101); inside the for loop. This will create and put your 100 random numbers in your array. As a beginner coder myself I like your input stream reader
     
  8. thread starter macrumors member

    Joined:
    May 8, 2007
  9. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #9
    A few notes...

    1. Are you sure you are not allowed to use the built-in array sort method to accomplish this? If you're in an introductory Java class and you walk in having implemented your own bubble sort when that isn't a requirement, your instructor is going to suspect you've plagiarized some code from somewhere.

    2. Is it a requirement of your assignment that the length of your array is an even number under 10000? I only ask because it's what your code does, and you didn't mention it.

    3. Your input value test accepts zero or negative numbers. Since these do not make sense for an array length, it probably isn't what you want.

    4. If your user puts in an invalid value, he just sees the same prompt again, with no indication why. Provide more information upfront about the restrictions on possible input values, and if you can, tell the user that his input was not valid before prompting him again.

    5. Your prompt is worded using a very casual tone. Some instructors will care. Later on, all employers will care. Be formal.

    6. You printed your original array, albeit with linebreaks between numbers, but then later you said you don't know how to print your sorted array. This confuses me. What difference do you think is relevant?
     
  10. thread starter macrumors member

    Joined:
    May 8, 2007
  11. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #11
    Clarifying the assignment fixes most of my concerns except:

    1. Your prompt says "gonna" where the specified prompt says "going to". If I were your very strict professor, I'd dock points for that.

    2. Your code accepts negative numbers, which the specification forbids, and rejects positive odd numbers, which the specification does not mention. Have the painkillers led you to confuse "positive" with "even"? Understandable in a way, but you should fix this.

    BONUS: Don't worry about this if you're short on time, because the specification doesn't say to explicitly, but if you have time to go for suckup points, when the user inputs something invalid tell him why it's invalid before displaying the specified prompt again.

    I don't see anything immediately wrong with your bubble sort, but I haven't run it or checked it thoroughly. I'm just assuming it works. You shouldn't.

    Conceptually, a bubble sort is more or less the simplest kind of sort you can do. It does several passes over the array. There isn't any need to break the algorithm up this way in implementation, but it makes more sense to think of the inner loop as its own function, "pushBiggestNumberToEnd." That's all it does, and it does it by checking each number against its immediate neighbor to the right. The bigger number always ends up to the right of the smaller number. In this way the big number floats out to the position you want it in.

    The outer loop simply calls pushBiggestNumberToEnd on a shrinking subset of the entire array. The first pass operates on the entire array, [0..n]. Once the first pass is done, you know the last number in the array (at n) is in the right position, so you can ignore it.

    The next pass calls pushBiggestNumberToEnd on the subset of the array [0..n-1]. This has the effect of pushing the second biggest number in the array to the position n-1. This is the right position for that number, so the outer loop now runs on [0..n-2] and so forth until it has sorted the entire array.

    Selection Sort is another simple sort algorithm that works sort of like an inside-out version of bubble sort. The inner "function" in the selection sort would be "swapSmallestWithFirst." This function examines each element in an array (or portion of an array) and keeps track of the location of the smallest number it encounters. When it reaches the end of the array, it swaps the number at the remembered location with the number at the first location in the array. At the end of a pass, you know that the first number in the portion of the array you're working on is the smallest number in the entire portion.

    Thus the outer loop in Selection Sort behaves like in Bubble Sort, except it shrinks at the small end instead of the big end. It puts the correct number into position 0, then position 1 and so forth until it has sorted the entire array.
     
  12. macrumors 68020

    Jasonbot

    Joined:
    Aug 15, 2006
    Location:
    The Rainbow Nation RSA
    #12
    Code:
    //
    
    import java.util.*;
    
    public class srt {
    
        public static void main (String args[]) {
            int a [] ={12,3,4,345,123,13,45,123123,43,1};
    		int x;
    		
    		str.sort(a[1]);
    		
    		do{
    			i++;
    			System.out.println( a [i]);
    			x= a[i];
    		}while (x!=null);
    			
        }
    	public static void sort(int[] a, int a[0], int a[10]){
    		
    	}
    }
    
    sorry to interrupt the thread, where is my sort messing up?
     
  13. macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #13
    You just need to check your basic Java syntax. I'm not even exactly sure what you're trying to accomplish with some of this.
     

Share This Page