Looking for some examples in Java programming using object oriented examples to
ID: 3577827 • Letter: L
Question
Looking for some examples in Java programming using object oriented examples to help study for my final Below is what the instructor is looking for: The exam will be a coding exam where you will create a bank account class, The BankAccount class will have: an accountNumber variable (int), a balance variable (double), firstName variable as a String lastName variable as a String. I will give you the specifications in class, however there is enough above to create all your "getter" and "setter" methods plus a couple of constructors. Plus... drop a main method in your class so you can test it.
Explanation / Answer
Hi, Please find my implementation of BankAccount class.
Please let me know in case of any issue.
public class BankAccount {
// instance variables
private int accountNumber;
private double balance;
private String firstName;
private String lastName;
// constructors
public BankAccount(int accNo, String firstName, String lastName, double bal) {
accountNumber= accNo;
this.firstName = firstName;
this.lastName = lastName;
balance= bal;
}
public BankAccount(int accountNo){
accountNumber = accountNo;
firstName = "";
lastName = "";
balance = 0.0;
}
// setters and getters
public int getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void withdraw(double amount){
if(amount > balance){
System.out.println("Insufficient Balance!!");
return;
}
balance = balance - amount;
}
public void deposite(double amount){
balance = balance +amount;
}
@Override
public String toString() {
return "Account Number: "+accountNumber+" "+
"First Name: "+firstName+" "+
"Last Name: "+lastName+" "+
"Balance: $"+balance+" ";
}
public static void main(String[] args) {
// creating BankAccount Object
BankAccount acc = new BankAccount(1212343);
acc.setFirstName("Pravesh");
acc.setLastName("Kumar");
acc.deposite(5432.34);
System.out.println(acc);
acc.withdraw(2000);
System.out.println(acc);
}
}
/*
Sample run:
Account Number: 1212343
First Name: Pravesh
Last Name: Kumar
Balance: $5432.34
Account Number: 1212343
First Name: Pravesh
Last Name: Kumar
Balance: $3432.34
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.