Objects and Classes - Objects and Classes, take 2 Designing...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Objects and Classes, take 2 Designing a Class Think about the objects that will be created from the new class type. Think about: what the object knows what the object does Example: For an Alarm class - think about what an Alarm object knows and does Example: For a Car class - think about what a Car object knows and does Instance variables are an object's data, i.e., the things an object knows about itself. Things an object can do are called methods . Note: Often our classes contain methods that read or write
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
the values of the instance variables. Designing A Class, Start to Finish Example: Rectangle class Before you start coding, think about how the objects of your class should behave. What operations should a Rectangle be capable of, for example? area perimeter get current length get current width set current length set current width These operations that you chose should become methods in the class. What data do you want to store for each Rectangle object? length width
Background image of page 2
Example : BankAccount - write a class that represents a bank account 1. Decide what operations/methods you need What operations do you want to carry out with a bank account? get balance deposit money withdraw money If you have a BankAccount object called myChecking, you would like to be able to call methods like: myChecking.deposit(2000); // add $2000 to my checking account myChecking.withdraw(30); // get some cash System.out.println(myChecking.getBalance()); // let me see my current balance So the BankAccount class should contain the methods: o deposit
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
o withdraw o getBalance 2. Figure out what information each method needs to
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course C S 305j taught by Professor Staff during the Spring '08 term at University of Texas at Austin.

Page1 / 11

Objects and Classes - Objects and Classes, take 2 Designing...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online