• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Enerjak

very weird problem in my program with roman numerals.

5 posts in this topic

I'm having the weirdest problem ever. See my assignment was to convert roman numerals to dec and vise versa. I also had to do arithmetic with them and convert them back to roman. when I do subtract it seems to work with all numbers except: X - II = XIII = 13 which is wrong!!!
anyways, here is the code for your viewing displeasure :-D

Roman.java
[CODE]
public class Roman
{
protected String romanNum;
protected int decimalNum;
public Roman()
{
romanNum = "I";
decimalNum = 1;
}
public Roman(String rString)
{
romanNum = rString;
romanToDecimal();
}
public void printDecimal()
{
System.out.println(decimalNum);
}
public void printRoman()
{
System.out.println(romanNum);
}
public void setRoman(String rString)
{
romanNum = rString;
romanToDecimal();
}
public void setDecimal(int dec)
{
this.decimalNum = dec;
this.DecimalToRoman();
}
public String getRoman()
{
return romanNum;
}

public int getDecimal()
{
return decimalNum;
}

public void DecimalToRoman()
{
String sum = "";
int temp = 0;

int answer = this.decimalNum / 1000;

for(int i = 0; i < answer; ++i)
{
sum = sum + "M";
}

answer = decimalNum % 1000;

temp = answer;
answer = temp / 500;

for(int i = 0; i < answer; ++i)
{
sum = sum + "D";
}
answer = temp % 500;

temp = answer;
answer = temp / 100;

for(int i = 0; i < answer; ++i)
{
sum = sum + "C";
}

answer = temp % 100;

temp = answer;
answer = temp / 50;

for(int i = 0; i < answer; ++i)
{
sum = sum + "L";
}

answer = temp % 50;

temp = answer;
answer = temp / 10;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 10;
temp = answer;
answer = temp / 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 5;

temp = answer;
answer = temp / 1;
for(int i = 0; i < answer; ++i)
{
sum = sum + "I";
}
answer = temp % 1;
romanNum = sum;

}
public void romanToDecimal()
{
int sum = 0;
int len = romanNum.length();
int index;
int previous = 1000;
for (index = 0; index < len; index++)
{
switch (romanNum.charAt(index))
{
case 'M': sum = sum + 1000;
if (previous < 1000)
sum = sum - 2 * previous;
previous = 1000;
break;
case 'D': sum = sum + 500;
if (previous < 500)
sum = sum - 2 * previous;
previous = 500;
break;
case 'C': sum = sum + 100;
if (previous < 100)
sum = sum - 2 * previous;
previous = 100;
break;
case 'L': sum = sum + 50;
if (previous < 50)
sum = sum - 2 * previous;
previous = 50;
break;
case 'X': sum = sum + 10;
if (previous < 10)
sum = sum - 2 * previous;
previous = 10;
break;
case 'V': sum = sum + 5;
if (previous < 5)
sum = sum - 2 * previous;
previous = 5;
break;
case 'I': sum = sum + 1;
previous = 1;
}
}
decimalNum = sum;
}

// to string
public String toString()
{
return "Roman num: " + romanNum;
}
}
[/CODE]

ExtRomans

[CODE]
public class ExtRomans extends Roman
{
public ExtRomans sub(ExtRomans r1, ExtRomans r2)
throws romanException
{
ExtRomans ret = new ExtRomans();
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

int Sub = (val1 - val2);
if(Sub < 0)
{
throw new romanException("Roman numerals cannot be negative");
}

System.out.println("Subtract = " + Sub);




ret.setDecimal(Sub);

ret.printRoman();
return ret;
}
public ExtRomans multiply(ExtRomans r1, ExtRomans r2)
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

ExtRomans ret = new ExtRomans();

int answer = (val1 * val2);
ret.setDecimal(answer);
return ret;
}
public ExtRomans divide(ExtRomans r1, ExtRomans r2)
throws romanException
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;
int div = 0;
if(val1 < val2)
{
throw new romanException("the divisor cannot be less then the dividend");
}
else
{
div = (val1/val2);
}
ExtRomans ret = new ExtRomans();
ret.setDecimal(div);
return ret;
}
public ExtRomans add(ExtRomans r1, ExtRomans r2)
{
int val1 = r1.decimalNum;
int val2 = r2.decimalNum;

int sum = val1 + val2;
ExtRomans ret = new ExtRomans();
ret.setDecimal(sum);
return ret;
}
}
[/CODE]

romanException.java

[CODE]
public class romanException extends Exception
{
public romanException(String message)
{
super(message);
}
}
[/CODE]


and last Application.java

[CODE]

