In: Computer Science
1. what gets printed if anything after executing the main method of class BankAccount below:
public class BankAccount {
private static int masterKey =1;
private String name;
private int accountNumber;
private double balance = 0;
private int age = 1;
public BankAccount (String myName , double
balance) {
name = myName;
this.balance = 5 + 2 * balance ;
accountNumber = masterKey;
age++;
masterKey++;
printMe();
}
public void printMe( ) {
System.out.println("Name : "+name);
System.out.println("Act# "+accountNumber);
System.out.println(“Balance
“+balance);
System.out.println(“Age “+age);
}
public static void main(String[] args) {
BankAccount a = new BankAccount(“Sami”, 500);
BankAccount b = new BankAccount(“Hala”,
700);
}
}
2. Make a static method called multiply in the Utility
class below that returns an int and has one parameter, an array of
int called nums. It should multiply all the numbers in the array
and return the result.
public class Utility {
}
3. Assuming that you are in a different class, write code to call your method in question 2 and find the result of multiplying the array having the values 52, 793, and 374.
(1). When this main() method is executed, it creates 2 objects with parameterized constructor. In this constructor, all the 5 data members values are modified and after that using printMe(), the same details are printed. This thing is done for two times. So, the output is:
Name : Sami
Act# 1
Balance 1005.0
Age 2
Name : Hala
Act# 2
Balance 1405.0
Age 2
(2). The required function is as follows: (The utility class)
package pkg;
public class Utility
{
public static int multiply(int nums[ ])
{
int result = 1;
for(int i=0; i<nums.length; i++)
{
result = result * nums[i];
}
return result;
}
}
Here, the function multiply is declared as public so that it can be used outside the class. This will help in the next question. static is used so that the function can be called by class name only, no need to create an object for the same. int specifies it returns an integer value. Now, inside the function, first define a result variable and initializing it with 1, the multiplication identity element. Now, from nums[0] to nums[length-1] we are multiplying the numbers and at last the result is returned. You can get the array length using length property directly so I have used it here.
(3). Now I am in a different class UtilityTesterClass which is in the same package where I wrote the above Utility class.
package pkg;
public class
UtilityTesterClass
{
public static void main(String[] args) {
int a[] = {52, 793, 374};
int multiply_result = Utility.multiply(a);
System.out.println("The multiplication result is:
"+multiply_result);
}
}
The reason of having in the same package helps in a way that you do not require to import that class in your code, it is already available because of the same package.
So, in this class, I wrote a main() method in which I created an integer array a[ ] with given values. Then I called the above multiply method as Utility.multiply(). This became possible because the method is declared static so you can call it with the class name. The result is stored in multiply_result variable and displayed. The output I got is:
The multiplication result is: 15422264
Please comment if there is any query. Thank you. :)