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: 1259
Experience:  BE CS, 4+ 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.

Resolved Question:

/** * 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.
Submitted: 3 years ago.
Category: Programming
Expert:  Raj replied 3 years ago.

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, Computer Engg.
Category: Programming
Satisfied Customers: 1259
Experience: BE CS, 4+ Experience in Programming and Database (ERP)
Raj and 7 other Programming Specialists are ready to help you

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
< Last | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer New York
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7463
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5603
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/unvadim/2010-11-15_210218_avatar.jpg unvadim's Avatar

    unvadim

    Computer Software Engineer

    Satisfied Customers:

    1158
    Good knowledge of OOP principles. 3+ years of programming experience with Java and C++. Sun Certified Java Programmer 5.0.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    950
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    864
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/RA/rajivsharma086/2012-6-6_17128_displaypic.64x64.jpg Raj's Avatar

    Raj

    Computer Engg.

    Satisfied Customers:

    860
    BE CS, 4+ Experience in Programming and Database (ERP)
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    590
    11 yrs of programming (PHP, WordPress, XSL, SQL, JavaScript)