-1

I'm a beginner in Java. I'm trying to figure out how to get this program up and running. I need to calculate the sum of 2 money inputs using rupees and paise. I am using the method setMoney() to input the money values and using the method findSum(Money m1, Money m2) to calculate the total sum of the 2 money inputs. Please ignore the findDifference method.

For some reason, the output from the findSum() method is coming as 0 and I am itching my head off the whole day to figure out why. Can anyone explain the problem and help me fix it?

Here is the code:

import java.util.*;

class Money
{
    int r ;
    int p ;
    int sum1 ;
    int sum2 ;
    Money m ;

    static Money setMoney()
    {
        Money m = new Money() ;
        Scanner sc = new Scanner(System.in) ;
    
        System.out.println("Please enter the value in rupees") ;
        m.r = sc.nextInt() ;

        System.out.println("Please enter the value in paise") ;
        m.p = sc.nextInt() ;

        return m ;
    }

    void findSum(Money m1, Money m2)
    {
        // Calculate the sum of the money here and display the result here itself
        Money m = new Money() ;
        m.sum1 = m.r + (m.p /100) ;
        m.sum2 = m.p % 100 ;
    
        System.out.println("Sum of money:"+m.sum1+" rupees "+m.sum2+" paise") ;
    } 

    static void findDifference(Money m1, Money m2)
    {
        // Calculate the difference of the money here and display the result here itself
        System.out.println("Difference of money") ;
    }

    public static void main(String[] args)
    {
        Money a = new Money(); 
    
        System.out.println("For the first set of money values") ;
        Money m1 = Money.setMoney() ;

        System.out.println("For the second set of money values") ;
        Money m2 = Money.setMoney() ;

        a.findSum(m1, m2) ;  // Find the sum of money objects
        Money.findDifference(m1, m2) ;// Find the difference of money objects
    }
}
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158

2 Answers2

2

The findSum is off because of multiple issues.

  • you do not use m1 and m2
  • if you were using them you would ignore the r of one of them
  • there is no point in having sum1 and sum2.

Instead the code should look like

void findSum(Money m1, Money m2)
{
    // Calculate the sum of the money here and display the result here itself
    Money m = new Money();
    int pSum = m1.p + m2.p;
    m.r = m1.r + m2.r + (pSum / 100);
    m.p = pSum % 100;

    System.out.println("Sum of money: " + m.r + " rupees " + m.p + " paise") ;
} 

and then drop sum1 and sum2 entirely from the code, they should not and currently have no reason to exist.

luk2302
  • 46,204
  • 19
  • 86
  • 119
0

Even though this is not great code design. Is that what you want?:

void findSum(Money m1, Money m2)
{
    // Calculate the sum of the money here and display the result here itself
    Money m = new Money();
    m.sum1 = m1.r + m2.r;
    m.sum2 = m1.p + m2.p;

    System.out.println("Sum of money:"+m.sum1+" rupees "+m.sum2+" paise");
}
IntoVoid
  • 714
  • 2
  • 7
  • Only partially because OP already had tried to manage the "overflow" of paise into rupees using the `/` and `%`. – luk2302 Mar 27 '21 at 16:48