Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this lab you will write a Java program that manipulates numbers. The program

ID: 3530039 • Letter: F

Question

For this lab you will write a Java program that manipulates numbers. The program will ask the user to enter a Roman numeral (or a Q to quit) and then convert that numeral into a base 10 integer and display the result. The program should loop until the user enters a "Q" when prompted.

For this assignment you should start with the following "skeleton" of Java code. Import this into your Eclipse workspace and fill in the methods as directed. For this assignment youWILLwant to add extra methods beyond the methods defined in the skeleton. Feel free to add any methods you find useful, but make sure that you add comments indicating what they do following the form of the rest of the comments in the code.

Project09.java

The following table shows the values of individual Roman numerals:


Here is a table of the digits from 1 to 9 in the Roman system:


Note that the Roman system in general is an additive system - the value for "VII" is determined by adding the value for the symbol "V" (5) to the value for the symbols "I" and "I" (1+1) to get a total of 7. The exceptional cases are where a symbol has a value that is smaller than the value of the symbol that comes after it - such as the case for "IV" (4) and "IX" (9). Here the smaller value issubtractedfrom the larger value instead.

This rule holds true for all place values. Here is a table of the values between 10 and 90, by tens:


And here is a table of the values between 100 and 900, by hundreds:


To build more complex values than these, Roman numerals use a system where, like the decimal system, there are "place values" for thousands, hundreds, tens and ones. For example, the decimal number 1986 has a 6 in the "ones" place, an 8 in the "tens" place, a 9 in the "hundreds" place and a 1 in the "thousands" place. To determine the value of the number you add 1x1000 + 9x100 + 8x10 + 6x1.

Place values in the Roman system operate a bit differently, because each place can have multiple symbols instead of just one. So to get the equivalent Roman numeral for a number we look at each place individually and concatenate the strings for each position together. For example, if we want to know the Roman numeral representation of 1986 we break it down into its component pieces:

1000 - M
+900 - CM
+ 80 - LXXX
+ 6 - VI

So as a Roman numeral, 1986 would be represented by the String "MCMLXXXVI".

To convert from a Roman numeral to a decimal number, your code will need to account for the fact that position matters for these symbols - a "I" that comes before a "V" means something different than a "I" that comes after a "V". One algorithm for performing this conversion is:

Here's an example of the numeral XCVI conversion to a decimal number by applying the algorithm above



This is a sample transcript of what your program should do. Items inboldare user input and should not be put on the screen by your program.

Enter a roman numeral (Q to quit):MCMLXXXIV
The numeral MCMLXXXIV is the decimal number 1984

Enter a roman numeral (Q to quit):MMMCMXCIX
The numeral MMMCMXCIX is the decimal number 3999

Enter a roman numeral (Q to quit):MMM
The numeral MMM is the decimal number 3000

Enter a roman numeral (Q to quit):Q
Goodbye!


Note that your output depends on the choices made by the user. You are not required to check to make sure that the user inputs a valid Roman numeral for this assignment, but you ARE required to make sure that the user does not enter an empty line.

Enter a roman numeral (Q to quit):
ERROR! You must enter a non-empty line!

Enter a roman numeral (Q to quit):XX
The numeral XX is the decimal number 20

Enter a roman numeral (Q to quit):Q
Goodbye!




Skeleton::

/*

* Project09.java

*

* A program that converts Roman numerals to decimal numbers.

* Used to practice breaking code up into methods.

*

* @author ENTER YOUR NAME HERE

*

*/

package osu.cse1223;

import java.util.Scanner;