import java.util.*;
public class TestRoman
{
static Scanner console = new Scanner(System.in);
static String romanStr1;
static String romanStr2;

public static void divRoman()
{
try
{
System.out.println("Enter the first roman numeral: ");

String roman1 = console.next();

System.out.println("Enter the second roman numeral: ");

String roman2 = console.next();

ExtRomans r1 = new ExtRomans();
ExtRomans r2 = new ExtRomans();
ExtRomans r3 = new ExtRomans();
r1.setRoman(roman1);
r2.setRoman(roman2);
r3 = r3.divide(r1, r2);

System.out.println(r3);
}
catch(romanException e)
{
System.out.println(e.getMessage());
}


}

public static void mulRoman()
{
System.out.println("Enter first roman to multiply: ");

String roman1 = console.next();

System.out.println("Enter the second roman to multiply: ");

String roman2 = console.next();

ExtRomans r1 = new ExtRomans();
ExtRomans r2 = new ExtRomans();
ExtRomans r3 = new ExtRomans();

r1.setRoman(roman1);
r2.setRoman(roman2);
r3 = r3.multiply(r1, r2);

System.out.println(r3);
}
public static void subRoman() throws romanException
{
try
{
System.out.println("Enter first number to subtract: ");

romanStr1 = console.next();

ExtRomans r1 = new ExtRomans();
r1.setRoman(romanStr1);

System.out.println("Enter the second num to subtract");

romanStr2 = console.next();

ExtRomans r2 = new ExtRomans();
r2.setRoman(romanStr2);

ExtRomans r3 = new ExtRomans();

r3 = r3.sub(r1, r2);

System.out.println("The decimal val = " + r3.getDecimal());
System.out.println(r3);

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void addRoman()
{
romanStr1 = " ";
romanStr2 = " ";
System.out.println("Type the first roman number: ");
romanStr1 = console.next();

ExtRomans r1 = new ExtRomans();
r1.setRoman(romanStr1);
System.out.println("Type the second roman number: ");
romanStr2 = console.next();
ExtRomans r2 = new ExtRomans();
r2.setRoman(romanStr2);

ExtRomans r3 = new ExtRomans();

r3 = r3.add(r1, r2);
System.out.println(r3);
}
public static void main(String[] args) throws romanException
{
boolean quit = false;

String menu = " ";

menu += ""
+ "1) add two roman numbers \n"
+ "2) divide two roman numbers \n"
+ "3) Multiply two roman numbers \n"
+ "4) Subtract two roman numbers \n"
+ "5) Exit";
double val1;
while(!quit)
{

System.out.println(menu);
int choice = console.nextInt();

switch(choice)
{
case 1:
{
TestRoman.addRoman();
}
break;
case 2:
{
TestRoman.divRoman();
}
break;
case 3:
{
TestRoman.mulRoman();
}
break;
case 4:
{
TestRoman.subRoman();
}
break;
case 5:
{
quit = true;
}
}
}
// exRo.divideRomans(romanString, romanString2);

}
}

[/CODE]

i can't figure out why X - II = XIII = 13
0

Share this post


Link to post
Share on other sites
You're doing a lot of extraneous operations. Start with this:

[CODE]
public void DecimalToRoman()
{
String sum = "";
int temp = 0;

int answer = this.decimalNum / 1000;

for(int i = 0; i < answer; ++i)
{
sum = sum + "M";
}

temp = decimalNum % 1000;

answer = temp / 500;

for(int i = 0; i < answer; ++i)
{
sum = sum + "D";
}

temp %= 500;

answer = temp / 100;

for(int i = 0; i < answer; ++i)
{
sum = sum + "C";
}

temp %= 100;

answer = temp / 50;

for(int i = 0; i < answer; ++i)
{
sum = sum + "L";
}

temp %= 50;

answer = temp / 10;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

temp %= 10;

answer = temp / 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "X";
}

answer = temp % 5;

for(int i = 0; i < answer; ++i)
{
sum = sum + "I";
}

romanNum = sum;

}
[/CODE]

Past that, check your code for fives. It will never output a V; it was carelessly copy-pasted from the tens code. Edited by Ectara
1

Share this post


Link to post
Share on other sites
For homework it is generally best to let the person struggle with it, so they learn for themselves. Good answer, though!
0

Share this post


Link to post
Share on other sites
To clarify, that doesn't mean you cannot help at all. Directly posting working answers is strongly discouraged. Hinting people toward the solution or pointing out bugs in the OP's current attempt is welcome however.
0

Share this post


Link to post
Share on other sites
Well, to be fair i did do most of the work i just needed to know why i was getting an X in the place of a V that would of been easy had i checked it but was kind of in a time crunch.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0