50.1k views
1 vote
java Your program class should be called RomanNumerals Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Do not accept a number less than 1 or greater than 10. s

User Legend
by
5.3k points

2 Answers

2 votes

Answer:

//import the Scanner class to allow for user's input

import java.util.Scanner;

// Write the class header with the appropriate name

public class RomanNumerals {

// Write the main method - this is where execution begins

public static void main(String[] args) {

//Create an object of the Scanner class to allow user's to enter the number

Scanner input = new Scanner(System.in);

//Prompt the user to enter a number

System.out.println("Please enter a number within the range of 1 through 10");

//Receive the number from the user and store in an int variable

int num = input.nextInt();

//Begin the switch statement using the num

switch (num) {

//If the number is 1

//Print the corresponding Roman numeral -> I

//Then break out of the switch statement

case 1 :

System.out.println("I");

break;

//If the number is 2

//Print the corresponding Roman numeral -> II

//Then break out of the switch statement

case 2 :

System.out.println("II");

break;

//If the number is 3

//Print the corresponding Roman numeral -> III

//Then break out of the switch statement

case 3:

System.out.println("III");

break;

//If the number is 4

//Print the corresponding Roman numeral -> IV

//Then break out of the switch statement

case 4:

System.out.println("IV");

break;

//If the number is 5

//Print the corresponding Roman numeral -> V

//Then break out of the switch statement

case 5:

System.out.println("V");

break;

//If the number is 6

//Print the corresponding Roman numeral -> VI

//Then break out of the switch statement

case 6:

System.out.println("VI");

break;

//If the number is 7

//Print the corresponding Roman numeral -> VII

//Then break out of the switch statement

case 7:

System.out.println("VII");

break;

//If the number is 8

//Print the corresponding Roman numeral -> VIII

//Then break out of the switch statement

case 8:

System.out.println("VIII");

break;

//If the number is 9

//Print the corresponding Roman numeral -> IX

//Then break out of the switch statement

case 9:

System.out.println("IX");

break;

//If the number is 10

//Print the corresponding Roman numeral -> X

//Then break out of the switch statement

case 10:

System.out.println("X");

break;

//If the number is not within range [That is the default case]

//Print the corresponding error message.

//You might want to print the error message using

//System.err.println() rather than

//the regular System.out.println()

//Then break out of the switch statement

default:

System.err.println("Error: The number should not be less than 1 or greater than 10");

break;

} //End of switch statement

} // End of main method

} // End of class declaration

=============================================================

Sample Output 1:

>> Please enter a number within the range of 1 through 10

5

>> V

==============================================================

Sample Output 2:

>> Please enter a number within the range of 1 through 10

0

>> Error: The number should not be less than 1 or greater than 10

===============================================================

Step-by-step explanation:

The code has been written in Java and it contains comments explaining every section of the code, please go through the comments to get a better understanding of the code.

Actual codes are written in bold face to distinguish them from the comments.

User DAB
by
5.6k points
5 votes

Answer:

// Scanner class is imported to allow program

// receive input

import java.util.Scanner;

// RomanNumerals class is defined

public class RomanNumerals {

// main method that signify beginning of program execution

public static void main(String args[]) {

// Scanner object scan is created

// it receive input via keyboard

Scanner scan = new Scanner(System.in);

// Prompt is display asking the user to enter number

System.out.println("Enter your number: ");

// the user input is stored at numberOfOrder

int number = scan.nextInt();

// switch statement which takes number as argument

// the switch statement output the correct roman numeral

// depending on user input

switch(number){

case 1:

System.out.println("I");

break;

case 2:

System.out.println("II");

break;

case 3:

System.out.println("III");

break;

case 4:

System.out.println("IV");

break;

case 5:

System.out.println("V");

break;

case 6:

System.out.println("VI");

break;

case 7:

System.out.println("VII");

break;

case 8:

System.out.println("VIII");

break;

case 9:

System.out.println("IX");

break;

case 10:

System.out.println("X");

break;

// this part is executed if user input is not between 1 to 10

default:

System.out.println("Error. Number must be between 1 - 10.");

}

}

}

Step-by-step explanation:

The program is well commented. A sample image of program output is attached.

The switch statement takes the user input (number) as argument as it goes through each case block in the switch statement and match with the corresponding case to output the roman version of that number. If the number is greater 10 or less than 1; the default block is executed and it display an error message telling the user that number must be between 1 - 10.

java Your program class should be called RomanNumerals Write a program that asks the-example-1
java Your program class should be called RomanNumerals Write a program that asks the-example-2
User Gambisk
by
5.2k points