Method Activity 1. Create a method called printOdd1to99 which prints odd numbers
ID: 3803145 • Letter: M
Question
Method Activity
1. Create a method called printOdd1to99 which prints odd numbers 1 through 99.
2. Create a method called printEven which takes an integer parameter and prints even numbers up to and including that number. For example, if the parameter is 8, then the method will print out: 2 4 6 8. If the number is less than 2 print out “Number is too small”).
3. Create a method called printStringUCLC which takes a String parameter and prints out the String in uppercase and lowercase.
This needs to be done in Java oracle. Thank you!
Explanation / Answer
import java.io.*;
public class Test
{
// method called printOdd1to99 which prints odd numbers 1 through 99.
public static void printOdd1to99()
{
for(int i = 1;i <= 99; i++)
{
if(i%2 != 0)
System.out.print(i + " ");
}
System.out.println();
}
// method called printEven which takes an integer parameter and prints even numbers up to and including that number.
public static void printEven(int n)
{
if(n < 2)
System.out.println("Number is too small");
else
{
for(int i = 1;i <=n ;i++)
{
if(i%2 == 0)
System.out.print(i + " ");
}
System.out.println();
}
}
// method called printStringUCLC which takes a String parameter and prints out the String in uppercase and lowercase.
public static void printStringUCLC(String s)
{
s = s.toUpperCase();
System.out.println(" In uppercase: " + s);
s = s.toLowerCase();
System.out.println("In lowercase: " + s);
}
public static void main(String[] args)
{
System.out.println(" Odd numbers between 1 and 99: ");
printOdd1to99();
System.out.print(" Even numbers between 1 and 12: ");
printEven(12);
printStringUCLC("Horse");
}
}
/*
output:
Odd numbers between 1 and 99:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Even numbers between 1 and 12: 2 4 6 8 10 12
In uppercase: HORSE
In lowercase: horse
*/