Programming - Java Programming Language - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Java Programming Language which includes Applet Class, Passing Parameters to Applets, Conversions, Applications and Applets, Running a Program, Applet, Application, Mouse Event, Keyboard Event etc. Key important points are: Programming, Objects and Classes, Programming Concepts, Declaring and Creating Objects, Constructors, Modifiers, Instance, Class Variables, Methods, Analyzing Relationships

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 44

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
Programming with Objects and Classes
OO Programming Concepts
Declaring and Creating Objects
Constructors
Modifiers (public, private and static)
Instance and Class Variables and Methods
Analyzing Relationships among Classes
Case Studies (Mortgage class and Rational class)
The Java API and Core Java classes
Processing Strings
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download Programming - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 5

Programming with Objects and Classes

  • OO Programming Concepts
  • Declaring and Creating Objects
  • Constructors
  • Modifiers (public, private and static)
  • Instance and Class Variables and Methods
  • Analyzing Relationships among Classes
  • Case Studies (Mortgage class and Rational class)
  • The Java API and Core Java classes
  • Processing Strings

OO Programming Concepts

data field 1

method n

data field n

method 1

An object

...

States

Behaviors

Data Field radius = 5 Method findArea A Circle object

Class Declaration

class Circle {

// states

double radius = 1.0;

// behaviors

double findArea() {

return radiusradius3.14159;

Declaring Objects

ClassName objectName;

Example:

Circle myCircle;

Declaring/Creating Objects

in a Single Step

ClassName objectName = new ClassName();

Example:

Circle myCircle = new Circle();

Differences between variables of primitive types and object types 1 c: Circle radius = 5 Primitive type int i = 1 Object type Circle c reference Created using new Circle(5)

Accessing Objects

• Referencing the object’s data:

objectName.data

myCircle.radius

• Referencing the object’s method:

objectName.method

myCircle.findArea()

Example 5.

Using Objects

• Objective: Demonstrate creating objects,

accessing data, and using methods.

TestCircle Run

Example 5.

Using Constructors

• Objective: Discuss the role of

constructors and use them to create

objects.

TestCircleWithConstructors Run

Passing Objects to Methods

• Passing by reference

• Passing by value

Example 5.3 Passing Objects as

Arguments

TestPassingObject Run

Example 5.

Using the private Modifier and

Accessor Methods

TestCircleWithPrivateModifier Run

In this example, private data are used for

the radius and the accessor methods

getRadius and setRadius are provided for

the clients to retrieve and modify the

radius.

Instance Variables, and

Methods

Instance variables belong to a specific

instance.

Instance methods are invoked by an

instance of the class.

Class Variables, Constants,

and Methods, cont.

Circle

  • radius
  • numOfObjects +getRadius +setRadius +getNumOfObjects +findArea 1 circle1:Circle radius
  • radius = 1
  • numOfObjects = 2 instantiate instantiate Memory 2 5 radius numOfObjects radius is an instance variable, and numOfObjects is a class variable Notation: +: public variables or methods -: private variables or methods underline: static variables or metods circle2:Circle
  • radius = 5
  • numOfObjects = 2

UML notation

(Unified Modeling

language)

Example 5.

Using Instance and Class

Variables and Method

Objective: Demonstrate the roles of

instance and class variables and

their uses. This example adds a

class variable numOfObjects to track

the number of Circle objects created.

TestInstanceAndClassVariable Run