Java Reference
In-Depth Information
public class Account {
}
3.
Add two instance variables to the
Account
class:
String name;
BigDecimal amount;
4.
Add a constructor method to set up new
Account
objects. Your constructor should accept two
String parameters, one for the name and another for the starting amount of the account. Since the
amount variable is a
BigDecimal
and represents currency, set the scale to two decimal points and
ROUND_HALF_UP
for the rounding mode. Your constructor should look like this:
public Account(String acctName, String startAmount) {
this.name = acctName;
this.amount = new BigDecimal(startAmount);
this.amount.setScale(2, BigDecimal.ROUND_HALF_UP);
}
5.
Add another new class called
AccountManager
. This time, check the box that creates the
main
method for you (you can also write the
main
method yourself after creating the class). Click Finish
to create the class. Your
AccountManager
class should look something like this:
public class AccountManager {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
6.
Remove the
// TODO
comment, and add a statement inside the
main
method that creates a new
account with the name
FirstAccount
and amount
10.00
.
7.
Add
print
statements to your
main
method so the user is given information about the account that
you created. Your
main
method should look like this:
public static void main(String[] args) {
Account myAccount = new Account("FirstAccount","10.00");
System.out.println("Account Created: " + myAccount.name);
System.out.println("Balance: " + myAccount.amount);
}
8.
Now add another statement to change the amount in
myAccount
to
-15.00
. Print this new balance
to the console. Your
main
method should look similar to this:
public static void main(String[] args) {
Account myAccount = new Account("FirstAccount","10.00");
System.out.println("Account Created: " + myAccount.name);
System.out.println("Balance: " + myAccount.amount);
Search WWH ::
Custom Search