Difference between revisions of "Print Out the Alphabet"

From CompSciWiki
Jump to: navigation, search
Line 8: Line 8:
 
* To get the next letter use the [[Increment and Decrement Operators|increment operator]].
 
* To get the next letter use the [[Increment and Decrement Operators|increment operator]].
  
|SideSectionTitle=While and For Loops
+
|SideSectionTitle=By Students...
  
 
|SideSection=
 
|SideSection=
[[Image:Wiki_loops03.jpg|center]]<BR>
+
Comments are very important in documenting how your program works. They make it easier for the marker to understand and grade your work and it also makes it easier for someone else to understand it. Commenting as you code is a very important skill that you should acquire. I usually don't comment as I code but I have been trying to change that. For small first year assignments not commenting as you code is ok because it is easy to add afterwards. But when you are working on later assignments and you end up having 600+ lines of code it gets quite a bit harder to add them at the end. I remember having to spend a couple of hours after I finished my assignment coding to add in all of my comments. I found that very stressful and not enjoying but if you document as you go it is easy and a lot more enjoyable.<BR>
  
 
|Solution=
 
|Solution=

Revision as of 10:26, 8 April 2010

Back to the Program-A-Day homepage

Problem

Print out the entire lower case letters of the aplhabet. Use the following guidelines in coming up with a solution:

 

By Students...

Comments are very important in documenting how your program works. They make it easier for the marker to understand and grade your work and it also makes it easier for someone else to understand it. Commenting as you code is a very important skill that you should acquire. I usually don't comment as I code but I have been trying to change that. For small first year assignments not commenting as you code is ok because it is easy to add afterwards. But when you are working on later assignments and you end up having 600+ lines of code it gets quite a bit harder to add them at the end. I remember having to spend a couple of hours after I finished my assignment coding to add in all of my comments. I found that very stressful and not enjoying but if you document as you go it is easy and a lot more enjoyable.

Solution

When printing out the alphabet you are going to need to know the number of letters to stop the for loop at the right time.

//final indicates that this is a constant
//Uppercase also indicates that this is a constant
final int NUMLETTERS = 26;

Using this constant we can build our for loop.

//our for loop will run from 0 to NUMLETTERS - 1
for(int i = 0; i < NUMLETTERS; i++)
{
    //code to be added here later
}

For printing out the letters you are going to need a char variable.

//the initial value of letter will be a since we are printing out the lower case alphabet.
char letter = 'a';

Char's and Int's can be manipulated in the same way. For example to get a number + 1, we can just do number++.

//our number variable
int number = 0;
//we are incrementing our number which is exactly the same as number = number + 1
number++;

System.out.println("number = " + number);

Output:
number = 1

We can do exactly the same thing with char's.

char letter = 'a';
//get the next letter in the alphabet
letter++;

System.out.println("letter = " + letter)

Output:
letter = 'b'

Putting all of this code together we can get our final for loop which will print out the alphabet.

for(int i = 0; i < NUMLETTERS; i++)
{
    //print out the current letter
    System.out.println(letter);
    //increase the letter to get the next
    letter++;
}

For the final complete code look at the Code area.

Code

Solution Code

Back to the Program-A-Day homepage