Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
Create a simple program that models a bank account. A bank account can be seen as a class that has feil sucj as balance, interest rate, owner name, and methods such as deposit.
- Define a constructor that assigns value to the field of the class.
- Create an object called account1 that has a balance of 1000, an interest of 0.05, an owner name of "Alice."
- Deposit 100$ into account1.
Task2
- In the bank account program, create an object called account2 that has
- Owner name "Bob"
- Balance is given via keyboard input
- Interest rate given via keyboard input (i.e. user enters 50, meaning interest rate is 50%,
so you have to add a conversion for proper calculation
- Define methods for withdraw, transfer, and display information
- Withdraw 200$ from account2.
- transfer 300$ from account1 to account2
- Display both accounts information after previous changes.
public class BankAccount {
// Declare the fields of the class
private double balance;
private double interestRate;
private String ownerName;
// Define the constructor of the class that takes three parameters
public BankAccount(double balance, double interestRate, String ownerName) {
// Assign the values of the parameters to the attributes
this.interestRate = interestRate;
this.ownerName = ownerName;
this.balance = balance;
}
// Define a method to deposit money into the account
public void deposit(double amount) {
this.balance +=amount;
}
// Define a method to withdraw money from the account
public void withdraw(double amount) {
this.balance += amount;
if (amount <= balance) {
// Subtract the amount from the balance//
// TODO: Add your code here
this.balance -= amount;
// Print a message to confirm the withdrawal
} else {
// Print a message to indicate insufficient funds
}
}
// Define a method to transfer money from one account to another
public void transfer(BankAccount other, double amount) {
// Check if the amount is less than or equal to the balance
if (amount <= balance) {
// Subtract the amount from the balance of this account
TODO: Add your code here
// Add the amount to the balance of the other account
// Print a message to confirm the transfer
System.out.println("You have transferred " + amount + " from your account to " + other.ownerName
+ "'s account.");
} else {
// Print a message to indicate insufficient funds
System.out.println("You do not have enough money in your account.");
}
}
// Define a method to display the information of the account
public void display() {
// Print the owner name, balance, and interest rate of the account
System.out.println("Owner name: " + ownerName);
System.out.println("Balance: " + balance);
System.out.println("Interest rate: " + interestRate);
}
public static void main(String[] args) {
// Create an object called account1 with initial values
TODO: Add your code here
// Create another object called account2 with different values
// Display the information of both accounts
// Deposit 100 into account1
// Withdraw 200 from account2
// Transfer 300 from account1 to account2
// Display the information of both accounts again
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team