2. Each of the following programs contains exactly one syntax error. Find the error and correct it. a) public static void main(String[] args) { BankAsseunt a = new BankAsSeunt (500); Bankacseunt b = new Bankacseuet () ; double c Sxsten.eut intin( "The combined balance is $" + c); b) public static void main(String[] args) Bankaeseunt a = new BaakāEseurt (500); a += 300; { Sxsten.eut intln( "The new balance is $" + augetkalance () ) ; c) public static void main(String[] args) BankAeseuet a Fnderesit (500); GuBaakAEseunt () ; Sxstem.eutuRKintle (argetNane () + { = new BankaEGRUrt (100); now has a 0 balance");

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

JAVA

 

need help with question 2

### BankAccount Class Overview

1. **Description:**
   The `BankAccount` class models changes to the balance of a person's savings account.

2. **Class Definition:**

   ```java
   public class BankAccount {
       public BankAccount(double initBal) {}
       public double getBalance() {}
       public String getName() {}
       public void deposit(double amount) {}
       public void withdraw(double amount) {}
       public void transferTo(BankAccount otherAcct, double amt) {}
       public boolean equals(Object other) {}

       private String acctName;
       private double balance;
   }
   ```

3. **Methods and Attributes:**

   - **Constructor:** `BankAccount(double initBal)` initializes the account with a starting balance.
   - **Methods:**
     - `getBalance()`: Returns the current balance of the account.
     - `getName()`: Returns the name associated with the account.
     - `deposit(double amount)`: Adds a specified amount to the account's balance.
     - `withdraw(double amount)`: Deducts a specified amount from the account's balance.
     - `transferTo(BankAccount otherAcct, double amt)`: Transfers a specified amount to another `BankAccount`.
     - `equals(Object other)`: Compares this account with another object for equality.
   - **Attributes:**
     - `private String acctName`: The name of the account holder.
     - `private double balance`: The current balance of the account.

### Task

**a) Implement the `transferTo(...)` Method:**

```java
{
    balance = balance + amt;
    otherAcct.withdraw(amt);
}
```

**Explanation:** The `transferTo` method increases the balance of the current account by the `amt` and deducts the same `amt` from the `otherAcct`. This allows for transferring funds between accounts.
Transcribed Image Text:### BankAccount Class Overview 1. **Description:** The `BankAccount` class models changes to the balance of a person's savings account. 2. **Class Definition:** ```java public class BankAccount { public BankAccount(double initBal) {} public double getBalance() {} public String getName() {} public void deposit(double amount) {} public void withdraw(double amount) {} public void transferTo(BankAccount otherAcct, double amt) {} public boolean equals(Object other) {} private String acctName; private double balance; } ``` 3. **Methods and Attributes:** - **Constructor:** `BankAccount(double initBal)` initializes the account with a starting balance. - **Methods:** - `getBalance()`: Returns the current balance of the account. - `getName()`: Returns the name associated with the account. - `deposit(double amount)`: Adds a specified amount to the account's balance. - `withdraw(double amount)`: Deducts a specified amount from the account's balance. - `transferTo(BankAccount otherAcct, double amt)`: Transfers a specified amount to another `BankAccount`. - `equals(Object other)`: Compares this account with another object for equality. - **Attributes:** - `private String acctName`: The name of the account holder. - `private double balance`: The current balance of the account. ### Task **a) Implement the `transferTo(...)` Method:** ```java { balance = balance + amt; otherAcct.withdraw(amt); } ``` **Explanation:** The `transferTo` method increases the balance of the current account by the `amt` and deducts the same `amt` from the `otherAcct`. This allows for transferring funds between accounts.
**Exercise 2: Finding and Correcting Syntax Errors in Java Programs**

Each of the following Java programs contains exactly one syntax error. Find the error and correct it.

**a)**
```java
public static void main(String[] args) {
    BankAccount a = new BankAccount(500);
    BankAccount b = new BankAccount();
    double c = a.balance + b.balance;
    System.out.println("The combined balance is $" + c);
}
```

**Explanation and Correction:**

- **Error:** The `balance` field is likely private and cannot be accessed directly. 
- **Correction:** Use a method like `getBalance()` to access the balance.

```java
double c = a.getBalance() + b.getBalance();
```

**b)**
```java
public static void main(String[] args) {
    BankAccount a = new BankAccount(500);
    a += 300;
    System.out.println("The new balance is $" + a.getBalance());
}
```

**Explanation and Correction:**

- **Error:** The operator `+=` cannot be used directly on object instance `a`; it should operate on a primitive or use a method.
- **Correction:** Use a deposit method like `a.deposit(300);`.

```java
a.deposit(300);
```

**c)**
```java
public static void main(String[] args) {
    BankAccount a = new BankAccount(100);
    a.deposit(500);
    a.BankAccount();
    System.out.println(a.getName() + " now has a 0 balance");
}
```

**Explanation and Correction:**

- **Error:** `a.BankAccount();` is not a valid method call; syntax for constructor is incorrect.
- **Correction:** Remove the incorrect line. Balances should update without calling constructors again.

```java
// Remove: a.BankAccount();
```

These corrections help ensure the code complies with standard syntax practices and logic for operations on objects.
Transcribed Image Text:**Exercise 2: Finding and Correcting Syntax Errors in Java Programs** Each of the following Java programs contains exactly one syntax error. Find the error and correct it. **a)** ```java public static void main(String[] args) { BankAccount a = new BankAccount(500); BankAccount b = new BankAccount(); double c = a.balance + b.balance; System.out.println("The combined balance is $" + c); } ``` **Explanation and Correction:** - **Error:** The `balance` field is likely private and cannot be accessed directly. - **Correction:** Use a method like `getBalance()` to access the balance. ```java double c = a.getBalance() + b.getBalance(); ``` **b)** ```java public static void main(String[] args) { BankAccount a = new BankAccount(500); a += 300; System.out.println("The new balance is $" + a.getBalance()); } ``` **Explanation and Correction:** - **Error:** The operator `+=` cannot be used directly on object instance `a`; it should operate on a primitive or use a method. - **Correction:** Use a deposit method like `a.deposit(300);`. ```java a.deposit(300); ``` **c)** ```java public static void main(String[] args) { BankAccount a = new BankAccount(100); a.deposit(500); a.BankAccount(); System.out.println(a.getName() + " now has a 0 balance"); } ``` **Explanation and Correction:** - **Error:** `a.BankAccount();` is not a valid method call; syntax for constructor is incorrect. - **Correction:** Remove the incorrect line. Balances should update without calling constructors again. ```java // Remove: a.BankAccount(); ``` These corrections help ensure the code complies with standard syntax practices and logic for operations on objects.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education