How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Raj Your Own Question

Raj
Raj, Computer Engg.
Category: Programming
Satisfied Customers: 1282
Experience:  MS CS, 5+ Experience in Programming and Database (ERP)
16752232
Type Your Programming Question Here...
Raj is online now
A new question is answered every 9 seconds

/** * BankAccount class * This class simulates a bank account.

This answer was rated:

/** * BankAccount class
* This class simulates a bank account.
*
* (Taken from "Starting Out with Java - Early Objects
* (Third Edition) by Tony Gaddis, 2008 by Pearson Educ.)
*
*/

public class BankAccount
{
private double balance; // Account balance
private double interestRate; // Interest rate
private double interest; // Interest earned
/**
* The constructor initializes the balance
* and interestRate fields with the values
* passed to startBalance and intRate. The
* interest field is assigned to 0.0.
*/
public BankAccount(double startBalance,
double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}

/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/

public void withdraw(double amount)
{
balance -= amount;
}
/**
* The addInterest method adds the interest
* for the month to the balance field.
*/
public void addInterest()
{
interest = balance * interestRate;
balance += interest;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterest method returns the
* value in the interest field.
*/
public double getInterest()
{
return interest;
}
}
/**
*
*
*
* This program demonstrates the BankAccount class.
*
* (Taken from "Starting Out with Java - Early Objects
* (Third Edition) by Tony Gaddis, 2008 by Pearson Educ.)
*
*
*
* Date: June 19, 2010
*
*/

import java.util.Scanner; // Needed for the Scanner class
import java.text.DecimalFormat; // Needed for 2 decimal place amounts

public class Program2

{
public static void main(String[] args)
{
BankAccount account; // To reference a BankAccount object
double balance, // The account's starting balance
interestRate, // The annual interest rate
pay, // The user's pay
cashNeeded; // The amount of cash to withdraw

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat ("#0.00");

// Get the starting balance.
System.out.print("What is your account's " +
"starting balance? ");
balance = keyboard.nextDouble();

// Get the monthly interest rate.
System.out.print("What is your monthly interest rate? ");
interestRate = keyboard.nextDouble();

// Create a BankAccount object.
account = new BankAccount(balance, interestRate);

// Get the amount of pay for the month.
System.out.print("How much were you paid this month? ");
pay = keyboard.nextDouble();

// Deposit the user's pay into the account.
System.out.println("We will deposit your pay " +
"into your account.");
account.deposit(pay);
System.out.println("Your current balance is $" +
formatter.format( account.getBalance() ));

// Withdraw some cash from the account.
System.out.print("How much would you like " +
"to withdraw? ");
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);

// Add the monthly interest to the account.
account.addInterest();

// Display the interest earned and the balance.
System.out.println("This month you have earned $" +
formatter.format( account.getInterest() ) +
" in interest.");
System.out.println("Now your balance is $" +
formatter.format( account.getBalance() ) );
}
}
Compile the two test files (BankAccount.java first and then Program2.java second). Execute Program2 with the following inputs:
starting balance - $500 (don't enter the dollar sign)
monthly interest rate - 0.00125 (this is a 1.5% annual rate)
monthly pay - $1000 (don't enter the dollar sign)
withdrawal amount - $900 (don't enter the dollar sign)
Verify that you earn $0.75 in interest and have an ending balance at the end of the month of $600.75.
Then modify the BankAccount class's constructor method to create a BankAccount object which stores a monthly interest when the user inputs an annual interest rate of the format "nnn.nn" (i.e. 1.5). Note that the BankAccount constructor stored a monthly interest rate for the BankAccount object's instance field originally, but the user had to convert the annual rate to a monthly rate (i.e. 1.5 to 0.00125). Then modify the Program2 driver class to prompt the user for an annual interest rate. Recompile both classes and execute the modified Program2 driver class again, this time with following inputs:
starting balance - $500 (don't enter the dollar sign)
annual interest rate - 1.5
monthly pay - $1000 (don't enter the dollar sign)
withdrawal amount - $900 (don't enter the dollar sign)
Verify that you still earn $0.75 in interest and still have an ending balance at the end of the month of $600.75 as you did with the original code.

Raj :

Hi Again

Raj :

I will be helping you with the code.

Raj :

By When do you need the solution file?

Customer:

hello again

Customer:

within 5 hours

Raj :

Ok I will submit the code in next 5 hrs. So do come back after 5 hrs and i will keep your code ready by then

Raj :

when you come back do post a message here so that i get notified that you are here

Customer:

will do

Customer:

see you then

Raj :

Ok

Customer:

ok I'm back

Customer:

Hi

Raj :

I am still working on it

Customer:

oh ok

Raj :

If I give you in another 1 hr. Will that be fine with you?

Customer:

yeah i can do that

Raj :

And I am really sorry for the delay

Customer:

no worries 1 hr is cool

Raj :

ok

Raj :

you just need the change for second part right?

Customer:

modify the bankaccount class

Customer:

yes

Raj :

yaa so thats the second part to get Annual Intrest rate

Raj :

Should I post code here or the link to java files/

Customer:

which ever is best

Raj :

Its on your convineance

Customer:

ok , post will work

Raj :

So you need code directly here? Right?

Customer:

yes that is fine

Raj :

Ok there here are the two class files

Raj :

First I am posting BankAccount.java one

Raj :

/** * BankAccount class * This class simulates a bank account. * * (Taken from "Starting Out with Java - Early Objects *


(Third Edition) by Tony Gaddis, 2008 by Pearson Educ.) * */


public class BankAccount
{
private double balance; // Account balance
private double interestRate; // Interest rate
private double interest; // Interest earned


/** * The constructor initializes the balance * and interestRate fields with the values * passed to startBalance and intRate.


The * interest field is assigned to 0.0. */


public BankAccount(double startBalance, double intRate)
{
balance = startBalance;
interestRate = (intRate) / (12 * 100);
interest = 0.0;
}


/** * The deposit method adds the parameter * amount to the balance field. */
public void deposit(double amount)
{
balance += amount;
}


/** * The withdraw method subtracts the * parameter amount from the balance * field. */
public void withdraw(double amount)
{
balance -= amount;
}


/** * The addInterest method adds the interest * for the month to the balance field. */
public void addInterest()
{
interest = balance * interestRate;
balance += interest;
}


/** * The getBalance method returns the * value in the balance field. */
public double getBalance()
{
return balance;
}


/** * The getInterest method returns the * value in the interest field. */
public double getInterest()
{
return interest;
}


}

Raj :

//-----------------------------------------------------------------------------------------------------------------------------------------------

Raj :

IN Above you will change the assignment of interestRate in Constructor

Raj :

Got it?

Raj :

Sorry its you will see the change is the assignment of interestRate in Constructor

Customer:

..im comparing th two..i see

Raj :

Ok

Raj :

And the other file i.e. Program2.java is as follows:

Raj :

/** * * * * This program demonstrates the BankAccount class. * * (Taken from "Starting Out with Java - Early Objects * (Third


Edition) by Tony Gaddis, 2008 by Pearson Educ.) * * * * Date: June 19, 2010 * */


import java.util.Scanner; // Needed for the Scanner class
import java.text.DecimalFormat; // Needed for 2 decimal place amounts


public class Program2
{
public static void main(String[] args)
{
BankAccount account; // To reference a BankAccount object
double balance, // The account's starting balance interestRate,
// The annual interest rate pay,
// The user's pay cashNeeded;
// The amount of cash to withdraw


// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);


// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat ("#0.00");
// Get the starting balance.
System.out.print("What is your account's " + "starting balance? ");
balance = keyboard.nextDouble();
// Get the Annual interest rate.
System.out.print("What is your annual interest rate? ");
interestRate = keyboard.nextDouble();
// Create a BankAccount object.
account = new BankAccount(balance, interestRate);
// Get the amount of pay for the month.
System.out.print("How much were you paid this month? ");
pay = keyboard.nextDouble();
// Deposit the user's pay into the account.
System.out.println("We will deposit your pay " + "into your account.");
account.deposit(pay);
System.out.println("Your current balance is $" + formatter.format( account.getBalance() ));


// Withdraw some cash from the account.
System.out.print("How much would you like " + "to withdraw? ");
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);


// Add the monthly interest to the account.
account.addInterest();
// Display the interest earned and the balance.
System.out.println("This month you have earned $" + formatter.format( account.getInterest() ) + " in interest.");


System.out.println("Now your balance is $" + formatter.format( account.getBalance() ) );
}
}

Raj :

//-------------------------------------------------------------------------------------------------------------------------------------------------------

Raj :

In Above case i.e. Program2.java the only change you will see is Instead of asking user for Monthly interest rate now it asks for Annual Interest rate

Customer:

got it, the thing that threw me was the bankaccount class

Raj :

Ok No Problem

Customer:

let me compile

Raj :

ok

Customer:

not compiling

Customer:


BankAccount2.java:4: class BankAccount is public, should be declared in a file named BankAccount.java



ÏϧÏpublic class BankAccount
ÏÏ§Ï ^
ÏϧÏ1 error

Customer:

c

Customer:

1

Customer:

one sec, gotta rename

Raj :

ok

Customer:

got it

Customer:

thank you

Customer:

here comes the accept :)

Customer:

i've got 6 more weeks , we'll be talking again real soon lol

Customer:

again, thank you very much

Raj and other Programming Specialists are ready to help you