-1

I report for you the question of my teacher:

Implement the static utility method intValue and reuse the code you have in the instance method.

// TODO implement this
        public static int intValue(String roman) {

            return 0;
        }

I don't understand better why I need to use a static method and why I need to reuse the code, that I think it's like this:

public int intValue ()  {
        int decimal = 0;
        int lastNumber = 0;
        number = number.toUpperCase();
        for (int x = number.length() - 1; x >= 0; x--) {
            char convertNumber = number.charAt(x);
            switch (convertNumber) {
            case 'M':
                decimal = processDecimal(1000, lastNumber, decimal);
                lastNumber = 1000;
                break;
            case 'D':
                decimal = processDecimal(500, lastNumber, decimal);
                lastNumber = 500;
                break;
            case 'C':
                decimal = processDecimal(100, lastNumber, decimal);
                lastNumber = 100;
                break;
            case 'L':
                decimal = processDecimal(50, lastNumber, decimal);
                lastNumber = 50;
                break;
            case 'X':
                decimal = processDecimal(10, lastNumber, decimal);
                lastNumber = 10;
                break;
            case 'V':
                decimal = processDecimal(5, lastNumber, decimal);
                lastNumber = 5;
                break;
            case 'I':
                decimal = processDecimal(1, lastNumber, decimal);
                lastNumber = 1;
                break;
            }   
        }
        return decimal;

Could someone explain to me? Thanks

InExperience
  • 103
  • 1
  • 3
  • 12
  • The answer to the question "Why" is probably: because the teacher told you to. As for the rest, what is your exact problem? what have you tried doing, where have you failed, what kind of errors did you get, ...? – UnholySheep Aug 17 '16 at 22:17
  • Why do you want to have a "static class"? The assignment asks you to implement a static method. That is a class method that has the `static` keyword (as can see in your first posted code). And by code reuse I am pretty sure your teacher meant for you to call the instance method (and not copy paste). I feel like you should be asking your teacher this questions as he is the one who created this assignment and can answer them better than anyone on a forum or website. Also what is a "static header" in Java? – UnholySheep Aug 17 '16 at 22:28
  • @UnholySheep: yes I said a mistake: My exact problem is how to make a static method? How reuse this code, I need to copy and paste into the static header closes by bracket? like this: public static int intValue(String roman) { // inside here return 0; } – InExperience Aug 17 '16 at 22:30
  • I would recommend you to 1. Ask your teacher. 2. Google it. 3. Use a good Java tutorial. I have already told you "how" to make a static method (as it comes from your code) and that I don't believe your teacher wants you to copy paste. If you want better explanations ask him/her as I can only guess at your teacher's intentions – UnholySheep Aug 17 '16 at 22:34
  • I have posted when I have started the question what my teacher has required:"Implement the static utility method intValue and reuse the code you have in the instance method." Also I have posted below the entire code. – InExperience Aug 17 '16 at 22:39
  • Things wrong with that: 1. why did you post your existing code as an answer? It's not an answer 2. In the time it took you to do that you could have followed any of my given advice (especially googling). 3. Assuming your existing code is correct, all you have to do is write 2 lines of code 4. You have shown 0 intention of doing any of the simple work you would have to do to solve this problem on your own or even just asking your teacher for help. So I am not going to answer you any further – UnholySheep Aug 17 '16 at 22:53
  • Sorry Sir, if I googled I don't have an answer. My questions was clear why and how to implement the static method intValue - Roman Numeral convert. If you don't know how and why, I say thank you :) – InExperience Aug 17 '16 at 22:59

2 Answers2

0

Your teacher wants you to remove the line return 0; and write in what the method body should be:

    public static int intValue(String roman) {

        //write code here that returns an int based on a Roman numeral String like "MVCXIII"
    }
Scott Shipp
  • 2,241
  • 1
  • 13
  • 20
0

I'm so sorry if I made mistakes on correct operation of the site.

I moved all the implementation method public int intValue() into the private static int intValue(String roman) and refactor the method public int intValue(). I think that this could be the means of the request of my teacher; a tentative solution is the following:

    public static int intValue (String roman){
        int decimal = 0;
        int lastNumber = 0;
        roman = roman.toUpperCase();
        for (int x = roman.length() - 1; x >= 0; x--) {
            char convertNumber = roman.charAt(x);       
            switch (convertNumber) {
            case 'M':
                decimal = processDecimal(1000, lastNumber, decimal);
                lastNumber = 1000;
                break;
            case 'D':
                decimal = processDecimal(500, lastNumber, decimal);
                lastNumber = 500;
                break;
            case 'C':
                decimal = processDecimal(100, lastNumber, decimal);
                lastNumber = 100;
                break;
            case 'L':
                decimal = processDecimal(50, lastNumber, decimal);
                lastNumber = 50;
                break;
            case 'X':
                decimal = processDecimal(10, lastNumber, decimal);
                lastNumber = 10;
                break;
            case 'V':
                decimal = processDecimal(5, lastNumber, decimal);
                lastNumber = 5;
                break;
            case 'I':
                decimal = processDecimal(1, lastNumber, decimal);

                lastNumber = 1;
                break;
            }
        }
        return decimal;     
    }

    public int intValue()  {

        int rn = this.intValue();
        return rn;  
    }
InExperience
  • 103
  • 1
  • 3
  • 12