Question

Point out errors in the following codes and correct them public class CheckingAccount {       //...

Point out errors in the following codes and correct them

public class CheckingAccount {

      // Create attributes accountNum, Balance

      private int accountNum; // Account Number

      private double balance; // Account balance

      // Constructor

      public void CheckingAccount(int aNum, double balance) {

            // Call setters to initialize attribute values

            setAccountNumber(aNum);

            setBalance;

      }

      // Setters

      public void setAccountNumber(int aNum) {

            if (accountNum >= 100) {

                  accountNum = aNum;

            }

            else {

                  System.out.println("This account number is invalid (Account number must be at least 3 digits)!");

                  this.accountNum = 100; // assign a default value

            }

      }

      public void setBalance(double aBalance) {

            // make sure the value is positive

            if (aBalance > 0) {

                  aBalance = balance;

            }

            else {

                  System.out.println("This balance value is invalid (input must be positive)!");

                  aBalance = 0; // assign a default value

            }

      }

      // Getters

      public void getAccountNumber() {

            return accountNum;

      }

      public void getBalance() {

            return balance;

      }

      /*=================================

      * Create a method for depositing

      * Parameter to indicate the amount to be to be deposited (must be

      *     positive)

      ====================================*/

      public void deposit(double dAmount) {

            double balance = 0;

            // make sure the value is positive

            if (dAmount > 0) {

                  balance += dAmount;

            }

            else { // The balance does not change

                  System.out.println("This deposit value is invalid (input must be positive)!");

            }

      }

      /*====================================

      * Create a method for withdrawing

      * Parameter to indicate the amount to be withdrawn

      * Must be positive and never more than the balance( i.e. no overdrawing)

      ====================================*/

      public void withdraw(double wAmount) {

            // make sure the value is positive and less than the balance

            if (wAmount > 0) { //

                  if (this.balance >= wAmount) {

                        this.balance = wAmount;

                  }

                  else {

                        System.out.println("Withdraw exceeds balance, operation cancelled!");

                  }

            }

            else {//negative value

                  System.out.println("This withdraw value is invalid, withdraw must be positive!");

            }

      }

}//end of class definition

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

Highlighted line represent correction.

package classes1;
public class CheckingAccount {

   // Create attributes accountNum, Balance

   private int accountNum; // Account Number

   private double balance; // Account balance

   // Constructor

   public CheckingAccount(int aNum, double balance) { //no need of return type void

       // Call setters to initialize attribute values

       setAccountNumber(aNum);

      setBalance(balance); //set balance was not called properly

   }

   // Setters

   public void setAccountNumber(int aNum) {

       if (accountNum >= 100) {

           accountNum = aNum;

       }

       else {

           System.out.println("This account number is invalid (Account number must be at least 3 digits)!");

           this.accountNum = 100; // assign a default value

       }

   }

   public void setBalance(double aBalance) {

       // make sure the value is positive

       if (aBalance > 0) {

           aBalance = balance;

       }

       else {

           System.out.println("This balance value is invalid (input must be positive)!");

           aBalance = 0; // assign a default value

       }

   }

   // Getters

   public int getAccountNumber() { //return type should be int

       return accountNum;

   }

   public double getBalance() { //return type should be of double type

       return balance;

   }

   /*=================================

   * Create a method for depositing

   * Parameter to indicate the amount to be to be deposited (must be

   * positive)

====================================*/

   public void deposit(double dAmount) {

       //double balance = 0; //no need of balance

       // make sure the value is positive

       if (dAmount > 0) {

           balance += dAmount;

       }

       else { // The balance does not change

           System.out.println("This deposit value is invalid (input must be positive)!");

       }

   }

   /*====================================

   * Create a method for withdrawing

   * Parameter to indicate the amount to be withdrawn

   * Must be positive and never more than the balance( i.e. no overdrawing)

====================================*/

   public void withdraw(double wAmount) {

       // make sure the value is positive and less than the balance

       if (wAmount > 0) { //

           if (this.balance >= wAmount) {

               this.balance -= wAmount; //wAmound should be deducted

           }

           else {

               System.out.println("Withdraw exceeds balance, operation cancelled!");

           }

       }

       else {//negative value

           System.out.println("This withdraw value is invalid, withdraw must be positive!");

       }

   }

}//end of class definition

Add a comment
Know the answer?
Add Answer to:
Point out errors in the following codes and correct them public class CheckingAccount {       //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • NOTE: Use the Account class codes in the section below these questions. Modify the main method...

    NOTE: Use the Account class codes in the section below these questions. Modify the main method in the CheckingAccountDemo class: Take out all previous code. Declare an object array with 10 accounts. Create a for loop to ask user to input each account’s information (name, account number, and initial balance) from keyboard and then initialize for each account object. Create a while loop to allow one to work on depositing and withdrawing operations on any account till one want to...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • /* * To change this license header, choose License Headers in Project Properties. * To change...

    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkingaccounttest; // Chapter 9 Part II Solution public class CheckingAccountTest { public static void main(String[] args) {    CheckingAccount c1 = new CheckingAccount(879101,3000); c1.deposit(350); c1.deposit(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.withdraw(350); c1.withdraw(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.deposit(1000); System.out.println(c1); System.out.println("After calling computeMonthlyFee()"); c1.computeMonthlyFee(); System.out.println(c1); } } class BankAccount { private int...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes...

    According to the following information, define Account, CheckingAccount, and TestPart1 classes. 1. Account and CheckingAccount classes Account - number: int - openDate: String - name: String - balance: double + Account(int, String, String, double) + deposit(double): void + withdraw (double): boolean + transferTo(Account, double): int + toString(): String CheckingAccount + CheckingAccount(int, String, String, double) + transferTo(Account, double): int Given the above UML diagam, define Account and CheckingAccount classes as follow: Account (8 points) • public Account(int nu, String op, String...

  • The code is attached below has the Account class that was designed to model a bank account.

    IN JAVAThe code is attached below has the Account class that was designed to model a bank account. An account has the properties account number, balance, annual interest rate, and date created, and methods to deposit and withdraw funds. I need creat program using the 2 java programs.Create two subclasses for checking and savings accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn.I need to write a test program that creates objects of Account,...

  • *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount...

    *Step1: -Create UML of data type classes: reuse from lab3 for class Account, CheckingAccount and SavingAccount -Read the requirement of each part; write the pseudo-code in a word document by listing the step by step what you suppose to do in main() and then save it with the name as Lab4_pseudoCode_yourLastName *Step2: -start editor eClipse, create the project→project name: FA2019_LAB4PART1_yourLastName(part1) OR FA2019_LAB4PART2_yourLastName (part2) -add data type classes (You can use these classes from lab3) Account_yourLastName.java CheckingAccount_yourLastName.java SavingAccount_yourLastName.java -Add data structure...

  • please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank...

    please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much! public abstract class BankAccount {    //declare the required class variables    private double balance;    private int num_deposits;    private int num_withdraws;    private double annualInterest;    private double serviceCharges;       //constructor that takes two arguments    // one is to initialize the balance and other    // to initialize the annual interest rate       public BankAccount(double balance,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT