4.2: Roman Numeral Converter 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. Input Validation: Do not accept a number less than 1 or greater than 10. SAMPLE RUN #1: ./Test
System.out.print("Please input a number (1 - 10): ");
int num = inStream.nextInt();
if(num < 1 || num > 10){
System.out.println("The input must be between 1 - 10");
}else{
switch(num){
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;
}
}
}
}
Explanation:
The first part of this program (Line 6 - 8) is to get input number from user using Scanner object. Input validation has been done to check if the input number is in the acceptable range (Line 10). If not, an error message will be displayed (Line 11).
If the input number is in the range of 1 - 10, the number will go through a series of switch statements checking. If the number meet the condition in one of the switch cases, it will print the corresponding roman numeral specified in the case block (Line 13 - 44). For example, if input number is 9, the case 9 condition is met and "IX" will be printed.
An instruction format characterizes the diverse part of a guidance. The fundamental segments of an instruction are opcode and operands. Here are the various terms identified with guidance design: Instruction set size tells the absolute number of guidelines characterized in the processor. Opcode size is the quantity of bits involved by the opcode which is determined by taking log of guidance set size. Operand size is the quantity of bits involved by the operand. Guidance size is determined as total of bits involved by opcode and operands.