JAVA HELP PLEASE Exercise 1 Part 1 Create a class SavingsAcc
[JAVA HELP PLEASE]
Exercise 1
Part 1
Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a driver program to test the class SavingsAccount. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 5% and calculate the next months interest and print the new balances for each of the savers.
Part 2
Write another class SpecialSavings that extends SavingsAccount to pay interest of 10% on accounts that have balances that exceed 10K. Also provided methods to deposit and take money out of savings account. Write a driver program to test the class SpecialSavings. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Make a few deposits and withdrawals and show balance and interest earned for each account.
Some Tips for helping you with Lab 4
For Lab 4 part 1 -
Pl. follow the instructions in the assignment and write the class definition. A driver program is the code you would write in main() to exercise the code of class definition.
You should declare private variable in your class definition. You should also write protected methods to set and get the value of private variables.
For Exercise 1 part 2 -
You need to write deposit and withdrawal methods. Should you put this in parent class or child class? Try to answer this question so that you have most reusability in your class definition.
In this part - you have to learn how polymorphism works.
You will have accounts whose balance might be above or below 10K. By using methods in both classes try to change the interest earned to 10% if balance is above 10K or 4% if the interest is lower.
Solution
import java.util.Scanner;
import java.util.Random;
public class ATM {
public static void main(String[] args){
// Declaring variables, Scanner and Random objects
String name;
double initial;
Scanner input = new Scanner(System.in);
Random numero = new Random();
int number = 1 + numero.nextInt(9999);
//Getting the initial values
System.out.println(\"Registering a new client.\");
System.out.print(\"Type your name: \");
name = input.nextLine();
System.out.print(\"Enter the initial amount of this account: \");
initial = input.nextDouble();
//Creating the new account
Bank myAccount = new Bank(name, number, initial);
myAccount.start();
}
}
Bank.java
import java.util.Scanner;
public class Bank {
private String name;
private int number, withdrawals;
private double balance;
Scanner input = new Scanner(System.in);
public Conta(String name, int number, double initialBalance){
this.name=name;
this.number=number;
balance=initialBalance;
withdrawals=0;
}
public void bankStatement(){
System.out.println(\"\\tBank Statement\");
System.out.println(\"Name: \" + this.name);
System.out.println(\"Account number: \" + this.number);
System.out.printf(\"Balance: %.2f\ \",this.balance);
System.out.println(\"Withdrawals made: \" + this.withdrawals + \"\ \");
}
public void cashOut(double value){
if(balance >= value){
balance -= value;
withdrawals++;
System.out.println(\"Cash value out: \" + value);
System.out.println(\"New balance: \" + balance + \"\ \");
} else {
System.out.println(\"Not enough balance amount. Deposit some value.\ \");
}
}
public void deposit(double value)
{
balance += value;
System.out.println(\"Deposit: \" + value);
System.out.println(\"New balance: \" + balance + \"\ \");
}
public void start(){
int option;
do{
showMenu();
option = input.nextInt();
chooseOption(opcao);
}while(opcao!=4);
}
public void showMenu(){
System.out.println(\"\\t Choose the right option\");
System.out.println(\"1 - Check bank statement\");
System.out.println(\"2 - Cash out\");
System.out.println(\"3 - Deposit\");
System.out.println(\"4 - Exit\ \");
System.out.print(\"Option: \");
}
public void chooseOption(int option){
double value;
switch( option ){
case 1:
bankStatement();
break;
case 2:
if(withdrawals<3){
System.out.print(\"How much do you want cash out: \");
value = input.nextDouble();
cashOut(value);
} else{
System.out.println(\"Withdrawals dayly limit reached.\ \");
}
break;
case 3:
System.out.print(\"How much do you want deposit: \");
value = input.nextDouble();
deposit(value);
break;
case 4:
System.out.println(\"Closed system.\");
break;
default:
System.out.println(\"Invalid option\");
}
}
}
Alternatively you can have the following as one class program
import java.util.Scanner;
public class BankTransaction {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num;
double balance = 0;
double amount;
do {
System.out.println(\"Type Number\");
System.out.println(\"1. Deposit\");
System.out.println(\"2. Withdrawal\");
System.out.println(\"3. Balance\");
System.out.println(\"4. Exit\");
num = scan.nextInt();
if (num == 1) {
System.out.println(\"Enter amount to deposit: \");
amount = scan.nextDouble();
// Add the amount to the balance
balance += amount;
System.out.println(\"Your balance is\");
System.out.println(balance);
} else if (num == 2) {
System.out.println(\"Enter amount to withdrawal: \");
amount = scan.nextDouble();
// Remove the amount from the balance
balance -= amount;
System.out.println(\"Your balance is\");
System.out.println(balance);
} else if (num == 3) {
System.out.println(\"Your Balance\");
System.out.println(balance);
}
} while (num != 4);
System.out.println(\"Good Bye!\");
}
}
![[JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj [JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj](/WebImages/23/java-help-please-exercise-1-part-1-create-a-class-savingsacc-1055333-1761550496-0.webp)
![[JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj [JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj](/WebImages/23/java-help-please-exercise-1-part-1-create-a-class-savingsacc-1055333-1761550496-1.webp)
![[JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj [JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj](/WebImages/23/java-help-please-exercise-1-part-1-create-a-class-savingsacc-1055333-1761550496-2.webp)
![[JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj [JAVA HELP PLEASE] Exercise 1 Part 1 Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each obj](/WebImages/23/java-help-please-exercise-1-part-1-create-a-class-savingsacc-1055333-1761550496-3.webp)