datalogi a 2: 15/9. java slides based on horstmann chapter 2&3 objects and classes import,...

28
Datalogi A 2: 15/9

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Datalogi A 2: 15/9

Page 2: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

JavaSlides based on Horstmann chapter 2&3

Objects and classes

Import, methods, references

Implementing a class

Page 3: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Variable declaration typeName variableName = value;

or typeName variableName;

Example:

 String greeting = "Hello, Dave!";

Purpose:

To define a new variable of a particular type and optionally supply an initial value

Page 4: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Identifiers• Identifier: name of a variable, method, or class • Rules for identifiers in Java:

– Can be made up of letters, digits, and the underscore (_) character

– Cannot start with a digit – Cannot use other symbols such as ? or % – Spaces are not permitted inside identifiers – You cannot use reserved words – They are case sensitive

• By convention, variable names start with a lowercase letter

• By convention, class names start with an uppercase letter

Page 5: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Objects• Object: entity that you can manipulate in your

programs (by calling methods)

• Each object belongs to a class. For example, System.out belongs to the class PrintStream

Page 6: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Methods• Methods• Method: Sequence of instructions that accesses

the data of an object • You manipulate objects by calling its methods • Class: Set of objects with the same behavior • Class determines legal methodsString greeting = "Hello";greeting.println() // Errorgreeting.length() // OK

Page 7: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

String Methods

length: counts the number of characters in a string String greeting = "Hello, World!";

int n = greeting.length(); // sets n to 13

toUpperCase: creates another String object that contains the characters of the original string, with lowercase letters converted to uppercase

String river = "Mississippi";String bigRiver = river.toUpperCase();

// sets bigRiver to "MISSISSIPPI"

When applying a method to an object, make sure method is defined in the appropriate class

System.out.length(); // This method call is an error

Page 8: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Implicit and Explicit Parameters Parameter (explicit parameter): Input to a method. Not all

methods have explicit parameters.System.out.println(greeting) greeting.length() // has no explicit parameter

Implicit parameter: The object on which a method is invokedSystem.out.println(greeting)

Page 9: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Passing Return Values You can also use the return value as a parameter of

another method:System.out.println(greeting.length());

Not all methods return values. Example: println

Page 10: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Example: RectangleA Rectangle object isn't a rectangular

shape–it is an object that contains a set of numbers that describe the rectangle

Page 11: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Constructing Objects new Rectangle(5, 10, 20, 30)

Detail: The new operator makes a Rectangle object It uses the parameters (in this case, 5, 10, 20, and 30) to

initialize the data of the object It returns the object

Usually the output of the new operator is stored in a variable

Rectangle box = new Rectangle(5, 10, 20, 30);

Page 12: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Rectangular shapes

Page 13: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Object Construction new ClassName(parameters)

Example:  new Rectangle(5, 10, 20, 30)

new Rectangle()

Purpose:

To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object

Page 14: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Accessor and Mutator Methods Accessor method: does not change the state of its implicit

parameterdouble width = box.getWidth();

Mutator method: changes the state of its implicit parameterbox.translate(15, 25);

Page 15: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Importing a Class from a Package  import packageName.ClassName;

Example:

 import java.awt.Rectangle;

Purpose:

To import a class from a package for use in a program.

Page 16: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

MoveTester.java import java.awt.Rectangle; public class MoveTester{ public static void main(String[] args){ Rectangle box = new Rectangle(5, 10, 20, 30);

// Move the rectangle box.translate(15, 25);

// Print information about the moved rectangle System.out.println("After moving,”); System.out.println("the top-left corner is:"); System.out.println(box.getX()); System.out.println(box.getY()); }}

Page 17: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Java API documentation

Page 18: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Rectangle Class

Page 19: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Object referencesMultiple object variables can refer to the same object

Rectangle box = new Rectangle(5, 10, 20, 30);Rectangle box2 = box;box2.translate(15, 25);

Page 20: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Copying numbersint luckyNumber = 13;

int luckyNumber2 = luckyNumber;

luckyNumber2 = 12;

Page 21: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Implementing a class• A black box magically does its thing • Hides its inner workings • Encapsulation: the hiding of unimportant details • What is the right concept for each particular

black box? • Concepts are discovered through abstraction • Abstraction: taking away inessential features,

until only the essence of the concept remains • In object-oriented programming the black boxes

from which a program is manufactured are called objects

Page 22: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Class Definition accessSpecifier class ClassName { constructors methods fields }Example: public class BankAccount{

public BankAccount(double initialBalance) { . . . }public void deposit(double amount) { . . . }. . .

}Purpose:To define a class, its public interface, and its

implementation details

Page 23: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Method Definition accessSpecifier returnType methodName(parameterType parameterName, . . .)

{ method body

}

Example:public void deposit(double amount){

. . .

}

Purpose:

To define the behavior of a method

Page 24: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Constructor Definition  accessSpecifier ClassName(parameterType parameterName, . . .)

{ constructor body }

Example: public BankAccount(double initialBalance)

{ . . . }

Purpose:

To define the behavior of a constructor

Page 25: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Instance Fields • An object stores its data in instance fields • Field: a technical term for a storage location inside a

block of memory • Instance of a class: an object of the class • The class declaration specifies the instance fields:

public class BankAccount{     . . .   private double balance;}

Page 26: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

Instance Fields

Page 27: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

BankAccount.java public class BankAccount{ private double balance; BankAccount(){ balance = 0; } BankAccount(double initialBalance){ balance = initialBalance; } public void deposit(double amount){ balance = balance + amount; } void withdraw(double amount){ balance = balance - amount; } double getBalance(){ return balance;} }

Page 28: Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class

BankAccountTester.java public class BankAccountTester{ public static void main(String[] args){ BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance()); }}

Prints out

1500