Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text,

Lab 4: Java Fundamentals, Part IV

In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text, page 184 (187 in Edition 5). Given one of the Roman numerals I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV as an input your program must determine and display the corresponding decimal digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.

Book = Tony Gaddis: Starting Out with Java, From Control Structures Through Objects, 7th Edition

Requirements

  1. The solution must use a nested if else construct built as follows:

  2. The top level if-else decision is made upon the first character of the Roman numeral, which is always I or V or X. That is, divide the given group of Roman numerals into three subgroups according to their first character either I or V or X.

  3. Both the if and else blocks contain deeply nested if-else structures where the input string is compared to the Roman numerals I, II, III, IV, IX in the first block and to V, VI, VII, VIII in the second block, and then to X, XI, XII, XIII, XIV, XV in the third block.

  • Input is solicited on a dialog window as shown on Figure 1.

  • Figure 1 = "Input a Roman Numeral between "I" and "XV"

  • Declare a String variable title to store the title text such as Conversion of Roman numerals. Declare a String variable task to store the input solicitation text, such as Enter a Roman numeral between I and XV . In the showInputDialog() method call that creates the window you must use the variables. For example, JOptionPane.showInputDialog(null, task, title, JOptionPane.QUESTION_MESSAGE). The input window must follow the template of Figure 1.

  • Declare a String variable roman to save the input string returned by the window.

  • Declare an int variable decimal to store the decimal digit equivalent of the roman numeral and

    initialize to 0.

  • Allow to use lowercase or uppercase, even mixed characters as input Roman numerals. The output Roman numerals must be in upper case. Use toUpperCase() for the conversions (see page 74).

  • The output must be displayed on a message dialog as shown on Figure 2 and the output window must follow the template, take note of the title line and the output arrangement. Use String.format() method for the output arrangement, such as The decimal value for the Roman numeral VIII is: ....... 8 ...... in two lines. (See the String.format in page 175-176).

  • Figure 2 = "The decimal value for the roman numeral "XIII" is: .....8....."

We assume that the input string is not empty and not null (each triggers a runtime error)

  • If the input string is none of the fifteen admissible Roman numerals, the message as shown on Figure 3 is displayed

  • Figure 3 = "input XIX is not an admissible Roman Numeral"

  • When you check the first character, use the == operator for equality an do not forget the character operator on the literal, put roman.charAt(0) == I into the topmost if header. (hints)

  • Use the equals method for string comparison (see pp 146-147 in your book). A nested if header shall look like (hints)

      if(roman.equals(VII)){ 
  • The output text (see Figure 2) contains the symbols. Use the escape sequence \ within the

    text. (hints)

  • Use do-while loop and the showConfirmDialog() method, such as int yesNo =JOptionPane.showConfirmDialog(null, "Any more Roman Numerals? yes or no", title,JOptionPane.YES_NO_OPTION); asking whether the process be continued as shown in Figure 4.

Figure 4 = "Any more Roman Numerals? Yes or No"

Finally, before the program ends, a message is displayed through the message box as shown in Figure 5.

Figure 5 = "End of Program!"

For JOptionPane class, you need to use the following two statements in a proper location. Do not excessively state System.exit(0); before the end of the main method. The import statement must be placed well top before the public class RomanNumerals{...}.

import javax.swing.JOptionPane; //needed for JOptionPane class.

and System.exit(0); //required JOptionPane class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Can animal studies provide an insight into human behaviour?

Answered: 1 week ago