In: Computer Science
Java Language
The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array
{ 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 }
then your code should set ordinals to the array
{ "1st", "2nd", "3rd", "4th", "5th", "10th", "11th", "12th", "13th", "21st", "22nd", "973rd", "1000th" }.
Test Cases
Test case #1
Expected result: ordinals is {"21st","22nd","973rd","1000th"}
Test case #2
Expected result: ordinals is {"1st","2nd","3rd","4th","5th"}
Test case #3
Expected result: ordinals is { "52nd", "11th", "74th", "78th", "27th" }
Test case #4
Expected result: ordinals is { "81st", "78th", "911th", "173rd", "61st" }
Below is the solution. This prompts the user to enter the array length and then the array of numbers. Then the given numbers will be converted to the strings with ordinals.
If you find this solution helpful please upvote. In case of any doubts please comment. Thanks
import java.util.Scanner;
/**
* Class that converts the input numbers to ordinal strings
*/
public class Ordinals {
//converts the number to a string with ordinal
/**
*
* @param arr of numbers
* @return String array of ordinals
*/
private static String[] returnOrdinal(int[] arr)
{
String[] ordinalString = new
String[arr.length];
int num = 0;
for (int i = 0; i <
ordinalString.length; i++) {
num =
arr[i];
//if the element
is out of range
if(num > 1000
|| num <=0) {
//System.out.println("Program abort, Number is:
"+num + " out of range");
ordinalString[i] = "Number: " + num + " is out
of Range";
}
else
if(num%10==0 || num%10==9 || num%10==8 || num%10==7 || num%10==6 ||
num%10==5 || num%10==4)
{
ordinalString[i] = String.valueOf(num) +
"th";
}
//if the last digit of the number is 2
else
if(num%10==2)
{
if((num/10)%10==1) { //this is to check if
second digit is 1 like 112, 212, 312
ordinalString[i] =
String.valueOf(num) + "th";
}
else {
ordinalString[i] =
String.valueOf(num) + "nd";
}
}
//if the last
digit of the number is 3
else
if(num%10==3)
{
if((num/10)%10==1) { //this is to check if
second digit is 1 like 112, 212, 312
ordinalString[i] =
String.valueOf(num) + "th";
}
else {
ordinalString[i] =
String.valueOf(num) + "rd";
}
}
//if the last digit of the number is 1
else
{
if((num/10)%10==1) {
ordinalString[i] =
String.valueOf(num) + "th";
}
else {
ordinalString[i] =
String.valueOf(num) + "st";
}
}
}
return ordinalString;
}
public static void main (String[] args) {
Scanner s = new
Scanner(System.in);
System.out.println("Enter the
length of the numbers array");
int length = s.nextInt();
int[] array = new
int[length];
System.out.println("Please enter "
+ length + " numbers between 1 and 1000 (inclusive)");
for (int i = 0; i < length; i++)
{
array[i] =
s.nextInt();
}
String[] ord =
returnOrdinal(array);
for (int i = 0; i < ord.length;
i++) {
System.out.println(ord[i]);
}
}
}