Help Me Please!!!!!!! Grade Droping As We Speak


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help Me Please!!!!!!! Grade Droping As We Speak

  1. #1
    Join Date
    Sep 2003
    Posts
    2

    Exclamation Help Me Please!!!!!!! Grade Droping As We Speak

    I need to finish this project for Programming 2 class. My
    Gay school just decided this year to switch us from c++ to Java and now i'm falling behind. I have gotten the basis of the program to work. What i have to do is make a program that prints out the value of roman numerals.
    Example: I=1 V=5 X=10. The only thing is i can't figure out how to get the program to subtract. Example: IV=4.
    Here is my programso far.
    Code:
    import javax.swing.JOptionPane;
    public class yomoma {
    
        public static void main(String args[])
        {
    	
    		//The place where variables are declared
    		String s1,s2;
    		int num1,ch,I = 1,V = 5,X = 10,L = 50,C = 100,D = 500,M = 1000,num;
    		num1 = 0;
    		//The place where input is entered.
    		s1 = JOptionPane.showInputDialog("Please enter a roman numeral.");
    		s2 = s1.toUpperCase();
    
    		for(int y=0;y<s2.length();y++)
    		{	
    			ch=s2.charAt(y);
            	if(ch=='I')
                {
                    num1 += 1;                    
                }
    			else if(ch == 'V')
    			{
    				num1 += 5;
    			}
    			else if(ch == 'X')
    			{
    				num1 = num1 + 10;
    			}
    			else if(ch == 'L')
    			{
    				num1 = num1 + 50;
    			}
    			else if(ch == 'C')
    			{
    				num1 = num1 + 100;
    			}
    			else if(ch == 'D')
    			{
    				num1 = num1 + 500;
    			}
    			else if(ch == 'M')
    			{
    				num1 = num1 + 1000;
    			}
    									
    		}// end for
    		JOptionPane.showMessageDialog(null,""+ num1 +"");
        } // end main
    } // end class
    can someone plz help me figure it out?
    Thanks in advance.

    [Added CODE tags - ArchAngel]
    I'm to Sexy for my Java application.

  2. #2
    Join Date
    Mar 2003
    Posts
    834

    Lightbulb

    I'm not sure what you mean "i can't figure out how to get the program to subtract" - I'm assuming you mean IV = 5 - 1 = 4. Anyway, here are some observations:

    1. The code:
    Code:
    s1 = JOptionPane.showInputDialog("Please enter a roman numeral.");
    s2 = s1.toUpperCase();
    "s1" and "s2" are terrible variable names! How about:
    Code:
    userInput = JOptionPane.showInputDialog("Please enter a roman numeral.");
    userInput = userInput.toUpperCase();
    2. The line:
    Code:
    int num1,ch,I = 1,V = 5,X = 10,L = 50,C = 100,D = 500,M = 1000;
    Should really be declared final as all of these are constants. Also they should be declared at the class level, rather than within the method.

    3. I don't think you've really thought this problem though - I really don't think you've come up with an algorithm of how to solve this problem. I know you're on a deadline and you're probably going to skip over this bit, but I really cannot stress enough that before you start coding, you should have a set of steps that you can follow through on paper to convert a roman numeral into a regular number. Once you've worked through it several times on paper, you'll have the algorithm clear in your head, ready to convert to code.

    OK. I'm feeling nice, so I'm going to work through an example. You will have to convert this into an algorithm...

    214 = CCXIV

    I first of all look at the last character 'V'. I add 5 to my running total. I then look at the next character 'I'. Now, 'I' is less than 'V', so we SUBTRACT 1 from the running total (so far, 4). I then look at the next character 'X'. I add 10 to my running total (so far, 14). I then look at the next character 'C'. I add 100 to my running total (so far, 114). I then look at the next character 'C'. I add 100 to my running total (so far, 214). I have run out of character - my running total is 214!

    Get the idea?

    4. If I was you, I would set up some automated testing, on this site you will find a HUGE collection of test data:

    http://www.wilkiecollins.demon.co.uk/roman/num1.htm

    This should be more than enough for you to write a first-class program.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Oct 2003
    Posts
    9

    Theory

    We had to produce one of these things too in college way back when, but I had to write it in COBOL - so consider yourself lucky...
    Anyway, the trick is to compare the current character to the last and if it is higher, then subtract it from the running total, and if it is lower, then to add it.
    Good luck.

    Ralpharama.

    [Note] No, hang on, that's not quite right is it? If the current character is higher than the last, you have to subtract the previous character from it and add that to the running total. Never did very well in that course. No wonder we adopted arabic numbers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center