#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    java pass by reference


    Hello

    How can I get totaleDonation get value each time got to method? Is anything as pass by reference or global variable?

    PHP Code:
    public class Donat {
        
            
        public static 
    void main(String[] args) {
            
    int numberOfDonator;
            
    String name;        
            
    doubel donatAmoount;
            
    double totaleDonation 0;
            
    double average;


            
    numberOfDonator getNumberOfDonator();

            for(
    int i 0numberOfDonatori++){
                
    name getDonatorName();
                
    donatAmoount getDonatAmoount(nametotaleDonation);
            }

        }

        public static 
    int getNumberOfDonator(){
            
    int x;
            
    Scanner input = new Scanner(System.in);

            
    System.out.print("Enter number of Donator: ");
            
    input.nextInt();

            return 
    x;
        }

        public static 
    String getDonatorName(){
            
    String name;

            
    Scanner input = new Scanner(System.in);

            
    System.out.print("Enter Donator name: ");
            
    name input.next();

            return 
    name;
        }

        public static 
    double getDonatAmoount(String namedouble totaleDonation){
            
    double y;

            
    Scanner input = new Scanner(System.in);

            
    System.out.print("Enter donation amount for " +name": ");
            
    input.nextDouble();

            
    totaleDonation totaleDonation y;

            return 
    y;
        }


  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2016
    Posts
    1
    Rep Power
    0
    I 'think' I see what you are trying to do here. To maintain a running total of donations from different Donators
    you will need to do the calculation within the main() method where your for/loop is located, for example:

    Code:
    for(int i = 0; i < numberOfDonator; i++){
         name = getDonatorName();
        //Notice the modification to the getDonatAmoount() parameters
        donatAmoount = getDonatAmoount(name);
        totaleDonation+= donatAmoount;
    }
    
    System.out.println("\nTotal Overall Donations are: " + totaleDonation);
    You will also need to make a slight modification to your getDonatAmoount() method, for example:

    Code:
    public static double getDonatAmoount(String name){
        Scanner input = new Scanner(System.in);
    
        System.out.print("Enter donation amount for " + name + ": --> ");
        double y = input.nextDouble();  // declare and initialize y
        
        // Close the Scanner Object to free resources.
        // You should do this in all your methods where
        // you use the Scanner Object.
        input.close();
    
        return y;
    }
    In the case of your Scanner Object where you find yourself using it in all your methods you may find
    it better to just declare the Object as Class Global, for example, here is your entire code using that very
    concept:

    Code:
    package donat;
    
    import java.util.Scanner;
    
    public class Donat {
        // Declare the Scanner Object as Class Global...
        static Scanner input = new Scanner(System.in);
            
        public static void main(String[] args) {
            int numberOfDonators = 0;
            String name = "";        
            double donatAmount = 0.0;
            double totalDonation = 0.0;
            double average = 0.0;
    
            numberOfDonators = getNumberOfDonators();
    
            for(int i = 0; i < numberOfDonators; i++){
               name = getDonatorName();
               donatAmount = getDonationAmount(name);
               totalDonation+= donatAmount;
            }
            
            // Round off the totalDonation amount and the average amount 
            // to the third decimal point. If you want to round to two
            // decimal points then change the 1000.0 values in the code 
            // below to 100.0
            totalDonation = Math.round((totalDonation)*1000.0)/1000.0;
            average =  Math.round((totalDonation/numberOfDonators)*1000.0)/1000.0;
    
    
            // Close the Scanner Object to free resources.
            // You should do this whenever you use the Scanner Object.
            input.close();
            
            //Display overall Total Donations and Average Donation in Console...
            System.out.println("-----------------------------------------------\n");
            System.out.println("Total Overall Donations Are:\t$" + totalDonation);
            System.out.println("The Average Donation Given Is:\t$" + average);
        }
    
        private static int getNumberOfDonators(){
            System.out.print("Enter number of Donators: --> ");
            int x = input.nextInt(); input.nextLine();
            /*
            Note: In the code line above you can see an additional input request
                  to the Scanner.nextLine() method which is actuallt skipped and
                  basically applied to nothing. This is done because when the
                  Scanner.nextInt() or Scanner.nextDouble() (etc.) methods are 
                  used there is always a newline character that sits within the
                  Scanner buffer waiting to be used by the Scanner.nextLine() 
                  method. By doing things this way you clear that newline character
                  from the Scanner.
            */
            return x;
        }
    
        private static String getDonatorName(){
            System.out.print("\nEnter Donator name: --> ");
            String name = input.nextLine();
            return name;
        }
    
        private static double getDonationAmount(String name){
            System.out.print("Enter donation amount for " + name + ": --> ");
            double y = input.nextDouble(); input.nextLine();
            /*
            Note: In the code line above you can see an additional input request
                  to the Scanner.nextLine() method which is actually skipped and
                  basically applied to nothing. This is done because when the
                  Scanner.nextInt() or Scanner.nextDouble() (etc.) methods are 
                  used there is always a newline character that sits within the
                  Scanner buffer waiting to be used by the next Scanner.nextLine() 
                  method. By doing things this way you clear that newline character
                  from the Scanner.
            */
            return y;
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo