Question

In: Computer Science

1. what gets printed if anything after executing the main method of class BankAccount below: public...

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.

Solutions

Expert Solution

(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. :)


Related Solutions

Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount...
using C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (using name & id), and - keeps track of a user's available balance. - how many transactions (deposits and/or withdrawals) are made. public class BankAccount { private int id; private String name private double balance; private int numTransactions; // ** Please define method definitions for Accessors and Mutators functions below for the class private member variables string getName();...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a...
Modify Example 5.1 to add a ReadAccount method to the BankAccount class that will return a BankAccountconstructed from data input from the keyboard. Override ReadAccount in SavingsAccount to return an account that refers to a SavingsAccount that you construct, again initializing it with data from the keyboard. Similarly, implement ReadAccount in the CheckingAccount class. Use the following code to test your work. public static void testCode()         {             SavingsAccount savings = new SavingsAccount(100.00, 3.5);             SavingsAccount s = (SavingsAccount)savings.ReadAccount();...
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                    void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref; slow_ref.next...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. (All other classes are provided below) Bank Account.java (This class is the one that needs to be modified) /** A bank account has a balance that can be changed by deposits and withdrawals. */...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000...
Problem: Add a condition to the deposit method of the BankAccount class, restricting deposits to $100,000 (the insurance limit of the U.S. government). The method should block until sufficient money has been withdrawn by another thread. Test your program with a large number of deposit threads. Bank account class: (THE ONE THAT NEEDS TO BE EDITED) /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private double balance; /**...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT