Download presentation
Presentation is loading. Please wait.
Published byJennifer Thomas Modified over 9 years ago
1
Constructing Objects Lab
2
Log into Wiley Plus
3
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]
4
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…..
5
….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
6
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]
7
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]
8
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
9
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?
10
Name in a Class Diagram
11
What are the Attributes?
12
What are the methods
13
Where is the constructor and are there any overloads?
14
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”
15
To add parameters: Click on Properties with the desired constructor highlighted
16
Go to Parameters and add the name of the parameter, the datatype and kind
17
Class diagram completed
18
Remember there are 2 classes The BankAccount and the BankAccountTester
19
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
20
Now ask yourself… How do these 2 classes interact? What type of diagram will help me understand how they interact? – Sequence Diagram
21
Put in the object lifelines for each of the objects; there is no instance name for the tester
22
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
23
Just like a carburetor cant run all by itself and must live in a car to work; a class needs to live in a program Tester class Instance of a class AKA an object
24
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.
25
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.
26
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.
27
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.
28
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.
29
Fifth: Get the balance
30
And print it
31
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.
32
Now you have a picture of the program and it is time to code.
33
Launch LabRat and make the file BankAccountTester.java
34
Construct a new bank account object
35
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]
36
Withdraw
37
withdraw
38
Get balance and assign to a variable if you don’t remember how to declare and add value to a variable go back and look at the API lecture
39
Print balance
40
Print expected result
41
Now submit to LabRat and see what happens.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.