Upload
jennifer-thomas
View
219
Download
1
Embed Size (px)
Citation preview
Constructing Objects Lab
Log into Wiley Plus
Read the problem
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
• Your tester class should be called BankAccountTester.
• Use the following class in your solution: [Wplus gives you the BankAccount class code here]
Now
• You probably feel overwhelmed.• You think: What do I do next? • You react: Somebody tell me what to do.• Professor reaction: take a deep breath, clear
your mind and remember…..
….what Eric Berlow said: “Hone in on the sphere of influence
that matters most.”
http://www.ted.com/talks/eric_berlow_how_complexity_leads_to_simplicity.html
What matters most? There are 2 classes
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
• Your tester class should be called BankAccountTester.
• Use the following class in your solution: [Wplus gives you the BankAccount class code here]
What matters about the BankAccountTester?You have to write it and BankAccountTester has a main method that
constructs a BankAccount and then invokes the methods of the Bank Account and then prints the results.
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
• Your tester class should be called BankAccountTester.
• Use the following class in your solution: [Wplus gives you the BankAccount class code here]
What matters about the BankAccount class?
• It is already written for you.
• You need to understand its structure {You gotta know what is in it.}
• How do we understand class structures?– Make a UML Class
Diagram
Writing a UML Class Diagram to reverse engineer code: Remember the 3 parts of Object Anatomy:
name, attributes, methods
• What is the name of the class?
Name in a Class Diagram
What are the Attributes?
What are the methods
Where is the constructor and are there any overloads?
Where does the constructor go in the class diagram?
• A constructor is a method that is called when you create a NEW object; the scope is “classifier” instead of “instance”
To add parameters: Click on Properties with the desired constructor highlighted
Go to Parameters and add the name of the parameter, the datatype and kind
Class diagram completed
Remember there are 2 classes
• The BankAccount and the BankAccountTester
What does the BankAccountTester class look like? What do the words in the problem tell us?
• “Write a BankAccountTester class whose main method”
• We know the name of the class and a method name
Now ask yourself…
• How do these 2 classes interact?• What type of diagram will help me understand
how they interact?– Sequence Diagram
Put in the object lifelines for each of the objects; there is no instance name for the tester
Why isn't there a instance name for the Tester?
• Because a tester class is a class with a main method that contains statements to run methods of another class
• It is the class that makes the calls to the instance of classes [objects]
• It will occupy space in memory when running but we will not create NEW
• The main method kicks off the program
Just like a carburetor cant run all by itself and must live in a car to work;
a class needs to live in a programTester class Instance of a class AKA an object
Back to the problem: what is the first thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
What is the second thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
What is the third thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
What is the fourth thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
What is the Fifth thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
• Question: is there a dependent operation that must be performed to print the balance?
• Answer: yes, you have to get the balance before you can print it.
Fifth: Get the balance
And print it
What is the sixth thing the tester does?
• Write a BankAccountTester class whose main method constructs a bank account, deposits $1,000, withdraws $500, withdraws another $400, and then prints the remaining balance. Also print the expected result.
Now you have a picture of the program and it is time to code.
Launch LabRat and make the file BankAccountTester.java
Construct a new bank account object
Make a deposit[if you don’t remember how to invoke a method go back to
object anatomy and look at the method ppt again]
Withdraw
withdraw
Get balance and assign to a variableif you don’t remember how to declare and add value to a variable go back
and look at the API lecture
Print balance
Print expected result
Now submit to LabRat and see what happens.