Question

In: Computer Science

Write a Java method that takes an input string and computes the income minus the expenses....

Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'.

The input string may contain lowercase and uppercase letters, as well as other characters.

Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int.

Test cases : calcNetIncome("salary 15000yuan bonus2000 rent -1000Y") → 16000 calcNetIncome("25000 gross income, -200 water, electricity:-300") → 24500

Solutions

Expert Solution

/* Java class */
public class Main{
        /* This function calculates the net income */ 
        /* Param: An alphanumeric string <s>*/
        /* Return: An integer number <net>, denoting net income */
        public int calcNetIncome(String s){
                /* declare variables */
                int len, i, net = 0;
                String num = "";
                
                /* find length of string */
                len = s.length();
                
                /* check for each character */
                for(i = 0; i <len; i++){
                        /* if characters are digits or -, concatenate */
                        if (((int)s.charAt(i) >= 48 && (int)s.charAt(i) <= 57) || (int)s.charAt(i) == 45){
                                num += s.charAt(i);
                        }
                        else{
                                /* if a string of digits and - found */
                                /* convert to integer and add to sum */
                                if(num.length() > 0){
                                        net += Integer.parseInt(num);
                                        /* make string empty */
                                        num = ""; 
                                }
                        }
                }
                /* End of string reached while reading an integer */
                if(num.length() > 0)
                        net += Integer.parseInt(num);
                
                /* return net income */
                return net;
        }
        public static void main(String[] args){
                /* declare object */
                Main obj = new Main();
                
                /* declare variables */
                int result;
                
                /* call the function */
                result = obj.calcNetIncome("25000 gross income, -200 water, electricity:-300");
                
                /* display the result */
                System.out.print("Net income: "+result);
        }
}

___________________________________________________________________

___________________________________________________________________

Net income: 24500

___________________________________________________________________

Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.


Related Solutions

Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Create a Java method that takes a String as input value and returns the number of...
Create a Java method that takes a String as input value and returns the number of vowels contained in that string.
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
Write a Java program which takes a String representing an arithmetic expression as an input and...
Write a Java program which takes a String representing an arithmetic expression as an input and displays whether or not the expression is balanced. If the expression is not balanced (i.e. the wrong # of parentheses or in the wrong order) it will display an error message. For Example: Input an expression: ( ( 2 + 4 ) * 2 ) Expression is balanced Input an expression: ( 5 * 7 – 6 ) ) Error: Expression is not balanced.......
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
1 Write a Java method that takes an integer, n, as input and returns a reference...
1 Write a Java method that takes an integer, n, as input and returns a reference to an array of n random doubles between 100.0 and 200.0. Just write the method. 2. You have a class A: public class A { int i, double d; public A(double d) { this.d=d; this.i=10; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT