• Log in or sign up for an account here: https://www.sythe.org/login

Where can I find Java exercises?

kmjt

-.- The nocturnal life chose me -.-

Banned
Joined
Aug 21, 2009
Sythe Gold
449
Messages
14,450
Location
Canada
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.
 
arg-highlightedPosts="Array" args="" />
Where can I find Java exercises?
Know anything about arrays?
 
arg-highlightedPosts="Array" args="" />
Where can I find Java exercises?
Yeah somewhat. The very basics.
 
arg-highlightedPosts="Array" args="" />
Where can I find Java exercises?
Yeah somewhat. The very basics.

Then write some methods to find the max, min, average, sum, etc. Basic and pretty easy. Plus you get array experience.
 
arg-highlightedPosts="Array" args="" />
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?
 
arg-highlightedPosts="Array" args="" />
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).
 
arg-highlightedPosts="Array" args="" />
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);
		
	}
 
arg-highlightedPosts="Array" args="" />
Where can I find Java exercises?
Oops I already figured out the previous program forgot to edit.


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).

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


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);
		
	}


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

Thanks guys.
 
arg-highlightedPosts="Array" args="" />
Runescape Gamblingrunescape gambling
Back
Top