Question

In: Computer Science

JAVA LANGUAGE Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You...

JAVA LANGUAGE

Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You need to create two classes, one for BankAccount (BankAccount.java) and the other for the tester (BankAccountTest.java).
Make sure your program compile without errors before submitting. Submit .java files to eCampus by the end of next Thursday, 10/15/2020.

Part 1: Create the BankAccount class (template is attached after the project description) in the project. You must add the following to the BankAccount class:

An instance variable for bank account number: accountNum
The long getAccountNum() method to get the account number
The void setAccountNum(long acctnum) method to set the account number
An instance variable for bank account owner’s name: name
The String getAccountName() method to get the account owner’s name
The void setAccountName(String acctname) method to set the account owner’s name
Another instance variable for account overdrawn status: overdrawn
The boolean isOverdrawn() method to get the account overdrawn status
The void setOverdrawn(boolean status) method to set the account overdrawn status
A constructor with four parameters: name, account number, the initial balance, and the overdrawn status to set the initial values for the instance variables when an object is created
Modify the withdraw() and deposit() methods
An account will be overdrawn if a withdrawal results in a negative balance.
An account that was overdrawn will not be overdrawn anymore if a deposit results in a positive balance or 0.
The deposit and withdraw methods must verify the amount to deposit (or to withdraw). If the amount is negative, the methods must printout "Negative amount!" and the balance of the account will not be changed.
Add a method called transfer(double amount, BankAccount otherAccount)
The transfer method is to transfer some money from the account to another account specified by otherAccount.
Add a method addInterest ( ) to add interest to the account and change the balance
use a constant to represent the interest rate 3.5%
Add the toString() method to represent an account in the following format:
“The Account Number xxx is owned by xxx with a Balance xxx. It is (not) overdrawn.”
Here xxx needs to replace by the values of the account. If it is overdrawn, show   
overdrawn. Otherwise, show not overdrawn.

Part 2: Create a BankAccountTest class with only the main method in the BankAccount project to test the BankAccount class. You must add the following to the BankAccountTest class:

Create a BankAccount object named harrysChecking with the initial values: name as "Harry", accoutNum as 12345, balance as 0.00, and overdrawn as false
Print out the object harrysChecking by calling toString() method
Call the deposit method to deposit $200 to harrysChecking and then print out the current balance and overdrawn status of harrysChecking
Call the withdraw method to withdraw $500 from harrysChecking and then print out the current balance and overdrawn status of harrysChecking
Again call the deposit method to deposit $400 to harrysChecking and then print out the current balance and overdrawn status of harrysChecking
Create a BankAccount object named harrysSaving with the initial values: name as "Harry", accoutNum as 86754, balance as 500.00, and overdrawn as false
Print out the object harrysSaving by calling toString() method
Call the transfer method to transfer $50 from harrysChecking to harrysSaving
Print out the current balances of harrysChecking and harrysSaving, respectively
Call the addInterest method to add interest to harrysSaving and Print out information for harrysSaving by calling toString() method

/**
A bank account has a balance that can be changed by deposits,
withdrawals, transfers, and interests.
*/
public class BankAccount {

   private double balance;
  
  
   /**
   Constructs a bank account with a zero balance
   */
   public BankAccount() {
       balance=0;
   }
  
   /**
   Constructs a bank account with a given balance
   @param initialBalance the initial balance
   */
   public BankAccount(double initialBalance) {
       balance=initialBalance;
   }
  
   /**
   Deposits money into the bank account.
   @param amount the amount to deposit
   */
   public void deposit (double amount) {
       balance=balance+amount;
   }
  
   /**
   Withdraws money from the bank account.
   @param amount the amount to withdraw.
   */
   public void withdraw (double amount) {
       balance=balance-amount;
   }
  
   /**
   Gets the current balance of the bank account.
   @return the current balance
   */
   public double getBalance() {
       return balance;
   }
}

public class BankAccountTest {

   public static void main(String[] args) {

   }
}

Solutions

Expert Solution

Java program with comments

BankAccount.java

