Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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
 
Old April 4th, 2006, 03:18 PM
Registered User
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ousys Send a message via Yahoo to ousys
Default 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!


 
Old April 6th, 2006, 03:42 AM
Authorized User
 
Join Date: Mar 2006
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Let me know in which method u have placed the line :- Account account= new Account();





 
Old April 6th, 2006, 02:56 PM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

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
 
Old April 7th, 2006, 06:56 AM
Registered User
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ousys Send a message via Yahoo to ousys
Default

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

 
Old May 8th, 2006, 05:01 AM
Authorized User
 
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default



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
 
Old May 19th, 2006, 02:54 AM
Registered User
 
Join Date: May 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I agree with vinary.
But what I want to make up is never to make an instance outside a method or constructor.

 
Old June 4th, 2006, 06:04 AM
Registered User
 
Join Date: Apr 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ousys Send a message via Yahoo to ousys
Default

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?

 
Old June 15th, 2007, 07:18 AM
Authorized User
 
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing code and errors dba BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 January 15th, 2008 01:34 PM
Program Code Errors ed123 Beginning PHP 0 September 8th, 2007 03:53 AM
Chapter 4 Example Code Errors beckerm13 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 September 28th, 2006 03:40 PM
Printing Errors / Example Code Fire BOOK: Access 2003 VBA Programmer's Reference 0 April 12th, 2005 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.