javaoopmethods

How object-to-object communication in Java works?


I am new in learning Java just finished with Methods lecture. I am working on assignment and I need to write program for Money transfer service. I have two classes, first one is CurrencyConverter and second one is MoneyTransferService, which uses the functionality of CurrencyConverter class. In the MoneyTransferService class I have main method where I created a MoneyTransferService object, which in turn is using the services offered by CurrentConverter object. I dont understand how object are communicate between each other?

When I run program with commented code works perfectly, but I dont undrestand why I have to create an object which will use service offered by another object?

class MoneyTransferService {
    public static void main(String[] args) {
        
                CurrencyConverter cc = new CurrencyConverter(); 

                /*double amount = cc.computeTransferAmount(0, 1000);                
               
                double fee = cc.computeTransferFee(0, 1000);
                
                System.out.println("Transfer Amount: " + amount);

                System.out.println("Transfer Fee: " + fee);*/
                
                

                
                MoneyTransferService transferService = new MoneyTransferService();
                
                

                double transferAmount = transferService.computeTransferAmount(0, 1000);

                double transferFee = transferService.computeTransferFee(0, 1000);

                System.out.println("Transfer Amount: " + transferAmount);

                System.out.println("Transfer Fee: " + transferFee);
                
                
    }  
   
}
public class CurrencyConverter {   

        // Currency exchange rates of different currencies relative to 1 US dollar

       double[] exchangeRates = new double[] {63.0, 3.0, 3.0, 595.5, 18.0, 107.0, 2.0};

   

       void setExchangeRates(double[] rates) {

             exchangeRates = rates;

        }

        void updateExchangeRate(int countryIndex, double newVal) {

             exchangeRates[countryIndex] = newVal;

        }



        double getExchangeRate(int countryIndex) {

            return exchangeRates[countryIndex];

        }

        double computeTransferAmount(int countryIndex, double amount) {

            return amount * getExchangeRate(countryIndex);

        }
        
        double computeTransferFee (int countryIndex, double amount) {
            
            return computeTransferAmount(countryIndex, amount) * 0.02;
            
        }

   

        void printCurrencies() {

             System.out.println("\nrupee: " + exchangeRates[0]);

             System.out.println("dirham: " + exchangeRates[1]);

             System.out.println("real: " + exchangeRates[2]);

             System.out.println("chilean_peso: " + exchangeRates[3]);

             System.out.println("mexican_peso: " + exchangeRates[4]);

             System.out.println("_yen: " + exchangeRates[5]);

             System.out.println("$australian: " + exchangeRates[exchangeRates.length-1]);

       }

}

Solution

  • You're using the transferService instance to access the computeTransferAmount, and computeTransferFee, methods.
    These methods are defined within the CurrencyConverter class, which can be referenced via the cc instance you've created.

    double transferAmount = cc.computeTransferAmount(0, 1000);
    double transferFee = cc.computeTransferFee(0, 1000);