/**
A bank account has a balance that can be changed by deposits,
withdrawals, transfers, and interests.
*/
public class BankAccount {

private double balance;
//An instance variable for bank account number: accountNum
private long accountNum;
//An instance variable for bank account owner’s name: name
private String name;
//Another instance variable for account overdrawn status: overdrawn
private boolean overdrawn;

//use a constant to represent the interest rate 3.5%
public static final double RATE = 3.5;
  
/**
Constructs a bank account with a zero balance
*/
public BankAccount() {
balance=0;
}
  
/**
Constructs a bank account with a given balance
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance) {
balance=initialBalance;
}
  
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit (double amount) {
       //The deposit and withdraw methods must verify the amount to deposit (or to withdraw).
//If the amount is negative, the methods must printout "Negative amount!" and the balance of the account will not be changed.
   if(amount <= 0)
   {
       System.out.println("Negative amount!");
       return;
   }
balance=balance+amount;
//An account that was overdrawn will not be overdrawn anymore if a deposit results in a positive balance or 0.
if(isOverdrawn() && balance >= 0)
   setOverdrawn(false);

}
  
/**
Withdraws money from the bank account.
@param amount the amount to withdraw.
*/
public void withdraw (double amount) {
   //The deposit and withdraw methods must verify the amount to deposit (or to withdraw).
//If the amount is negative, the methods must printout "Negative amount!" and the balance of the account will not be changed.
   if(amount <= 0)
   {
       System.out.println("Negative amount!");
       return;
   }
     
     
balance=balance-amount;
//An account will be overdrawn if a withdrawal results in a negative balance.
if(balance < 0)
   setOverdrawn(true);
}
  
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance() {
return balance;
}

//The long getAccountNum() method to get the account number
long getAccountNum()
{
   return accountNum;
}
//The void setAccountNum(long acctnum) method to set the account number
void setAccountNum(long ac)
{
   accountNum = ac;
}

//The String getAccountName() method to get the account owner’s name
String getAccountName()
{
   return name;
}
//The void setAccountName(String acctname) method to set the account owner’s name
void setAccountName(String acctname)
{
   name = acctname;
}

//The boolean isOverdrawn() method to get the account overdrawn status
boolean isOverdrawn()
{
   return overdrawn;
}

//The void setOverdrawn(boolean status) method to set the account overdrawn status
void setOverdrawn(boolean status)
{
   overdrawn = status;
}

//A constructor with four parameters: name, account number, the initial balance, and the overdrawn status
//to set the initial values for the instance variables when an object is created
BankAccount(String n, long acn, double bal, boolean status)
{
   setAccountName(n);
   setAccountNum(acn);
   balance = bal;
   setOverdrawn(status);
}

//Add a method called transfer(double amount, BankAccount otherAccount)
void transfer(double amount, BankAccount otherAccount)
{
//The transfer method is to transfer some money from the account to another account specified by otherAccount.
   withdraw(amount);
   otherAccount.deposit(amount);     
}

//Add a method addInterest ( ) to add interest to the account and change the balance
void addInterest()
{
   double interest;
   interest = RATE / 100 * getBalance();
   deposit(interest);
}

//Add the toString() method to represent an account in the following format:
   // “The Account Number xxx is owned by xxx with a Balance xxx. It is (not) overdrawn.”
   // Here xxx needs to replace by the values of the account. If it is overdrawn, show   
   // overdrawn. Otherwise, show not overdrawn.
public String toString()
{
   String result="";
   result += "The Account Number ";
   result += getAccountNum();
   result += " is owned by ";
   result += getAccountName();
   result += " with a Balance ";
   result += getBalance();
   result += ". It is ";
   if(!isOverdrawn())
       result += "(not) ";
   result += "overdrawn.";
   return result;
}

}

BankAccountTest.java


public class BankAccountTest {

public static void main(String[] args) {
     
   //Create a BankAccount object named harrysChecking with the initial values:
   //name as "Harry", accoutNum as 12345, balance as 0.00, and overdrawn as false
   BankAccount harrysChecking = new BankAccount("Harry", 12345, 0.00, false);
     
   //Print out the object harrysChecking by calling toString() method
   System.out.println(harrysChecking.toString());
     
   //Call the deposit method to deposit $200 to harrysChecking and
   //then print out the current balance and overdrawn status of harrysChecking
   harrysChecking.deposit(200);
   System.out.println("Current balance: "+ harrysChecking.getBalance());
   System.out.println("Overdrawn Status: "+ harrysChecking.isOverdrawn());
     
   //Call the withdraw method to withdraw $500 from harrysChecking and then
   //print out the current balance and overdrawn status of harrysChecking
   harrysChecking.withdraw(500);
   System.out.println("Current balance: "+ harrysChecking.getBalance());
   System.out.println("Overdrawn Status: "+ harrysChecking.isOverdrawn());
     
   //Again call the deposit method to deposit $400 to harrysChecking and then
   //print out the current balance and overdrawn status of harrysChecking
   harrysChecking.deposit(400);
   System.out.println("Current balance: "+ harrysChecking.getBalance());
   System.out.println("Overdrawn Status: "+ harrysChecking.isOverdrawn());
     
   //Create a BankAccount object named harrysSaving with the initial values:
   //name as "Harry", accoutNum as 86754, balance as 500.00, and overdrawn as false
   BankAccount harrysSaving = new BankAccount("Harry", 86754, 500.00, false);

     
   //Print out the object harrysSaving by calling toString() method
   System.out.println(harrysSaving.toString());

   //Call the transfer method to transfer $50 from harrysChecking to harrysSaving
   harrysChecking.transfer(50, harrysSaving);
   //Print out the current balances of harrysChecking and harrysSaving, respectively
   System.out.println("Current balance harrysChecking: "+ harrysChecking.getBalance());
   System.out.println("Current balance harrysSaving: "+ harrysSaving.getBalance());

     
   //Call the addInterest method to add interest to harrysSaving and Print out information for harrysSaving by calling toString() method
   harrysSaving.addInterest();
   System.out.println(harrysSaving.toString());

}
}

Sample I/O and Output:


Related Solutions

Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
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();...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a...
Create a new Java project named Program 4 Three Dimensional Shapes. In this project, create a package named threeDimensional and put all 3 of the classes discussed below in this package Include a header comment to EACH OF your files, as indicated in your instructions. Here's a link describing the header. Note that headers are not meant to be in Javadoc format. Note that Javadoc is a huge part of your grade for this assignment. Javadoc requires that every class,...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT