• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects and Classes

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Okay I'm stumped, I'm working on an assignment that asks me to write a class named account and a client program to test the account class. In the program I'm to create an Account object with an account ID of 1122, a balance of 20,000 and an annual interest rate of 4.5%. Then I'm to withdraw $2500, deposit $3000 and calculate the monthly interest rate on the final balance. I'm suppose to use the withdraw method and deposit method to do the above as well. So I've got my program to compile but I'm getting a "NoSuchMethodError: Main. So I don't know what else to do. Below is the code I have wrote. I'm not looking for someone to debug it for me, what I need is help on what I should be looking for because I really don't know.
Thanks in advance for your help.
Stacey
public class TestAccountClass {
/**states the main method*/
public static void main(String [] args) {

//Create an object named account
Account account = new Account (1122, 20000, 4.5);
//***Not to sure if I created the object correct. Still a little shaky one how and why its done.

account.setID(1122);
account.setBalance(20000);
account.setAnnualInterestRate(.045);

System.out.print("Account ID: " +account.getID());
System.out.print("\n");
System.out.print("Current Balance: " +account.getBalance());
System.out.print("\n");
System.out.print("Annual Interest Rate: " +account.getAnnualInterestRate());
System.out.print("\n");
System.out.print("With a withdrawl of $2,500 and a deposit of $3,000,");
System.out.print("\n");
System.out.print("the final account balance is: "+account.getBalance());
System.out.print("\n");
System.out.print("The monthly interest is:" +account.monthlyInterest());


}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;

public Account() {
}

//Create account object
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}

//Return ID
public int getID() {
return id;
}
//Set a new ID
public void setID (int id) {
this.id = id;
}

//Return Balance
public double getBalance() {
return balance;
}

//Set a new Balance
public void setBalance (double balance) {
this.balance = balance;
}

//Return Annual Interest Rate
public double getAnnualInterestRate() {
return annualInterestRate;
}

//Set a new Annual Interest Rate
public void setAnnualInterestRate (double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}

public double finalBalance() {
double finalBalance = balance ;
return (balance - 2500) + 3000;
}

public double monthlyInterest() {
double monthlyInterestRate = annualInterestRate/1200;
return balance * monthlyInterestRate;
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, not to belabor the obvious, but "java Account" will give you this error, but "java TestAccountClass" shouldn't. What are you trying to execute that's giving this error?
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm not too sure what you are trying to say. From what I understand, TestAccountClass is my main class in my program, the class Account is created within the program. I'm trying to execute the whole program to run. And display my output. My program compiles but it won't run. I get the error.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try running "java TestAccountClass" on the command line.
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stacey Johnson:
Hi,
I'm not too sure what you are trying to say. From what I understand, TestAccountClass is my main class in my program, the class Account is created within the program. I'm trying to execute the whole program to run. And display my output. My program compiles but it won't run. I get the error.


Stacey, how are you trying to execute you app? You already know that you must use the 'java' command. The java command will usually take a class name (case-sensitive) to execute that class. That class must have an entry point called main() which is the main() method with its standard signature.
Since in you example, the main() method is in TestAccountClass, you must invoke 'java' with TestAccountClass as the argument to it.
In the main method you are correctly instantiating the Account object. You don't need to call the setID(), setBalance() & setAnnualInterestRate() methods since these values have already been passed via the contructor.
What you are missing in the Account class are the following methods:
withdraw(double amt)
deposit(double amt)
In the withdraw(double amt) method you subtract amt from from Account's balance. Similarly in deposit(double amt) you add amt to balance.
In you main method, after creating the Account object you call account.withdraw() & account.deposit() and pass the appropriate values to these methods.
You don't need the finalBalance() method since you already have getBalance(). getBalance() will return the current balance. The balance would have already been updated by the withdraw() & deposit() methods.
Hope this helped.
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi out there,
Thanks for all the help with this I think I'm finally understanding it (or starting too). :roll:
So I've changed the last part of my program upon suggestions to this:
public double withdraw (double amount) {
return balance - amount;
}

public double deposit (double amount){
return balance + amount;
}


public double monthlyInterest() {
double monthlyInterestRate = (annualInterestRate/1200);
return balance * monthlyInterestRate;
}
}
And I've got it up and running. Everything works accept for some reason it's not updating the new balance so I still display 20000 instead of 20500. Can anyone help me on this one?
Thanks Stacey
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stacey Johnson:

So I've changed the last part of my program upon suggestions to this:
public double withdraw (double amount) {
return balance - amount;
}

public double deposit (double amount){
return balance + amount;
}
}
And I've got it up and running. Everything works accept for some reason it's not updating the new balance so I still display 20000 instead of 20500. Can anyone help me on this one?
Thanks Stacey


Did you code your other two statements?
account.withdraw(2500);
account.deposit(3000);
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stacey Johnson:
Hi out there,
Thanks for all the help with this I think I'm finally understanding it (or starting too). :roll:
So I've changed the last part of my program upon suggestions to this:

And I've got it up and running. Everything works accept for some reason it's not updating the new balance so I still display 20000 instead of 20500. Can anyone help me on this one?
Thanks Stacey


Hi Stacey,
The reason your balance is not changing is that your withdraw, deposit and monthlyInterest methods are not changing the value of the class attribute "balance". You are simply returning the value of the result of your equations (balance + amount and balance � amount). You never assign the new value to balance.
So you must not only calculate (and return the balance), but also assign it to the class attribute. For example:

which can be abbreviated:

The parentheses around the equation are not required, but they do make the code easier to read and it easier to understand what�s happening. This can further be abbreviated (if you are familiar with the += and -= and *= notations) as:

This new method will not only return the new balance, but set it as well so any getBalance methods will return an accurate amount. You will need to do the same type of thing to your deposit and monthyInterst methods.
Regards,
Mark
[ February 04, 2004: Message edited by: Mark Vender ]
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stacey Johnson:
Hi out there,
Thanks for all the help with this I think I'm finally understanding it (or starting too). :roll:
So I've changed the last part of my program upon suggestions to this:
public double withdraw (double amount) {
return balance - amount;
}

public double deposit (double amount){
return balance + amount;
}


public double monthlyInterest() {
double monthlyInterestRate = (annualInterestRate/1200);
return balance * monthlyInterestRate;
}
}
And I've got it up and running. Everything works accept for some reason it's not updating the new balance so I still display 20000 instead of 20500. Can anyone help me on this one?
Thanks Stacey


Others have already pointed out the reason for this bug. I've just 2 points to add/mention:
1. Use the code tags to enclose your code in. It makes it easier to read & format.
2. The deposit & withdraw methods can have a void return; they don't have to return the new balance. Returning the new balance from these methods doesn't hurt either. It is a design decision. The reason I mention this is to make sure that you understand that a method doesn't have to return a value. You can just as well write:

If you want to add a little more feature to this:
Think about what you want to do if the amount to withdraw > balance?
 
Stacey Johnson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just like to thank everyone for their help. My program works like a charm and I acually have a pretty good idea why which I is nice too!
Thanks so much, this is a great sight for beginners, it's nice when people out there care enough to help others out.
Stacey!
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic