 |
| Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 4th, 2006, 03:18 PM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
can you help me find the errors with the code
package myPackage;
/**
*
* @author Robin
*/
public class Account {
private int id;
private double balance;
private double annualInterestRate;
/** Creates a new instance of Account */
public Account() {
this(1122, 20000, 4.5);
}
public Account (int id ,double balance, double annualInterestRate){
this.id = id;
this.balance=balance;
this.annualInterestRate=annualInterestRate ;
}
public int getId(){
return id;
}
public double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setId(int id){
this.id=id;
}
public void setBalance(double balance){
this.balance=balance;
}
public void setAnnualInterestRate (double annualInterestRate) {
this.annualInterestRate=annualInterestRate;
}
public double getMonthlyInterestRate(){
return (annualInterestRate/12.0);
}
public void withdraw(double amount){
this.balance=getBalance()-amount;
}
Account account= new Account();
public void deposite(double amount){
this.balance=getBalance()+ amount;
}
}
/*
* UseOfAccount.java
*
* Created on 2006Ãê3ÃÃ25ÃÃ, ÃÃÃç8:49
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package myPackage;
import javax.swing.*;
public class UseOfAccount {
public static void main( String[] args){
String idBox= JOptionPane.showInputDialog(null,"enter your id","idSet",JOptionPane.INFORMATION_MESSAGE);
int id= Integer.parseInt(idBox) ;
String balanceBox= JOptionPane.showInputDialog(null,"enter your balance","idSet",JOptionPane.INFORMATION_MESSAGE);
double balance= Double.parseDouble(balanceBox) ;
String annualInterestRateBox= JOptionPane.showInputDialog(null,"enter your annoullll","idSet",JOptionPane.INFORMATION_MESSAGE );
double annualInterestRate= Double.parseDouble(annualInterestRateBox) ;
Account account= new Account(id,balance,annualInterestRate);
String output="the balance is" +account.getBalance()+"\nThe monthly interest is "+ account.getMonthlyInterestRate();
System.out.println(output);
}
}
there is something wrong with the code above, if i delete the red line , the codes will be fine.but i don't know why the code will cause loop back all the time, someone tell me the loop caused at the beginning that jvm will initial all the variables of the code, someone tell me public Account() {
this(1122, 20000, 4.5);
}
call
public Account (int id ,double balance, double annualInterestRate){
when the code run to the red line , it will cause loop back , and i don't know which one is right , can you help me?
Thank you!
|
|

April 6th, 2006, 03:42 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Let me know in which method u have placed the line :- Account account= new Account();
|
|

April 6th, 2006, 02:56 PM
|
|
Authorized User
|
|
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
To me, that red line isn't necessary in the code for it to function in the way you seem to intend it to.
Why do you think that line is necessary?
Many shoes,
Jamez/SiliconFuRy
|
|

April 7th, 2006, 06:56 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
in fact , i don't know what is the meaning of the code , i don't know what the application can do , i just write as my book , but i have made so serious mistakes, thanks for your reply.
Robin
|
|

May 8th, 2006, 05:01 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thrown when a stack overflow occurs because an application recurses too deeply.
robin
since u call a constructor of account in UseOfAccount.java it calls the constructor which has arguments which in turn calls a 0 argument constructor account which inturn calls another 0 argument constructor and so on since it continues this java stack overflows and u get error instead of output and when u remove the red line the 0 argument constructor is not called and ur program executes fine
hav i answered ur question
vinay
Thanks & Regards
vinay
|
|

May 19th, 2006, 02:54 AM
|
|
Registered User
|
|
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I agree with vinary.
But what I want to make up is never to make an instance outside a method or constructor.
|
|

June 4th, 2006, 06:04 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks, vinary and zhangwujian , i have understood that , but new question come : is there a way that i can get the integer from the method whose return type is boolean?
|
|

June 15th, 2007, 07:18 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You cannot convert boolean to integer. May be if you could tell what exactly u r trying to do we could find an alternate way.
Thanks & Regards
vinay
|
|
 |