Results 1 to 4 of 4
Thread: Need Help with Example
- 02-22-2012, 02:03 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0
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
- 02-22-2012, 02:29 AM #2
Re: Need Help with Example
Please change the colors in your post to something easier to read.
- 02-22-2012, 10:05 AM #3
Re: Need Help with Example
In which you're a student, right?This is for people in my Comp Sci class
Learn proper coding conventions. As written, your code is well-nigh unreadable.any feed back is appreciated.
Code Conventions for the Java Programming Language: Contents
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-22-2012, 11:51 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 2
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks