Hello All,
I have a Java exam coming up and I want to get some practice in. I am only a beginner. The topics I have covered are as follows:
Variables and operators
IF statements / switch statements
For Loops / While Loops
Arrays
Methods
Just basic Java.
What I am asking is, if people will give me problems to solve and I will try to complete them to the best of my ability. For example someone gave me the following:
Create a program that loops from 1 to 12 and outputs the number. Each number should be on a blank line. Save it as prac4months1.java.
Now insert a switch statement inside the loop that will output the name of that month. For example the number 1 should produce the month name �January�. Your output should look like this:
1 January
2 February
3 March
...
etc
Rewrite your program using a �while� loop instead of a �for� loop. Save it as prac4months2.java.
This was my answer for part 1:
import java.io.*;
public class prac4months1 {
public static void main (String args [])
throws IOException {
String month="" ;
for (int i=1;i<=12;i++)
{
switch(i){
case 1 : month="January"; break;
case 2 : month="Febuary"; break;
case 3 : month="March"; break;
case 4 : month="April"; break;
case 5 : month="May"; break;
case 6 : month="June"; break;
case 7 : month="July"; break;
case 8 : month="August"; break;
case 9 : month="September"; break;
case 10 : month="October"; break;
case 11 : month="November"; break;
case 12 : month="December"; break;}
System.out.println(i+" "+month);
}
}
}
This is my answer to part 2:
import java.io.*;
public class prac4months2 {
public static void main (String args [])
throws IOException {
String month="" ;
int i = 1;
while (i<=12)
{
switch(i){
case 1 : month="January"; break;
case 2 : month="Febuary"; break;
case 3 : month="March"; break;
case 4 : month="April"; break;
case 5 : month="May"; break;
case 6 : month="June"; break;
case 7 : month="July"; break;
case 8 : month="August"; break;
case 9 : month="September"; break;
case 10 : month="October"; break;
case 11 : month="November"; break;
case 12 : month="December"; break;}
System.out.println(i+" "+month);
i= i+1;
}
}
}
If anybody willing to give me some problems to solve, I would be highly grateful.
Thanks!!