From Problem Descriptions to Classes

 

From the Description

In Java…

1.  What is the name of the class you are creating:

     __BankAccount_____________ (ex. Rectangle)

 

public class _ BankAccount ____________

{

(ex. public class Rectangle)

2.  What are the attributes (nouns)?

 

Name

Type

(ex. length)

(ex. double)

Account number

String

balance

Double

 

 

 

//instance variables   

 

(ex.private double myLength;)

 

    private String  myAccountNumber;

    private double myBalance;

3.  What are the initializers (how do you create an object)?

 

Parameters (arguments)

What does it do

(ex. length, width)

(ex. Initializes length and width)

Account number, balance

Initialize acct num and balance

Account number

Initialize the account number and balance = 100

 

 

 

//constructors

 

(ex. public Rectangle(double length, double width))

 

    public BankAccount(String acctNum, double balance)

    public BankAccount(String acctNum)

 

 

4.  What are the behaviors (verbs)?

 

Name

Returns

Parameters (arguments)

What does it do

(ex. perimeter)

(ex. double)

(ex.)

(ex. calculate the perimeter)

Withdraw

 

void

amount

Subtract amount from balance

deposit

 

void

amount

Add amount to balance

getBalance

 

double

 

Returns balance

 

 

 

//methods   

 

 

 

(ex.//add amount to balance

    public double perimeter())

    

    public void withdraw(double amount)

   

    public void deposit(double amount)

 

    public double getBalance()

 

 

5.  Write the java code filling in the constructors & methods. 

 

6.  Write a main method to test our your code.

    public static void main(String[] args)

    {

   (ex. Rectangle rec = new Rectangle(5, 4);

        System.out.println(rec.perimeter());

    }