Design a RomanNumerals class that takes a number within 1 to 10 and display the Roman numeral version of that number (I, II, III, IV, V … X). If the number entered by the user is outside the range of 1-10, the program should display an error message. Code a driver class RomanNumeralsApp to test the class by asking user to enter a number, creating an object of RomanNumerals and calling its methods getNumber, setNumber, convertNum, and displayResult to perform the described task.

Respuesta :

Answer:

Explanation:

The following code is written in Java. It  creates a RomanNumberals class with all of the requested methods, including a convertNum method and displayResults method. The user is then asked to input a number within the main method. A RomanNumeral object is created, passed the userInput and all of it's methods are called for testing. A test output can be seen in the attached image below.

import java.util.Scanner;

class Brainly {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

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

       int number = in.nextInt();

       RomanNumerals romanNumeral1 = new RomanNumerals();

       romanNumeral1.setNumber(number);

       System.out.println("Test getNumber Method: " + romanNumeral1.getNumber());

       romanNumeral1.convertNumber();

       romanNumeral1.displayResult();

   }

}

class RomanNumerals {

   int number;

   String romanNumeral;

   public RomanNumerals() {

   }

   public int getNumber() {

       return number;

   }

   public void setNumber(int number) {

       this.number = number;

   }

   public void convertNumber() {

       switch (this.number) {

           case 1: romanNumeral = "I"; break;

           case 2: romanNumeral = "II"; break;

           case 3: romanNumeral = "III"; break;

           case 4: romanNumeral = "IV"; break;

           case 5: romanNumeral = "V"; break;

           case 6: romanNumeral = "VI"; break;

           case 7: romanNumeral = "VII"; break;

           case 8: romanNumeral = "VIII"; break;

           case 9: romanNumeral = "IX"; break;

           case 10: romanNumeral = "X"; break;

           default: System.out.println("Not a valid number"); break;

       }

   }

   public void displayResult() {

       System.out.println("Number " + this.number + " is represented as " + this.romanNumeral);

   }

}

Ver imagen sandlee09
ACCESS MORE