|
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.
|
|
|
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;
}
}
|
|
|
|
|
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 ?)
|
|
|
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.
|
|
|
|
|
|
|
// |