Question
Needed in a basic java format. no switch cases please. thanks!!!
(4 pts) Problem 1 An ordinal number can be thought of as the adjective forin of a ununber. Examples of ordinal numbers are
Witlhin the sane Ordinal Numbers. java class as the previous probleu. write a nain uetl the user to specify a range of intege
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.List;
import java.util.NoSuchElementException;


class OrdinalNumbers {

    public String toOrdinal(int x) {
        if (x < 0) {
            return "invalid";
        }
        String pref[] = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"};
        if (x >= 10 && x <= 20) {
            return x + pref[0];
        }
        return x + pref[x % 10];
    }

    public void toOrdinal(int left, int right) {
        for (int i = left; i <= right; i++) {
            System.out.println(toOrdinal(i));
        }
    }

}

class Main {
    public static void main(String[] args) {
        OrdinalNumbers o = new OrdinalNumbers();
        o.toOrdinal(16,28);
    }
}

OUTPUT :

Run Main C:\Program Files\Java\jdk1.8.0-131\bin\java 16th 17th 18th 19th to E 20th 22nd 23rd 24th 25th oit 27th 28th Proce

Add a comment
Know the answer?
Add Answer to:
Needed in a basic java format. no switch cases please. thanks!!! (4 pts) Problem 1 An...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from...

    In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT