constructing objects lab. log into wiley plus read the problem write a bankaccounttester class whose...

41
Constructing Objects Lab

Upload: jennifer-thomas

Post on 31-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Constructing Objects Lab

Page 2: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Log into Wiley Plus

Page 3: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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]

Page 4: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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…..

Page 5: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

….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

Page 6: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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]

Page 7: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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]

Page 8: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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

Page 9: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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?

Page 10: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Name in a Class Diagram

Page 11: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

What are the Attributes?

Page 12: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

What are the methods

Page 13: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Where is the constructor and are there any overloads?

Page 14: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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”

Page 15: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

To add parameters: Click on Properties with the desired constructor highlighted

Page 16: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Go to Parameters and add the name of the parameter, the datatype and kind

Page 17: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Class diagram completed

Page 18: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Remember there are 2 classes

• The BankAccount and the BankAccountTester

Page 19: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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

Page 20: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Now ask yourself…

• How do these 2 classes interact?• What type of diagram will help me understand

how they interact?– Sequence Diagram

Page 21: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Put in the object lifelines for each of the objects; there is no instance name for the tester

Page 22: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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

Page 23: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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

Page 24: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 25: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 26: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 27: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 28: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 29: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Fifth: Get the balance

Page 30: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

And print it

Page 31: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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.

Page 32: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Now you have a picture of the program and it is time to code.

Page 33: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Launch LabRat and make the file BankAccountTester.java

Page 34: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Construct a new bank account object

Page 35: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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]

Page 36: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Withdraw

Page 37: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

withdraw

Page 38: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

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

Page 39: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Print balance

Page 40: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Print expected result

Page 41: Constructing Objects Lab. Log into Wiley Plus Read the problem Write a BankAccountTester class whose main method constructs a bank account, deposits

Now submit to LabRat and see what happens.