Results 1 to 4 of 4
  1. #1
    Dr_Death is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Need Help with Example

    My first try at creating a small class with lots of explanations. This is for people in my Comp Sci class, so any feed back is appreciated. Here is the code and also a zip with the actual project:

    /*This program is an example of how to use the different type of for loops using an array of type int
    *if comment starts with a !! then it is extra information that is not necessary for*
    *the completion of the program
    */
    Java Code:
    public class index {
    	
    	public static void main (String [] args){
    		
    		int[] a = new int[5]; //created an array with the ability to hold 5 integers
    		                      /*!! this array can also be created by putting the brackets after
    							  *the variable name as such:
    							  *int a[] = new int[]; 
    							  */
    
    		System.out.println("First array :: ");
    		
    		for (int i = 0; i < a.length; i++){  
    			a[i] =(int) (Math.random()*20);      //This creates a random number between 1 and 20
    			System.out.print(" " + a[i]);        //!! The Math.random method needs to be casted to (int) because, by default, it is supposed to return a double, and we need and int
    		}	                                     //!! It needs to be multiplied by 20 because this class also, by default, only returns a number between 0 and 
    			
    		printOddAndEven(a);                      //What is happening here is that I have called a method named printOddAndEven and given it 1 parameters (a)
    		                                         //This method is create below, it is not a default java method
    	}
    	
    	//REMEMBER TO CREATE OTHER METHODS OUTSIDE OF MAIN CLASS
    	
    	
    	/* 
    	*public means it can be accessed by other classes, alternatively private means that it can only be used in this class 
    	*static means that i can call this method without creating an object. This is why you can use Math.random() without creating a Math object 
    	*void means it doesn't return anything, it's just printing something out
    	*printOddAndEven is the name that we gave in the main class
    	*(int c[]) is the parameter; remember that the variable type must match the variable type we gave it in the main method, i.e a must be an int[]
    	*/
    	//This class is going to check the 2 arrays and see if there are any matches and print them out
    	private static void printOddAndEven(int c[]){ 
    		int count=0,odds=0, evens=0, numsEven[]= new int[c.length], numsOdds[] = new int[c.length];	 //!!You can create an int and an int[] in the same line
    		String numsOddsPrint = new String("[ ");		 // Creating a String to put the array into !! or else when you try to print the array you would end up with
    		String numsEvenPrint = new String("[ ");         // something like [I@76cc518c which is the hexadecimal location of your array in the memory of the computer...
    		
    		for(int i : c){                                  //This enhanced for loop just checks every value of the first array sees if the number is odd or even, excluding 0s
    			if (i != 0){//excluding 0s	                 //!! An enhances for loop is a condensed regular for loop designed for arrays and lists, it sets the variable
    				if (i % 2 == 0){//evens...               //before the colon to each value in the array(if the types match) and executes the block of code each time the
    					numsEven[count] = i;                 //value changes
    					count++;
    					evens++;
    				
    				}
    				else{//odds...
    					numsOdds[count] = i;
    					count++;
    					odds++;
    				}
    			}
    		}
    		
    		for(int i : numsOdds){//putting the odds into a string to be printed
    			if(i != 0)
    				numsOddsPrint += i + " ";
    		}
    		
    		numsOddsPrint += "]";
    		
    		for(int i : numsEven){//putting the evens into a string to be printed
    			if(i != 0)
    				numsEvenPrint += i + " ";
    		}
    		
    		numsEvenPrint += "]";
    		
    		System.out.printf("\nThere were %d odds :: %s",odds,numsOddsPrint);
    		System.out.printf("\nThere were %d evens :: %s",evens,numsEvenPrint);
    	}
    }
    Last edited by Dr_Death; 02-22-2012 at 11:56 AM. Reason: Removed annoying oversize font

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default Re: Need Help with Example

    Please change the colors in your post to something easier to read.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Need Help with Example

    This is for people in my Comp Sci class
    In which you're a student, right?

    any feed back is appreciated.
    Learn proper coding conventions. As written, your code is well-nigh unreadable.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Dr_Death is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need Help with Example

    Quote Originally Posted by DarrylBurke View Post
    In which you're a student, right?
    Yes, I am, sorry for the confusing sentence.

    Quote Originally Posted by DarrylBurke View Post
    Learn proper coding conventions. As written, your code is well-nigh unreadable.
    Code Conventions for the Java Programming Language: Contents
    Thanks for the link, I'd never seen that before.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •