1 Replies - 7411 Views - Last Post: 16 December 2006 - 04:38 PM Rate Topic: -----

#1 Felisha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-December 06

Convert Roman Numberal from C++ to JAVA code

Post icon  Posted 16 December 2006 - 01:09 PM

Can someone please help me convert this over to Java.. I would greatly appreciate it.. Thanks a bunches
#include <iostream>
using namespace std;

class romanType{
	romanType(char&);  // ** make constructor
	int convert();
	void print();
	void get();

	int M, D, C, L, X, V, I;
	char romanNumeral;
romanType::romanType(char &ch){
	M = 1000;
	D = 500;
	C = 100;
	L = 50;
	X = 10;
	V = 5;
	I = 1;
	cout << ch << endl;
	romanNumeral=ch;	 // ** set up romanNumeral
int romanType::convert(){
	if (romanNumeral == 'M'){
		return 1000;				   // ** return value
	}else if(romanNumeral == 'D'){
		return  500;
	}else if(romanNumeral == 'C'){
		return  100;
	}else if(romanNumeral == 'L'){
		return  50;
	}else if(romanNumeral == 'X'){
		return  10;
	}else if(romanNumeral == 'V'){
		return  5;
	}else if(romanNumeral == 'I'){
		return  1;
	return 0;   // ** error - 
void romanType::print(){
	cout << romanNumeral << endl;
void romanType::get(){
int main(){
	char romanNumeral;
	cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
	cin >> romanNumeral;
	romanType roman=romanType(romanNumeral);  //  ** set up value
	cout << roman.convert() << endl;;

return 0;

Is This A Good Question/Topic? 0
  • +

Replies To: Convert Roman Numberal from C++ to JAVA code

#2 GWatt  Icon User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,104
  • Joined: 01-December 05

Re: Convert Roman Numberal from C++ to JAVA code

Posted 16 December 2006 - 04:38 PM

Is this supposed to handle a Roman Numeral string, or simply one character, like 'M'?

Anyway, in Java you to explicitly declare the scope of each field/method. If you do not, the default is whatever the class is, which would be public (usually). That looks like private int foo; No colon after the scope declaration.
To use other classes, you have to import their package. import java.util.*; You can specify a single class in a package, or you can use the '.*' to gain access to all the classes in that package. The java.lang package is already included automatically in every program.
When you declare methods in java, yu do not put the class name and then the method name, you simply give a scope, return type, method name, and the signature. public int fizzle(int foo) { } And FYI, the main method in java is declared this way: public static void main(String argv[]) The compiler will give you an error if you try anything else. At least, it did the last time I futzed around with it.
Most java classes are declared like this Foo fooObj = new Foo() Some classes cannot be declared that way, and some may be declared like regular primitve data types. When in doubt, consult the Java API.

Also there is no operator overloading in java (fooey)

I hope that helps you convert it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1