Question

In: Computer Science

Java Language The array s of ints contain integers each of which is between 1 and...

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" }

Solutions

Expert Solution

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]);
       }
   }
}


Related Solutions

The array s of ints contain integers each of which is between 1 and 1000 (inclusive)....
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",...
This is JAVA Return the centered average of an array of ints, which we'll say is...
This is JAVA Return the centered average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array has a length of 3 or more. 1 public int centeredAverage(int[] nums) 2 { 3...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as...
JAVA LANGUAGE 1. What is the value of creditScores.length, if you have declared an array as follows: int[] creditScores = {670, 720, 815};? a. 0 b. 1 c. 2 d. 3 2. Which of the following correctly assigns the value 100 to each of the array element? Assume the array is declared as int [] num = new int[4] a. for(x=0;x<3;++x) num[x]=100; c. for(x=1;x<4;++x) num[x]=100 ; 3. Consider the following code fragment int[][] rectangle = new int[4][2]; What is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT