Where can I find Java exercises?

Discussion in 'Programming General' started by kmjt, Feb 20, 2013.

Where can I find Java exercises?
  1. Unread #1 - Feb 20, 2013 at 4:35 PM
  2. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Where can I find Java exercises?

    What sites are good for java exercises? I would say I am pretty much a beginner in the language. Specifically looking to get the hang of methods.
     
  3. Unread #2 - Feb 20, 2013 at 5:03 PM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Where can I find Java exercises?

    Know anything about arrays?
     
  5. Unread #3 - Feb 20, 2013 at 6:00 PM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Where can I find Java exercises?

    Yeah somewhat. The very basics.
     
  7. Unread #4 - Feb 20, 2013 at 6:51 PM
  8. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Where can I find Java exercises?

    Then write some methods to find the max, min, average, sum, etc. Basic and pretty easy. Plus you get array experience.
     
  9. Unread #5 - Feb 21, 2013 at 11:24 AM
  10. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Where can I find Java exercises?

    I attempted it.


    Code:
    import java.util.Scanner;
    public class Calculations 
    {
    	
    	static Scanner input = new Scanner(System.in);
    	public static void main(String[] args) 
    	{
    	
    		System.out.println("Enter ten integers: ");
    		int tenIntArray[] = new int[10];	
    		for (int counter = 0, index = 0; counter<10; counter++, index++)
    		{
    			
    			tenIntArray[index] = input.nextInt();
    			
    		}
    		
    		getSum(tenIntArray);
    		getAverage(tenIntArray);
    		getMin(tenIntArray);
    		getMax(tenIntArray);
    		
    	}
    	
    	
    	//getSum method
    	static void getSum(int array[])
    	{
    		
    		int sum = 0;
    		
    		for (int counter=0; counter<array.length; counter++)
    		{
    			sum += array[counter];
    		}
    		
    		System.out.println("\nThe sum of your integers is: " + sum);
    		
    	}
    	
    	
    	
    	//getAverage method
    	static void getAverage(int array[])
    	{
    		int sum = 0;
    		int counter;
    		
    		for (counter=0; counter<array.length; counter++)
    		{
    			sum+=array[counter];
    		}
    		
    		int average = sum/counter;
    		
    		System.out.println("The average of your integers is: " + average);
    	}
    	
    	
    	//getMin method
    	static void getMin(int array[])
    	{
    		int min = array[0];
    		
    		
    		for (int counter=0; counter<array.length; counter++)
    		{
    			if (array[counter]<min)
    			{
    				array[counter]=min;
    			}
    			
    		}
    		
    		System.out.println("The smallest number in your integers is: " + min);
    	}
    	
    	
    	
    	//getMax method
    	static void getMax(int array[])
    	{
    		int max = array[0];
    		
    		for (int counter=0; counter<array.length; counter++)
    		{
    			if (array[counter]>max)
    			{
    				array[counter]=max;
    			}
    		}
    		System.out.println("The largest number in your integers is: " + max);
    	}
    	
    }

    Why don't my getMin and getMax method work? When I run the program they give answers that are not correct.

    Also, did I even have to manually make these methods? Or are there shortcuts like .length ()for example.

    array[].getMax() or something?
     
  11. Unread #6 - Feb 21, 2013 at 3:13 PM
  12. RuneScapeJJ
    Joined:
    Aug 14, 2011
    Posts:
    1,522
    Referrals:
    1
    Sythe Gold:
    0
    Potamus

    RuneScapeJJ Guru
    $25 USD Donor New

    Where can I find Java exercises?

    In the getMax() method you are storing the value in the array[counter]
    array[counter]=max;
    Needs to be max = array[counter]

    Same for getMin().

    Also you put them as
    int array[]
    I suggest following the standards and going with
    int[] numbers

    Could also do final int... numbers which is useful at certain times, you can input one or more ints. So you could put in one int but also 100.

    Also you are already getting the first number here:
    int min = array[0];
    So in your loop you can start at 1 instead of 0.


    Also for tutorials I followed these: http://thenewboston.org/list.php?cat=31 until 30 or something then I started to write my first Java based RuneScape scripts because I had experience with programming for Simba already (Pascal).
     
  13. Unread #7 - Feb 21, 2013 at 4:49 PM
  14. Clashfan
    Joined:
    Sep 2, 2005
    Posts:
    3,973
    Referrals:
    1
    Sythe Gold:
    1
    Two Factor Authentication User

    Clashfan Swim To The Moon
    Highly Respected Retired Administrator

    Where can I find Java exercises?

    Try using a for-each loop to iterate through arrays / collections. It's much nicer looking.
    Code:
    	static void getSum(int array[])
    	{
    		
    		int sum = 0;
    		
    		for (int i : array)
    		{
    			sum += i;
    		}
    		
    		System.out.println("\nThe sum of your integers is: " + sum);
    		
    	} 
     
  15. Unread #8 - Feb 21, 2013 at 5:12 PM
  16. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Where can I find Java exercises?

    Oops I already figured out the previous program forgot to edit.


    Yeah i've actually been watching his videos, i'm at 35 I think.



    Haha I literally learned the enhanced for loop 30 minutes after I figured out this program >.<

    Thanks guys.
     
< The perfect white hat? | Cs:s Cheat 64bit >

Users viewing this thread
1 guest


 
 
Adblock breaks this site