110 likes | 207 Views
More Programming. Peter Newman. So, in theory you should know what a Class and what an Object is (Assuming a combination of the Carlton and my explanation didn’t confuse you). Quick Recap. Just to make sure... What is a Class? What is an Object?
E N D
More Programming Peter Newman
So, in theory you should know what a Class and what an Object is (Assuming a combination of the Carlton and my explanation didn’t confuse you). Quick Recap • Just to make sure... • What is a Class? • What is an Object? • Can you briefly explain the relationship between them?
Today, we will be going over some of the more clever constructs in programming (specifically with Java). Today Construct? Basically, when I say construct, I mean a statement in java that is followed by { } – So for example, conditional statements, loops etc.
Firstly, what kind of looping constructs are available in Java (or similar language)? • For Loop • While Loop • Do-While Loop Repetition & Looping Now that we know what loops exist, could you give reasons why you would use a while loop rather than an if loop? A for loop allows you to iterate a specified number of times whilst retaining a count of the iterations whereas a while loop allows you to continue looping until a condition has been met. So, you should use a for loop when you know how many times you need to iterate, and use a while loop for iterating until something has been achieved.
Does anybody know of another form of iteration in programming languages? The answer lies in recursion. Another answer What is recursion? Recursion is the process of a method (or any construct in some languages) calling itself. A method that does this is known as Reentrant. public void myRecursiveMethod( inti ){ System.out.println(“Hello”); i++; if ( i < 10 ){ myRecursiveMethod( i ); } } This example briefly shows a recursive method. As you can see, this method prints “Hello”, increments the number passed to it, and calls itself again with the new value.
So how does this work? Well, you don’t need to know the exact mechanism right now but it’s probably best to illustrate. How it works Only when the last embedded method has finished executing (which might happen through a condition), all the methods start to cascade to completion (from the last one in the chain). myRecursiveMethod myRecursiveMethod myRecursiveMethod myRecursiveMethod Once the myRecursiveMethod is called from within itself, the current state of that method is saved to the Stack. It’s execution is paused and the new method call is performed. The old method has still not finished.
It seems pretty pointless... Because you’ve been blessed with abundant memory you might think that. Recursion was used heavily back in the early days of computing when adding an extra integer variable was considered too costly (such as the variable counter found in the while loop). However, it is important not to underestimate the power of recursion – some algorithms seem natural to use recursion and it often looks tidier than having for statements within your code. What is the point? Do we really need to use it? Within the work we set you, we won’t expect you to use recursion, however having knowledge of it gives you a new tool to use when developing a new algorithm.
Does anyone know what an Array is? An array is a block of contiguous memory locations used to store variable information. They tend to be of the same type ( such as int or String ) but not necessarily . Lets talk Arrays... What is the purpose of an array? The purpose is generally to store a collection of variables that tend to be related in some manner. This will reveal itself as a good idea as time goes on...
What does an array look like? Well, below is an illustration... Lets see Arrays... 4 3 1 9 10 2 3 7 1 29 2 30 1 0 1 2 3 4 5 6 7 8 9 10 11 12 What does all that mean? Each block within the array is the same size (thus why they tend to be of the same type). Each block is indexed with a number – the indexes start at 0 (for traditional computing reasons). Within the example above, I have stored numbers – so, in position ‘6’, I have stored the number ‘3’. Make sense?
That’s all well and good, but how do I use them? Not that much extra effort really... Declare it... int[] myIntArray = new int[ 10 ]; Fill it... myIntArray[0] = 10; myIntArray[1] = 7; myIntArray[...] = 9; etc Use it... System.out.println( myIntArray[1] ); //prints out ‘7’ Lets use Arrays...
Question Time! That’s all I haveTime to ask questions....Followed by either sleep or Caffeine...