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
}
}