How To Write Conversion Method From Roman Numerals To Arabic
Nov 8, 2014
I am a student taking a Java programming class. My assignment is to write a program that converts a roman numeral input as entered by a user and converting it to it's integer value (arabic number). These are the methods that I must have in it:
1. Write a method that takes input from the user and passes it to a conversion method.
2. Write a method that yields the numeric value of each of the letters (conversion method).
3. Write a method that outputs the number the user entered and the converted number.
4. Write a main method to test the 3 methods.
I have written the first method, at least I think. Here is what I did there:
public static String romanInput(String number) {
Scanner numberInput = new Scanner (System.in);
System.out.print("Enter a roman numeral: ");
String userInput = numberInput.next();
return userInput;
}
I returned the userInput and I think that is passing it to the conversion method? Now I am working on this conversion method and to be honest I don't know where to begin. I am told how to convert a string in my assignment by the professor. I am told:
- Look at the first two characters. If the first has a larger value than the second, then simply convert the first.
- Call the conversion method again for the substring starting with the second character.
-Add both values. If the first one has a smaller value than the second, compute the difference and add to it the conversion of the tail.
I am also told to use a single-dimensional array. But, I don't know what I am to use a single dimensional array for? So this is what I wrote so far for this method:
public static int numberConversion(int number) {
int romanConv = 0;
char[] romanNum = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
char[] arabicNum = {1, 5, 10, 50, 100, 500, 1000};
romanNum = arabicNum;
}
I have written a character array for the roman numerals, and then one for arabic numerals, then I set them equal to each other. I also declared an integer variable set to 0 because I think that is what I will be returning at the end of the method. Now I don't know where to start for the conversion algorithm here. I know this is what I have to do, but I don't know how to do it:
1. Add the numbers together if they are in decreasing value or are equal in value. For example: VI is read as 5 + 1 = 6 XVI is read as 10 + 5 + 1 = 16 XXXVIII is 10 + 10 + 10 + 5 + 1 + 1 + 1 = 38
2. Use subtraction if a number is less than the number that follows it. For example, I is less than V, so when I is in front of V, you subtract its value.
3. For example: IV is 5 1 = 4 IX is 10 1 = 9 XL is 50 10 = 40 MCM is 1,000 + (1,000 - 100) = 1,900
I can't use hashtables or enums because I haven't learned about that yet. I have a feeling I need to use a for loop. I know I haven't done any of the actual programming work but I don't know how to begin writing this conversion method.
View Replies
ADVERTISEMENT
Aug 20, 2014
I don't need the code , I just want to try on my own ....
View Replies
View Related
Feb 26, 2014
Writing an program to add any 2 given Roman numerals together and return the results.
[I don't want to convert Roman numbers to decimal numbers & then add together, then convert in to Roman numerials ]
Is there any direct way to add roman numerials?
For example: I + I = II
III + I = IV
View Replies
View Related
Oct 5, 2014
I'm trying to do a program to convert roman numbers to arabic numbers. My problem is that I don't have the right to use Methods and Arrays. Only loops and if/else.
View Replies
View Related
Mar 25, 2015
Im working on a roman numeral to arabic converter and all I had to do was fill out the conversion method romanToDecimal. But for some reason no matter what number I enter It always says my number is equal to one.
//Quiz 1 EC
import java.util.*;
class Roman {
private String romanNum;
private int decimalNum;
public Roman(){
romanNum = "I";
decimalNum = 1;
[code]....
View Replies
View Related
Sep 12, 2014
I got part of this converter built and running fine but now it's getting weird. Specifically I am getting to the part where I have to count above ten which means things like attaching Vs to Is and Xs and suchlike.
I know I have to use an .append someplace, I am just not certain how to go about it. I've been playing with it some, but this is honestly the biggest thing I've built so far that actually *ran* so I'm afraid to poke too many holes in it... where do I start? It's not like I can invent another return string and concatenate them. R is what I've got.
package hello;
public class RomansatHomans {
public static void main(String[] args) {
// input fed on this line loops through toRo and comes back as a String
// R
String R = toRo(23);
// int D = froRo("x");
[Code] ......
View Replies
View Related
Jul 29, 2014
I have made a program that is supposed to convert roman numerals to decimals. It is NOT supposed to convert decimals to roman numerals. The only problem it seems there is with the program is that it only adds and does NOT subtract. This causes some roman numeral conversions to be flawed. What can I try to fix this. Keep in mind that the reason some methods exist is because the directions for the code say that I have to make it this way.
Instructions:
Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class called Roman. An object of type Roman should be able to do the following:
1) Store the number as a Roman numeral.
2) Convert and store the number into decimal.
3) Print the number as a Roman numeral or decimal number as requested by the user. (Be sure to over-ride the toString function).
4) Your class must contain the method romanToDecimal to convert a Roman numeral into its equivalent decimal number.
5) Test your program using the following Roman numerals: MCXIV, CCCLIX, and MDCLXVI.
This is what I have so far. It compiles and there are no errors, but it does not subtract I from X, or I from V and so on. For example,CMXLVII should be 947, but the program outputs 1067.
import javax.swing.*;
import java.util.*;
public class Roman{
private String roman;
private int decimal;
public int romanToDecimal(String s)
[Code] .....
View Replies
View Related
Feb 25, 2015
convert roman to ordinary conversion without using any predefine methods,where we are giving input thru command prompt
View Replies
View Related
Oct 14, 2014
This is my program
import java.util.Scanner;
import java.text.DecimalFormat;
public class Lab6 {
public static void main(String[] args) {
int F;
double C = 0;
[Code] ......
output
0 1 2 3 4 5 6 7 8 9
0 degree Fahrenheit equal to :-00.00 degree Celsius
1 degree Fahrenheit equal to :-00.00 degree Celsius
2 degree Fahrenheit equal to :-00.00 degree Celsius
3 degree Fahrenheit equal to :-00.00 degree Celsius
4 degree Fahrenheit equal to :-00.00 degree Celsius
5 degree Fahrenheit equal to :-00.00 degree Celsius
6 degree Fahrenheit equal to :-00.00 degree Celsius
7 degree Fahrenheit equal to :-00.00 degree Celsius
8 degree Fahrenheit equal to :-00.00 degree Celsius
9 degree Fahrenheit equal to :-00.00 degree Celsius
I don't know why the program is not calculating the C degree ?
View Replies
View Related
Mar 7, 2014
My OrderCalculator class is throwing an exception. The input from my HourPanel that is supposed to add to my OrderCalc is cooperating.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class OrderCalculator extends JFrame
{
private JobPanel jobs;
private HourPanel rate;
[Code] .....
View Replies
View Related
May 19, 2014
which when user enter a number then will display how many * in a line .Produce a line: ***** But my code only show 1 * there... what wrong with it ?
import java.util.Scanner;
public class display {
public static void main(String args[]) {
int i,n = 0 ;
[Code] ...
View Replies
View Related
Mar 29, 2015
I have a question related to trees in Java. The following code is given:
public class BinarySearchTree {
private Node root;
public BinarySearchTree() {
this.root = null;
}
public BinarySearchTree(Node root) {
[Code] ....
The inorder-method is the one that really imports. I have to go through a tree using the Inorder-method. I do understand how this works, however I don't understand how the code fits to the inorder process. I understand the code up to the point where it reaches the most left "root", but then I do not know how to go further. How do I reach the root above then?
View Replies
View Related
May 7, 2014
Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:
public static void main(String[] args)
{
printChars(‘A’,’z’,10);
printChars(‘0’,’9’,5);
}
View Replies
View Related
Apr 11, 2014
So here is a method i wrote for the first part for initializing an array:The question was, "write a method which initializes a 2D target array (assuming all other classes have been imported and given), and all targets should be stationary."and this was my method i wrote:
public void initializeTargetArray(Target[][] targets) {
int row = targets.length();
int col = targets[0].length;
for(int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
targets[i][j] = new Target(Target.STATIONARY, false);
}
Now my question how do I initialize an array similar to the method above for a different question?
Quote
Part b
Write a method which initializes an array (You can assume that the 2D array itself (but not the Target
objects inside) is already initialized), where the game level stage is now set:
Game level 1 - all targets are stationary,
Game level 2 - For even numbered rows, targets at even numbered columns are stationary, targets at odd
numbered columns are movable. For odd numbered rows, targets at odd numbered columns are
stationary, targets at even numbered columns are movable.
Game Level 3 - All targets are movable.
public void initializeTargetArray(Target[][] targets, int gameLevel) {
//*** Finish this method. ***//
View Replies
View Related
Oct 25, 2014
I must write a method that accepts a string and returns an int. The method should sum the unicode values of each character, so the string "ABC" should return 198. Also the only string class methods I'm aloud to use are length, charAt, and substring. I just don't know how the get the Unicode value.
View Replies
View Related
May 29, 2014
Write an instance method, contains, that has one explicit parameter of type Rectangle. The method should return true if every point of the rectangle determined by the explicit parameter is on or within the rectangle determined by the implicit parameter. It should return false otherwise.
This is what i did so far?
public boolean contains(Rectangle other) {
Rectangle intersect = Rectangle.intersection(this, other);
if ((intersect.left == this.left) && (intersect.bottom == this.bottom) && (intersect.width == this.width)
&& (intersect.height == this.height)) {
return true;
} else {
return false;
}
}
View Replies
View Related
Mar 23, 2015
How do i write a method in java that will add a <b> or <em> tag to a specific word regardless of case or punctuation for example for "run forest RUN!" adding bold to run would be
<b>run<b> & <b>RUN!<b>
public void addTag(String word, String tag) {
View Replies
View Related
Dec 3, 2014
I have understood my programming class up to this point and now I have been given a lab that I can't figure out for the life of me. Here what I have to do: Write a program that will call a method (called f) to calculate the following function" f(x)=(x^2)-16...this is what the output should be:
x f(x)
1 -15
2 -12
3 -7
4 0
5 9
6 20
7 33
8 48
9 65
10 84
I guess it also has to print this output in a table even though my professor never mentioned it. Usually
public class Lab12 {
public static int f(int x)
{
return x*x-16;
[code]....
View Replies
View Related
Apr 22, 2015
I am supposed to write a static method that simulates a flip of a weighted coin by returning either heads or tails each time it is called. The coin is twice as likely to turn up heads as tails. My idea was to of course use a random class
Random flip = new random
and somehow initialize it so 3 numbers are called and create if statements so that when 0 and 1 are called heads returns and when 3 is called tails is.How do I put all this together?
View Replies
View Related
Sep 1, 2014
I wrote this program to prompt user to enter his choice to do a i/o operation in a file. It shows error. How to clear the error. My code is:
import java.io.*;
import java.util.*;
class Files {
public static void main(String args[]) {
String n;
[Code] ....
error:
E:java>javac Files.java
Files.java:26: error: no suitable method found for write(String)
fos.write(n);
[Code] ....
View Replies
View Related
Oct 27, 2014
So I am calling this method several times and trying to write multiple records to a file. Problem is that every time I call the method it overwrites the file from before and doesn't add it.
public void fileWriterMethod() throws IOException{
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
raf.writeInt(id);
raf.writeInt(existingMileage);
raf.writeInt(gasCost);
raf.writeInt(ndays);
raf.writeInt(rate);
raf.writeInt(totalCharge);
raf.writeInt(discount);
raf.writeInt(tax);
raf.writeInt(netCharge);
raf.writeInt(returnMileage);
raf.writeBytes(carName + "
");
//Closing the stream
raf.close();
}
View Replies
View Related
Apr 22, 2014
i have to "Write a method called addToOverThirty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 30 in the array.
Add a call to the addToOverThirty method, then display a message telling what the output is followed by the results For example:The nums3 array after adding 1 to all numbers greater than 30 is10 6 15 and so on (check with the values you assigned to nums3)"its pointless because we were told not to make the array have a number over 30.
import java.lang.*;
import java.util.*;
public class LastProject
public static void main(String[] args) {
int nums1[] = new int[15];
int nums2[] = new int[15];
int nums3[] = {5,2,15,8,26,22,1,29,4,23,30,11,19,6,24};
[code]....
View Replies
View Related
Feb 6, 2014
I am trying to write a program that asks the user to enter a number from 1 through 10 and then the program will display the roman numeral for that number.
I am also adding a error message in which i haven't yet because im still trying to figure out how to the program will do the roman numeral.'
I have used the if and else if. but when i input a number it just repeats the number back to me.
The program cimpiles but it doesn't do what i want. here is what i have so far. how can i get the program to display the roman numeral after the number is entered.
import javax.swing.JOptionPane;
public class Romannumeral
{
public static void main(String[] args)
{
double number;
[Code] .....
View Replies
View Related
Feb 7, 2015
Write a program called RomanNumeralHelper that allows a user to enter a roman numeral and then outputs the integer number value. Use a Scanner to accept command line input from the user and continually ask for a roman numeral until the user enters Q (or q) to stop. Your solution should NOT use a switch statement.
Here is sample input / output:
Enter a roman numeral [Q | q to quit]: III
>> 3
Enter a roman numeral [Q | q to quit]: IV
>> 4
Enter a roman numeral [Q | q to quit]: V
>> 5
Enter a roman numeral [Q | q to quit]: Q
Good Bye!
This is what I have so far in my code, but I cant get what the user inputs when I want it to output the number.
import java.util.Scanner;
public class RomanNumber4
{
public static void main(String[] args) {
// obtain input from command window
Scanner input = new Scanner(System.in);
[Code] ....
View Replies
View Related
Aug 20, 2014
java program to convert integer to Roman numbers and vice versa?
View Replies
View Related
Feb 2, 2015
I have requirement to convert the HTML to JSP.
purpose: From the mobile i will get the HTML content and need the change content so need to convert html to JSP.
View Replies
View Related