codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Highschool Java  baoduy at 03:39 on Wednesday, October 12, 2005
 

Hello everybody I just entered this programming class in my high school, and I'm totally lost.

They're doing Class and Objects right now.

I would like to get help on this one problem:

Develop and implement a class Account that represents a savings account. An Account object should have three attributes: the name of the account, the owner of the account, and the current balance. The class should provide the following constructors behaviors.

Name(): configures a new account with name "name", owner "owner", and balance 0.
Name(String s, String t, double n): configures a new account with name s, owner t, and balance n.
void deposit(double n): adds amount n to the current balance of the associated account.
void withdraw(double n): subtracts amound n from the current balance of the associated account.
void close():zeroes out the account balance, sets the owner to "".
String getAccountName(): returns the name of the associated account.
String getOwner(): returns the owner of the associated account.
double getBalance():returns the balance of the associated account.
void setName(String s): sets the name of the associated account to s.
void setOwner(String s): sets the owner of the associated account to s.
String toString(): returns a textual representation of the attributes of the associated account.

Besides that the teacher doesn't want anyone to add anything into the code, just what it says up there.

  Re: Highschool Java  crwood at 19:31 on Wednesday, October 12, 2005
 


public class AccountTest
{
public static void main(String[] args)
{
Account account1 = new Account();
System.out.println(account1);
Account acct = new Account("John Adams", "John Adams", 100.0);
acct.deposit(1000.0);
System.out.println("acct balance = " + acct.getBalance());
acct.close();
System.out.println(acct);
Account freds = new Account("freds party account", "Fred Smith", 50.0);
System.out.println("name = " + freds.getAccountName() + "\n" +
"owner = " + freds.getOwner() + "\n" +
"balance = " + freds.getBalance());
freds.withdraw(2.59);
System.out.println(freds);
}
}

class Account
{
String name;
String owner;
double balance;

Account()
{
name = "name";
owner = "owner";
balance = 0;
}

Account(String s, String t, double n)
{
name = s;
owner = t;
balance = n;
}

void deposit(double n)
{
balance = balance + n;
}

void withdraw(double n)
{
balance = balance - n;
}

void close()
{
owner = "";
balance = 0;
}

String getAccountName()
{
return name;
}

String getOwner()
{
return owner;
}

double getBalance()
{
return balance;
}

void setName(String s)
{
name = s;
}

void setOwner(String s)
{
owner = s;
}

public String toString()
{
return "name: " + name + ", owner: " + owner + ", balance: " + balance;
}
}


  Re: Highschool Java  baoduy at 04:16 on Thursday, October 13, 2005
 

But
I'm not sure why my teacher wants me to have a special method (Name) that is really just doing the job of a constructor.

(I may have missed out
on some java thinking here...or is he planning on using Reflection later ?)

  Re: Highschool Java  crwood at 18:21 on Thursday, October 13, 2005
 

I read it as meaning the name of your class. Constructors have the same name as their class. You know they are constructors because they have the same name and no return type/declaration in their signature.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Date script issues
•  perl script help needed
•  onChange issue
•  perl remote execution
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007