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...
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}
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The method signature is as follows:  public int count(String s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT