Check the code ChangingPeoplejava For method changePeople li
Check the code ChangingPeople.java. For method changePeople, list the actual parameters and formal parameters below: Actual paramters are:______________________________________________________ Formal parameters are:_____________________________________________________ Among the actual paramters, which are passed by value and which are passed by object? Pass by value:____________________________ Pass by object:___________________________ Choose the correct statement to fill in the blanks: When a method passes an object as parameter, both the formal parameter and actual parameter are referring to the same object. So, _______; When a method pass a value as parameter, only a copy of the actual parameter is send to the actual parameter. So, _______; if the formal parameter is changed, actual parameter is changed accordingly. if the formal parameter is changed, actual parameter is not changed. HERE IS ACCOUNT.JAVA //******************************************************* // Account.java // // A bank account class with methods to deposit, withdraw, // and check the balance. //******************************************************* public class Account { private double balance; private String name; private long acctNum; //---------------------------------------------- //Constructor -- initializes balance, owner, and account number //---------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } //---------------------------------------------- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. //---------------------------------------------- public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println(\"Insufficient funds\"); } //---------------------------------------------- // Adds deposit amount to balance. //---------------------------------------------- public void deposit(double amount) { balance += amount; } //---------------------------------------------- // Returns balance. //---------------------------------------------- public double getBalance() { return balance; } //---------------------------------------------- // Returns account number //---------------------------------------------- public double getAcctNumber() { return acctNum; } //---------------------------------------------- // Returns a string containing the name, acct number, and balance. //---------------------------------------------- public String toString() { return \"Name: \" + name + \"\ Acct #: \" + acctNum + \"\ Balance: \" + balance; } } HERE IS CHANGINGPEOPLE.JAVA // ****************************************************************** // ChangingPeople.java // // Demonstrates parameter passing -- contains a method that should // change to Person objects. // ****************************************************************** public class ChangingPeople { // --------------------------------------------------------- // Sets up two person objects, one integer, and one String // object. These are sent to a method that should make // some changes. // --------------------------------------------------------- public static void main (String[] args) { Person person1 = new Person (\"Sally\", 13); Person person2 = new Person (\"Sam\", 15); int age = 21; String name = \"James\"; changePeople (person1, person2, age, name); System.out.println (\"\ Values after calling changePeople...\"); System.out.println (\"person1: \" + person1); System.out.println (\"person2: \" + person2); System.out.println (\"age: \" + age + \"\\tname: \" + name + \"\ \"); } // ------------------------------------------------------------------- // Change the first actual parameter to \"Jack - Age 101\" and change // the second actual parameter to be a person with the age and // name given in the third and fourth parameters. // ------------------------------------------------------------------- public static void changePeople (Person p1, Person p2, int age, String name) { // Make changes p1.changeName (\"Jack\"); p1.changeAge (101); p2.changeName (name); p2.changeAge (age); } }
Solution
Actual paramters are; Person p1,Person p2,String s, int a, and Formal parameters are:person1, person2, age, name Among the actual paramters, which are passed by value and which are passed by object? Pass by value: String s, int a.Pass by object: Person p1,Person p2 When a method passes an object as parameter, both the formal parameter and actual parameter are referring to the same object. So,no separate copy is created; When a method pass a value as parameter, only a copy of the actual parameter is send to the actual parameter. So, separate copy is created; if the formal parameter is changed, actual parameter is changed accordingly. if the formal parameter is changed, actual parameter is not changed.
Actual parameters are parameters that in actual defination of function hence they are according to Function Defination of changepeople.
Formal parameters are parameters that are send to function while calling we can get this in main method.
