Java Constructor help -


//******************************************************* // account.java // // bank account class methods deposit to, withdraw from, // change name on, , string representation // of account. //******************************************************* import java.util.random; public class account {   private double balance;   private string name;   private long acctnum;    //----------------------------------------------   //constructor -- initializes balance, owner, , account number   //----------------------------------------------   public account(double initbal, string owner, long number)   {     balance = initbal;     name = owner;     acctnum = number;   }    //----------------------------------------------   // checks see if balance sufficient withdrawal.   // if so, decrements balance amount; if not, prints message.   //----------------------------------------------   public void withdraw(double amount)   {     if (balance >= amount)        balance -= amount;     else        system.out.println("insufficient funds");   } //---------------- //track how many accounts //----------------     private static int numaccounts=0;     {         numaccounts++;         }     public static int getnumaccounts()     {         return numaccounts;         }    //----------------------------------------------   // adds deposit amount balance.   //----------------------------------------------   public void deposit(double amount)   {     balance += amount;   }    //----------------------------------------------   // returns balance.   //----------------------------------------------   public double getbalance()   {     return balance;   } // name of account     public string getname()     {         return name;     }     //----------------------------------------------   // returns account number.   //----------------------------------------------    public long getacctnumber()   {     return acctnum;   }  //---------------- //void , close accounts //----------------      public void close() {     balance = 0;     name += "close";      numaccounts--;      }  //---------------- //consolidating accounts //----------------     public static account consolidate(account acct1,account acct2)     { account newaccount=null;         if((acct1.getname()).equals(acct2.getname()))         if(acct1.getacctnumber()!=acct2.getacctnumber())             {newaccount= new account(acct1.getbalance()+acct2.getbalance(),string owner);                          random generator = new random();             acctnum= generator.nextint();                 acct1.close();                 acct2.close();      }      else      system.out.println("not allow,same account number");      else      system.out.println("can't use other people account");      return newaccount;     }    //----------------------------------------------   // returns string containing name, account number, , balance.   //----------------------------------------------   public string tostring()   {     return "name: " + name +  "\naccount number: " + acctnum + "\nbalance: " + balance;    } } 

please @ //consolidate section. i'm trying is, consolidate acct1 , acct2 1 new account, restrictions acct1 , acct2 has has same name, acct1 , acct2 account number has different each other, , if met, create new account new balance 2 old account, keep same name , randomly generate new account number. there missing in code? wont compile. these errors got

      account.java:95: ')' expected                 {newaccount= new account(acct1.getbalance()+acct2.getbalance(),string owner);                                                                                      ^     account.java:95: illegal start of expression                 {newaccount= new account(acct1.getbalance()+acct2.getbalance(),string owner);                                                                                            ^  
share|improve question
    
next time please tag question homework if (this time i'll it) , format code it'll readable (use buttons above text box). – mbyd apr 11 '11 @ 20:02
    
this looks very similar your other question few hours ago (and, matter, your question hour before that). pick 1 edit updates, keep things in 1 place? – pops apr 11 '11 @ 20:03
    
yea it's same one, sorry, didnt know if should edited old 1 or make new one – tuan nguyen apr 11 '11 @ 20:08

string owner should acct1.getname() or whatever function retrieves name.

also, line acctnum = generator.nextint(); fail acctnum isn't defined in context. furthermore, don't set account number of newaccount acctnum variable.

i suggest change this:

newaccount.setacctnumber(generator.nextint());

share|improve answer
    
if newaccount.setacctnumber(generator.nextint()); wouldnt hace make setacctnumber in constructor? – tuan nguyen apr 11 '11 @ 20:06
    
not in constructor - rather, in body of account class declare method signature void setacctnumber(int number) {this.acctnumber = number;} – finbarr apr 11 '11 @ 21:24

the compile error @ line

newaccount= new account(acct1.getbalance()+acct2.getbalance(),string owner)

is because string owner declaration should used in method signature, did above. when make call constructor, though, need send string parameter in.

the compiler complaining because doing @ line declaring string variable named owner. java won't allow in method call.

finbarr right; use method name of account instead.

share|improve answer
    
what if want generate random number new consolidated account? couldn't figure how within section – tuan nguyen apr 11 '11 @ 20:14

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments