AN INTRODUCTION TO OBJECTS AND CLASSES - PowerPoint PPT Presentation

1 / 59
About This Presentation
Title:

AN INTRODUCTION TO OBJECTS AND CLASSES

Description:

Rectangle Class. Construct a rectangle: new Rectangle(5, 10, 20, 30) new Rectangle ... Invent a new class, say MoveTest. Supply a main method. Place ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 60
Provided by: Facu148
Category:

less

Transcript and Presenter's Notes

Title: AN INTRODUCTION TO OBJECTS AND CLASSES


1
CHAPTER 2
  • AN INTRODUCTION TO OBJECTS AND CLASSES

2
Chapter Goals
  • To understand the concepts of classes and objects
  • To realize the difference between objects and
    object references
  • To become familiar with the process of
    implementing classes
  • To be able to implement simple methods
  • To understand the purpose and use of constructors
  • To understand how to access instance fields and
    local variables
  • To appreciate the importance of documentation
    comments

3
Objects and Classes
  • Object entity that you can manipulate in your
    programs (by invoking methods)
  • Each object belongs to a class
  • Class Set of objects with the same behavior
  • Class determines legal methods"Hello".println()
    // Error"Hello".length() // OK

4
Rectangle Class
  • Construct a rectanglenew Rectangle(5, 10, 20,
    30)new Rectangle()
  • Use the constructed objectSystem.out.println(new
    Rectangle(5, 10, 20, 30))prints
    java.awt.Rectanglex5,y10,width20,height30

5
Rectangle Shapes
6
A Rectangle Object
7
Syntax 2.1 Object Construction
  • new ClassName(parameters)
  • Example
  • new Rectangle(5, 10, 20, 30)
  • new Car("BMW 540ti", 2004)
  • Purpose
  • To construct a new object, initialize it with the
    construction parameters, and return a reference
    to the constructed object.

8
Object Variables
  • Declare and optionally initializeRectangle
    cerealBox new Rectangle(5, 10, 20,
    30)Rectangle crispyCrunchy
  • Apply methodscerealBox.translate(15, 25)
  • Share objectsr cerealBox

9
Uninitialized and Initialized Variables
Uninitialized
Initialized
10
Two Object Variables Referring to the Same Object
11
Syntax 2.2 Variable Definition
  • TypeName variableName
  • TypeName variableName expression
  • Example
  • Rectangle cerealBox String name "Dave"
  • Purpose
  • To define a new variable of a particular type
  • and optionally supply an initial value

12
Writing a Test Program
  • Invent a new class, say MoveTest
  • Supply a main method
  • Place instructions inside the main method
  • Import library classes by specifying the package
    and class nameimport java.awt.Rectangle
  • You don't need to import classes in the java.lang
    package such as String and System

13
Syntax 2.3 Importing a Class from a Package
  • importpackageName.ClassName
  • Example
  • import java.awt.Rectangle
  • Purpose
  • To import a class from a package for use in a
    program

14
File MoveRect.java
  • 1 import java.awt.Rectangle
  • 2
  • 3 public class MoveTest
  • 4
  • 5 public static void main(String args)
  • 6
  • 7 Rectangle cerealBox new Rectangle(5,
  • 10, 20, 30)
  • 8 // move the rectangle
  • 9 cerealBox.translate(15, 25)
  • 10 // print the moved rectangle
  • 11 System.out.println(cerealBox)
  • 12

15
A Simple Class
  • public class Greeter public String
    sayHello() String message
    "Hello,World!" return message

16
Method Definition
  • access specifier (such as public)
  • return type (such as String or void)
  • method name (such as sayHello)
  • list of parameters (empty for sayHello)
  • method body in

17
Method Parameters
  • public class Rectangle . . . public void
    translate(int x, int y) method body
    . . .

18
Syntax 2.4 Method Implementation
  • public class ClassName ...
    accessSpecifier returnType methodName(parameterTyp
    e parameterName,...) method body
    ...
    Continue

19
  • Continue
  • Example
  • public class Greeter public String
    sayHello() String message
    "Hello,World!" return message
  • Purpose
  • To define the behavior of a method A method
    definition specifies the method name, parameters,
    and the statements for carrying out the method's
    actions

20
Syntax 2.5 The return Statement
  • return expression
  • or
  • return
  • Example
  • return message
  • Purpose
  • To specify the value that a method returns, and
  • exit the method immediately. The return value
  • becomes the value of the method call
  • expression.

21
Testing a Class
  • Test class a class with a main method that
    contains statements to test another class.
  • Typically carries out the following steps
  • Construct one or more objects of the class that
    is being tested.
  • Invoke one or more methods.
  • Print out one or more results

22
A Test Class for the Greeter Class
  • public class GreeterTest
  • public static void main(String args))
    Greeter worldGreeter new Greeter()
  • System.out.println(worldGreeter.sayHello())

23
Building a Test Program
  • 1. Make a new subfolder for your program.
  • 2. Make two files, one for each class.
  • 3. Compile both files.
  • 4. Run the test program.

24
Testing with the SDK Tools
  • mkdir greeter
  • cd greeter
  • edit Greeter.java
  • edit GreeterTest.java
  • javac Greeter.java
  • javac GreeterTest.java
  • java GreeterTest

25
Testing with BlueJ
26
Instance Fields
  • public class Greeter
  • ...private String name
  • access specifier (such as private)
  • type of variable (such as String)
  • name of variable (such as name)

27
Instance Fields
28
Accessing Instance Fields
  • The sayHello method of the Greeter class can
    access the private instance fieldpublic String
    sayHello()String message "Hello, " name
    "!"return message

29
  • Other methods cannotpublic class
    GreeterTestpublic static void main(String
    args). . .System.out.println(daveGreeter.name)
    // ERROR
  • Encapsulation Hiding data and providing access
    through methods

30
Syntax 2.6 Instance Field Declaration
  • accessSpecifier class ClassName ...
    accessSpecifier fieldType fieldName ...

31
  • Example
  • public class Greeter ... private
    String name ...
  • Purpose
  • To define a field that is present in every object
    of a class

32
Constructors
  • A constructor initializes the instance variables
  • Constructor name class name
  • public class Greeter() public Greeter(String
    aName) name aName . . .
  • Invoked in new expressionnew Greeter("Dave")

33
Syntax 2.7 Constructor Implementation
  • accessSpecifier class ClassName ...
    accessSpecifier ClassName(parameterTypeparameterN
    ame ...) constructor implementation
    ...

34
  • Example
  • public class Greeter ... public
    Greeter(String aName) name aName
    ...
  • Purpose
  • To define the behavior of a constructor, which is
    used to initialize the instance fields of newly
    created objects

35
File Greeter.java
  • 1 public class Greeter
  • 2
  • 3 public Greeter(String aName)
  • 4
  • 5 name aName
  • 6
  • 7
  • 8 public String sayHello()
  • 9

36
  • 10 String message "Hello, " name "!"
  • 11 return message
  • 12
  • 13
  • 14 private String name
  • 15

37
File GreeterTest.java
  • 1 public class GreeterTest
  • 2
  • 3 public static void
  • main(String args)
  • 4
  • 5 Greeter worldGreeter new Greeter("World")
  • 6System.out.println(worldGreeter.sayHello())
  • 7

38
  • 8 Greeter daveGreeter new Greeter("Dave")
  • 9System.out.println(daveGreeter.sayHello())
  • 10
  • 11

39
Designing the Public Interface
  • Behavior of bank account
  • deposit money
  • withdraw money
  • get balance
  • Methods of BankAccount class
  • deposit
  • withdraw
  • getBalance

40
BankAccount Public Interface
  • public BankAccount()
  • public BankAccount(double initialBalance)
  • public void deposit(double amount)
  • public void withdraw(double amount)
  • public double getBalance()

41
Using the Public Interface
  • Transfer balance
  • double amt 500momsSavings.withdraw(amt)harry
    sChecking.deposit(amt)
  • Add interest
  • double rate 5 // 5double amt
    acct.getBalance() rate / 100acct.deposit(am
    t)

42
Commenting the Public Interface
  • / Withdraws money from the bank account.
    _at_param the amount to withdraw / public void
    withdraw(double amount) implementation filled
    in later

43
  • / Gets the current balance of the bank
    account. _at_return the current balance / public
    double getBalance() implementation filled in
    later

44
Class Comment
  • / A bank account has a balance that can
    be changed by deposits and withdrawals. /
    public class BankAccount ...

45
Javadoc Method Summary
46
Javadoc Method Detail
47
BankAccount Class Implementation
  • Determine instance variables to hold object
    stateprivate double balance
  • Implement methods and constructors

48
File BankAccount.java
  • 1 /
  • 2 A bank account has a balance that can be
    changed by
  • 3 deposits and withdrawals.
  • 4 /
  • 5 public class BankAccount
  • 6

49
  • 7 /
  • 8 Constructs a bank account with a zero balance
  • 9 /
  • 10 public BankAccount()
  • 11
  • 12 balance 0
  • 13
  • 14

50
  • 15 /
  • 16 Constructs a bank account with a given balance
  • 17 _at_param initialBalance the initial balance
  • 18 /
  • 19 public BankAccount(double initialBalance)
  • 20
  • 21 balance initialBalance
  • 22

51
  • 23
  • 24 /
  • 25 Deposits money into the bank account.
  • 26 _at_param amount the amount to deposit
  • 27 /
  • 28 public void deposit(double amount)
  • 29
  • 30 double newBalance balance amount
  • 31 balance newBalance
  • 32
  • 33

52
  • 34 /
  • 35 Withdraws money from the bank account.
  • 36 _at_param amount the amount to withdraw
  • 37 /
  • 38 public void withdraw(double amount)
  • 39
  • 40 double newBalance balance - amount
  • 41 balance newBalance
  • 42
  • 43

53
  • 44 /
  • 45 Gets the current balance of the bank account.
  • 46 _at_return the current balance
  • 47 /
  • 48 public double getBalance()
  • 49
  • 50 return balance
  • 51
  • 52
  • 53 private double balance
  • 54

54
File BankAccountTest.java
  • 1 /
  • 2 A class to test the BankAccount class.
  • 3 /
  • 4 public class BankAccountTest
  • 5
  • 6 /
  • 7 Tests the methods of the BankAccount class.
  • 8 _at_param args not used
  • 9 /

55
  • 10 public static void main(String args)
  • 11
  • 12 BankAccount harrysChecking new
    BankAccount()
  • 13 harrysChecking.deposit(2000)
  • 14 harrysChecking.withdraw(500)
  • 15 System.out.println(harrysChecking.getBalance())
  • 16
  • 17

56
Calling a Method in BlueJ
57
The Method Return Value in BlueJ
58
Variable Types
  • Instance fields (balance in BankAccount)
  • Local variables (newBalance in deposit method)
  • Parameter variables (amount in deposit method)

59
Explicit and Implicit Parameters
  • public void withdraw(double amount) double
    newBalance balance - amount balance
    newBalance
  • balance is the balance of the object to the left
    of the dot
  • momsSavings.withdraw(500)
  • means
  • double newBalance momsSavings.balance -
    amount momsSavings.balance newBalance
Write a Comment
User Comments (0)
About PowerShow.com