Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Show interest rate properly

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Show interest rate properly

    Hello everyone, how do i get my code to output "The annual interest rate is: 4.5" and not. "The annual interest rate is:0.045".
    Below i have attached my code thank you.

    import java.util.Date;

    public class Account {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Account account1 = new Account(1122, 20000.00, .045);
    account1.withdraw(2500);
    account1.deposit(3000);
    System.out.println("The balance is: " + account1.getBalance());
    System.out.println("The monthly interest is: " + account1.monthlyInterest());
    System.out.println("The monthly interest rate is: " + account1.getMonthlyInterestRate());
    System.out.println("The anunual interest rate is:" + account1.getAnnualInterestRate());


    }


    //define variables
    private double id;
    private double balance; // balance for account
    private double annualInterestRate; //stores the current interest rate
    private Date dateCreated; //stores the date account created

    //no arg construtor
    Account () {
    id = 0.0;
    balance = 0.0;
    annualInterestRate = 0.0;
    }
    //constructor with specific id and initial balance
    Account(double newId, double newBalance) {
    id = newId;
    balance = newBalance;
    }
    Account(double newId, double newBalance, double newAnnualInterestRate) {
    id = newId;
    balance = newBalance;
    annualInterestRate = newAnnualInterestRate;
    }
    //accessor/mutator methods for id, balance, and annualInterestRate
    public double getId() {
    return id;
    }
    public double getBalance() {
    return balance;
    }

    public double monthlyInterest() {
    return balance * getMonthlyInterestRate();
    }
    public double getAnnualInterestRate() {
    return annualInterestRate;
    }
    public void setId(int newId) {
    id = newId;
    }
    public void setBalance(double newBalance) {
    balance = newBalance;
    }
    public void setAnnualInterestRate(double newAnnualInterestRate) {
    annualInterestRate = newAnnualInterestRate;
    }
    //accessor method for dateCreated
    public void setDateCreated(Date newDateCreated) {
    dateCreated = newDateCreated;
    }
    //define method getMonthlyInterestRate
    double getMonthlyInterestRate() {
    return annualInterestRate/12;
    }
    //define method withdraw
    double withdraw(double amount) {
    return balance -= amount;
    }
    //define method deposit
    double deposit(double amount) {
    return balance += amount;
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default re: Show interest rate properly

    The annual interest rate is: 4.5" and not. "The annual interest rate is:0.045".
    Can you multiply 0.045 by a value to get 4.5?

    What is the definition of interest rate? For your example the return on $100 would be $4.50

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Interest Program
    By Roisin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2014, 02:57 AM
  2. Rate this code on a scale from 1 - 10 and please suggest some improvements.
    By BurningCa007 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: March 14th, 2013, 10:31 PM
  3. How to Compute Interest
    By salsera1908 in forum Object Oriented Programming
    Replies: 5
    Last Post: December 4th, 2012, 02:38 PM
  4. Dollar Rate
    By kanna39 in forum Java Theory & Questions
    Replies: 0
    Last Post: November 3rd, 2012, 05:50 AM
  5. Beginner: Show Image in Label when Results Show Up
    By Big Bundy in forum Java Theory & Questions
    Replies: 3
    Last Post: April 4th, 2011, 02:43 PM