public class Project09 {


public static void main(String[] args) {

// Fill in the body

}


// Given a Scanner as input, prompts the user to input a Roman numeral. Checks to make

// sure that the user does not enter an empty String. If the user does enter an

// empty String, report an error and ask for a new String until a non-empty String is

// provided. Return the String input by the user to the calling program.

private static String promptUserForNumeral(Scanner inScanner) {

// Fill in the body

}


// Given a String as input, converts the String to a number assuming that the String

// is a Roman numeral (following the rules in the writeup for Project 09). Returns the

// number to the calling program. NOTE: This method can possibly get long and complex.

// This about how you can break it up into two or three smaller methods to make it

// less complex.

private static int convertNumeralToNumber(String numeral) {

// Fill in the body

}


// Given a character that contains a single numeral, returns the integer value for

// that character. Use the tables in the writeup for Project 09 to fill in this method.

// For example, if the method is called with:

// convertCharacterToNumber('X')

// the method should return the value 10. And if the method is called with:

// convertCharacterToNumber('L')

// the method should return the value 50.

// Use this method with convertNumeralToNumber above to convert an entire Roman numeral

// into a number.

private static int convertCharacterToNumber(char numeral) {

// Fill in the body

}





}

I
1
V
5
X
10
L
50
C
100
D
500
M
1000

Explanation / Answer

/*

* Project08.java

*

* A program that converts decimal numbers to Roman numerals.

* Used to practice breaking code up into methods.

*

* @author ENTER YOUR NAME HERE

*

*/


import java.util.Scanner;



public class Project08 {


public static void main(String[] args) {

Scanner Yi = new Scanner(System.in);

String RomanNumeral;

int validnumber;

while(true){

validnumber= promptUserForNumber(Yi);

if(validnumber==0)

break;

RomanNumeral=convertNumberToNumeral(validnumber);

System.out.println(RomanNumeral);

}

Yi.close();

}


// Given a Scanner as input, prompts the user to input a number between 1 and 3999.

// Checks to make sure the number is within range, and provides an error message until

// the user provides a value within range. Returns the number input by the user to the

// calling program.

private static int promptUserForNumber(Scanner inScanner) {

boolean run= true;

int number=0;

//Scanner Yi= new Scanner(System.in); //you already have a Scanner to use

while (run){

System.out.println("Input a number between 1 and 3999 (or 0 to quit): ");

number = inScanner.nextInt();

if ((number>3999)||((number<1)&&(number!=0))){

System.out.println("You're not very good at listening to directions are you?");

}

else{run=false;}

}

return number;

}


// Given a number as input, converts the number to a String in Roman numeral format,

// following the rules in the writeup for Lab 09. Returns the String to the calling

// program. NOTE: This method can possibly get long and complex. Use the

// convertDigitToNumeral method below to break this up and make it a bit simpler to code.

private static String convertNumberToNumeral(int number) {

char;

char five= 'V';

char ten= 'X';

char fifty= 'L';

char;

char fivehundred= 'D';

char;

String str="";

//convertDigitToNumeral(number,one,five,ten,fifty,onehundred,fivehundred,onethousand);

//0-9

str+=convertDigitToNumeral(number,one,five,ten);

number/=10;

//10-99

str=convertDigitToNumeral(number,ten,fifty,onehundred)+str;

number/=10;

//100-999

str=convertDigitToNumeral(number,onehundred,fivehundred,onethousand)+str;

number/=10;

//1000 - 3000

while(number>0){

str=onethousand+str;

number--;

}

return str;

}


// Given a digit and the Roman numerals to use for the "one", "five" and "ten" positions,

// returns the appropriate Roman numeral for that digit. For example, if the number to

// convert is 49 we would call convertDigitToNumeral twice. The first call would be:

// convertDigitToNumeral(9, 'I','V','X')

// and would return a value of "IX". The second call would be:

// convertDigitToNumeral(4, 'X','L','C')

// and would return a value of "XL". Putting those together we would see that 49 would be the

// Roman numeral XLIX.

// Call this method from convertNumberToNumeral above to convert an entire number into a

// Roman numeral.

private static String convertDigitToNumeral(int digit, char one, char five, char ten) {

String str="";

if(digit>9||digit<0){

return "Invalid Digit";

}

if(digit==9){

str+=ten;

digit-=10;

}

else if(digit>=4){

str+=five;

digit-=5;

}

while(digit>0){//add to back if positive

str+=one;

digit--;

}

while(digit<0){//add to front if negative

str=one +str;

digit++;

}

return str;

}


}//end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote