Difference between revisions of "Print Out the Alphabet"

From CompSciWiki
Jump to: navigation, search
(Added quotations to student quotes)
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{1010PrAD|ProblemName=Print Out the Alphabet
 
{{1010PrAD|ProblemName=Print Out the Alphabet
  
|Problem=Print out the entire lower case letters of the aplhabet.
+
|Problem=Print out the entire lower case letters of the alphabet.
Use the following guidelines in coming up with a solution:
+
Use the following guidelines when coming up with a solution:
* [[Common Primitive Variables|char]] for printing out the each lowercase letter in the alphabet.
+
* [[Common Primitive Variables|char]] for printing out each of the lowercase letter's in the alphabet.
 
* Output using [[Input/Output using System.out.|System.out.println]]
 
* Output using [[Input/Output using System.out.|System.out.println]]
 
* [[Comments|Comment]] as you code.
 
* [[Comments|Comment]] as you code.
* To get the next letter use the [[Increment and Decrement Operators|increment operator]].
+
* To get the next letter in sequence use the [[Increment and Decrement Operators|increment operator]].
 +
* To repeat the incrementation, we need to use [[For Loops|For Loops]].
  
|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 okay because it is easy to add afterwords. 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 coding an assignment in order to add in all of my comments; I found that very stressful and not enjoyable. It is easier and more enjoyable to document code as you write it."<BR>
  
 
|Solution=
 
|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.
 
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.
<pre>
+
{{CodeBlock
 +
|Code=
 
//final indicates that this is a constant
 
//final indicates that this is a constant
 
//Uppercase also indicates that this is a constant
 
//Uppercase also indicates that this is a constant
 
final int NUMLETTERS = 26;
 
final int NUMLETTERS = 26;
</pre>
+
}}
Using this constant we can build our for loop.
+
<pre>
+
//our for loop will run from 0 to NUMLETTERS - 1
+
for(int i = 0; i < NUMLETTERS; i++)
+
{
+
    //code to be added here later
+
}
+
</pre>
+
For printing out the letters you are going to need a char variable.
+
<pre>
+
//the initial value of letter will be a since we are printing out the lower case alphabet.
+
char letter = 'a';
+
</pre>
+
Char's and Int's can be manipulated in the same way. For example to get a number + 1, we can just do number++.
+
<pre>
+
//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:
+
For printing out the letters you are going to need a char variable. A [[Common Primitive Variables|char]] type have an equivalent ASCII code integer. Like the example below, 'a' is equal to 97 in ASCII code.
number = 1
+
{{CodeBlock
</pre>
+
|Code=
We can do exactly the same thing with char's.
+
char letter = 'a'; //initialize the first letter
<pre>
+
}}
 +
 
 +
 
 +
Since [[Common Primitive Variables|char]] type have an equivalent ANSI code, incrementing the variable will also increase the ASCII code integer. In order to get the next letter, increment the variable "letter".
 +
{{CodeBlock
 +
|Code=
 
char letter = 'a';
 
char letter = 'a';
//get the next letter in the alphabet
+
letter++; //will increment the letter
letter++;
+
  
 
System.out.println("letter = " + letter)
 
System.out.println("letter = " + letter)
 +
}}
 +
{{OutputBlock
 +
|Code=
 +
letter = b
 +
}}
 +
 +
 +
Since you need to increment NUMLETTERS times, you need to use For Loop.
 +
{{CodeBlock
 +
|Code=
 +
for(int i = 0; i < NUMLETTERS; i++)
 +
{
 +
    //code to be added here later
 +
}
 +
}}
 +
  
Output:
 
letter = 'b'
 
</pre>
 
 
Putting all of this code together we can get our final for loop which will print out the alphabet.
 
Putting all of this code together we can get our final for loop which will print out the alphabet.
<pre>
+
{{CodeBlock
 +
|Code=
 
for(int i = 0; i < NUMLETTERS; i++)
 
for(int i = 0; i < NUMLETTERS; i++)
 
{
 
{
     //print out the current letter
+
     System.out.println(letter);//print out the current letter
     System.out.println(letter);
+
      
    //increase the letter to get the next
+
    letter++;//increase the letter to get the next
    letter++;
+
 
}
 
}
</pre>
+
}}
For the final complete code look at the Code area.
+
 
 +
For the complete final code you can look under the code heading.
  
|SolutionCode=
+
|SolutionCode=public class PrintOutTheAlphabet
<pre>
+
public class PrintOutTheAlphabet
+
 
{
 
{
 
     public static void main(String [ ] args)
 
     public static void main(String [ ] args)
Line 86: Line 86:
 
             System.out.println(letter);
 
             System.out.println(letter);
 
             //increase the letter to get the next
 
             //increase the letter to get the next
 +
            letter++;
 
         }
 
         }
 
     }
 
     }
 
}
 
}
</pre>
 
 
}}
 
}}

Latest revision as of 15:20, 8 December 2011

Back to the Program-A-Day homepage

Problem

Print out the entire lower case letters of the alphabet. Use the following guidelines when 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 okay because it is easy to add afterwords. 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 coding an assignment in order to add in all of my comments; I found that very stressful and not enjoyable. It is easier and more enjoyable to document code as you write it."

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; 


For printing out the letters you are going to need a char variable. A char type have an equivalent ASCII code integer. Like the example below, 'a' is equal to 97 in ASCII code.

 char letter = 'a'; //initialize the first letter 


Since char type have an equivalent ANSI code, incrementing the variable will also increase the ASCII code integer. In order to get the next letter, increment the variable "letter".

 char letter = 'a';
letter++; //will increment the letter

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


Since you need to increment NUMLETTERS times, you need to use For Loop.

 for(int i = 0; i < NUMLETTERS; i++)
{
    //code to be added here later
} 


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++)
{
    System.out.println(letter);//print out the current letter
    
    letter++;//increase the letter to get the next
} 

For the complete final code you can look under the code heading.

Code

Solution Code

Back to the Program-A-Day homepage