Question

Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to...

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? \nyes 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Program to convert Roman Numerals to Numbers
import java.util.*;
  
public class RomanToNumber
{
// This function returns value of a Roman symbol
int value(char r)
{
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
  
// Finds decimal value of a given romal numeral
int romanToDecimal(String str)
{
// Initialize result
int res = 0;
  
for (int i=0; i<str.length(); i++)
{
// Getting value of symbol s[i]
int s1 = value(str.charAt(i));
  
// Getting value of symbol s[i+1]
if (i+1 <str.length())
{
int s2 = value(str.charAt(i+1));
  
// Comparing both values
if (s1 >= s2)
{
// Value of current symbol is greater
// or equalto the next symbol
res = res + s1;
}
else
{
res = res + s2 - s1;
i++; // Value of current symbol is
// less than the next symbol
}
}
else
{
res = res + s1;
i++;
}
}
  
return res;
}
  
// Driver method
public static void main(String args[])
{
RomanToNumber ob = new RomanToNumber();
  
// Considering inputs given are valid
String str = "MCMIV";
System.out.println("Integer form of Roman Numeral" + " is " + ob.romanToDecimal(str));
}
}

Add a comment
Know the answer?
Add Answer to:
Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a...

    Can someone code this asap? Use any language that you want. 2. Ancestral Names Given a list of strings comprised of a name and a Roman numeral, sort the list first by name, then by decimal value of the Roman numeral. In Roman numerals, a value is not repeated more than three times. At that point, a smaller value precedes a larger value to indicate subtraction. For example, the letter I represents the number 1, and Vrepresents 5. Reason through...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated...

    Write a Python program (question2.py) that reads from a file called “input.txt” numbers in [1,39] separated in by commas. The numbers are in [1-99]. The program will then convert each number to a possible Roman Numeral equivalent, and print it on the screen. Remember, I is 1, V is 5, X is 10 For example, if the input is: 23, 11 the output is: XXIII, XI. ROMAN NUMERALS CHART 1 TO 100 69 LXIX 11 2 11 3 III 4...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • For this week's lab, you will use two of the classes in the Java Collection Framework:...

    For this week's lab, you will use two of the classes in the Java Collection Framework: HashSet and TreeSet. You will use these classes to implement a spell checker. Set Methods For this lab, you will need to use some of the methods that are defined in the Set interface. Recall that if set is a Set, then the following methods are defined: set.size() -- Returns the number of items in the set. set.add(item) -- Adds the item to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT