Question: Modify the program to print 1st, 2nd, 3rd, 4th and 5th rather than 1th, 2th, etc., without introducing redundant statements (Hint: Precede the if-else statement

Modify the program to print "1st", "2nd", "3rd", "4th" and "5th" rather than "1th", "2th", etc., without introducing redundant statements (Hint: Precede the "if-else" statement with a separate if-else statement that determines the appropriate ending based on the number).

import java.util.ArrayList; import java.util.Scanner;

public class MostPopularOS { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); ArrayList operatingSystems = new ArrayList(); int nthOS = 1; // User input, Nth most popular OS

// Source: Wikipedia.org, 2013 operatingSystems.add(new String("Windows 7")); operatingSystems.add(new String("Windows XP")); operatingSystems.add(new String("OS X")); operatingSystems.add(new String("Windows Vista")); operatingSystems.add(new String("Windows 8")); operatingSystems.add(new String("Linux")); operatingSystems.add(new String("Other"));

System.out.print("Enter N (1-7): "); nthOS = scnr.nextInt();

if ((nthOS >= 1) && (nthOS <= 7)) { System.out.print("The " + nthOS + "th most popular OS is "); System.out.println(operatingSystems.get(nthOS - 1)); }

return; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!