Docsity

Understanding User-Defined Classes & Constructors in Java Programming - Prof. Donald Adjer, Papers of Computer Science

Toggle sidebar
bg1
1
Java Programming: From Problem Analysis to Program Design, Second Edition 1
Chapter 8: User-Defined Classes
๎šŒClasses - introduction.
๎šŒProtection and modifiers
๎šŒClass implementation.
๎šŒOperations on classes.
๎šŒConstructors and finalizers.
๎šŒThe method toString.
๎šŒAbstract data types (ADT).
Java Programming: From Problem Analysis to Program Design, Second Edition 2
Classes
๎šŒclass: A reserved word; a collection of a
fixed number of components.
๎šŒComponents: Members of a class.
๎šŒMembers are accessed by name.
๎šŒClass categories/modifiers:
โ€“ private
โ€“ protected
โ€“ public
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Understanding User-Defined Classes & Constructors in Java Programming - Prof. Donald Adjer and more Papers Computer Science in PDF only on Docsity!

Java Programming: From Problem Analysis to Program Design, Second Edition 1

Chapter 8: User-Defined Classes

ยŒ Classes - introduction. ยŒ Protection and modifiers ยŒ Class implementation. ยŒ Operations on classes. ยŒ Constructors and finalizers. ยŒ The method toString. ยŒ Abstract data types (ADT).

Java Programming: From Problem Analysis to Program Design, Second Edition 2

Classes

ยŒ class: A reserved word; a collection of a fixed number of components. ยŒ Components: Members of a class. ยŒ Members are accessed by name. ยŒ Class categories/modifiers:

  • private
  • protected
  • public

Java Programming: From Problem Analysis to Program Design, Second Edition 3

Classes

ยŒ Private: Members of class are not accessible outside class. ยŒ Public: Members of class are accessible outside class. ยŒ Class members: Can be methods or variables. ยŒ Variable members are declared like any other variables.

Java Programming: From Problem Analysis to Program Design, Second Edition 4

Syntax

The general syntax for defining a class is:

ยŒ If a member of a class is a named constant, you declare it just like any other named constant. ยŒ If a member of a class is a variable, you declare it just like any other variable. ยŒ If a member of a class is a method, you define it just like any other method. ยŒ If a member of a class is a method, it can (directly) access any member of the classโ€”data members and methods. Therefore, when you write the definition of a method (of the class), you can directly access any data member of the class (without passing it as a parameter).

Java Programming: From Problem Analysis to Program Design, Second Edition 7

class Clock: Constructors ยŒ Default constructor is public Clock().

Constructor with parameters: ยŒ public Clock(int hours, int minutes,int seconds)

Constructors

Java Programming: From Problem Analysis to Program Design, Second Edition 8

Unified Modeling Language Class Diagrams

Java Programming: From Problem Analysis to Program Design, Second Edition 9

Variable Declaration and Object Instantiation

The general syntax for using the operator new is: new className() OR new className(argument1, argument2, ..., argumentN)

Clock myClock; Clock yourClock; myClock = new Clock(); yourClock = new Clock(9, 35, 15);

Java Programming: From Problem Analysis to Program Design, Second Edition 10

Variable Declaration and Object Instantiation

Java Programming: From Problem Analysis to Program Design, Second Edition 13

Assignment Operator: A Precaution

ยŒ Shallow copying: Two or more reference variables of the same type point to the same object. ยŒ Deep copying: Each reference variable refers to its own object.

Java Programming: From Problem Analysis to Program Design, Second Edition 14

The Copy Constructor

ยŒ Executes when an object is instantiated. ยŒ Initialized using an existing object. ยŒ Syntax: public ClassName(ClassName otherObject)

Java Programming: From Problem Analysis to Program Design, Second Edition 15

Example: class Clock

Java Programming: From Problem Analysis to Program Design, Second Edition 16

The Method toString

ยŒ Public value-returning method. ยŒ Takes no parameters. ยŒ Returns address of a String object. ยŒ Output using print, println, printf methods. ยŒ Default definition creates String with name of objectโ€™s class name followed by hash code of object.

Java Programming: From Problem Analysis to Program Design, Second Edition 19

void setX(int a) { x = a; } public String toString() { return("x = " + x + ", y = " + y

  • ", count = " + count); } public static void incrementY() { y++; } }

Illustrate illusObject = new Illustrate(); Illustrate.incrementY(); Illustrate.count++;

The Modifier static

Java Programming: From Problem Analysis to Program Design, Second Edition 20

Illustrate illusObject1 = new Illustrate(3); Illustrate illusObject2 = new Illustrate(5);

The Modifier static

Java Programming: From Problem Analysis to Program Design, Second Edition 21

Illustrate.incrementY(); Illustrate.count++;

The Modifier static

Java Programming: From Problem Analysis to Program Design, Second Edition 22

Finalizers

ยŒ Automatically execute when class object goes out of scope. ยŒ Have no parameters. ยŒ Only one finalizer per class. ยŒ Name of finalizer: finalize.

Java Programming: From Problem Analysis to Program Design, Second Edition 25

Creating Package for class Clock

package jpfpatpd.ch08.clockPackage; public class Clock { //put instance variables and methods, //as before, here }

import jpfpatpd.ch08.clockPackage.Clock;

Java Programming: From Problem Analysis to Program Design, Second Edition 26

The Reference this

ยŒ Refers to instance variables and methods of a class. ยŒ Used to implement cascaded method calls.

Java Programming: From Problem Analysis to Program Design, Second Edition 27

Inner Classes

ยŒ Defined within other classes. ยŒ Can be either a complete class definition or an anonymous inner class definition. ยŒ Used to handle events.

Java Programming: From Problem Analysis to Program Design, Second Edition 28

Abstract Data Types

A data type that specifies the logical properties without the implementation details.

Java Programming: From Problem Analysis to Program Design, Second Edition 31

Programming Example:

Candy Machine

Components: ยŒ Cash register ยŒ Dispenser ยŒ Machine

Java Programming: From Problem Analysis to Program Design, Second Edition 32

Programming Example:

Candy Machine

Java Programming: From Problem Analysis to Program Design, Second Edition 33

Programming Example:

Candy Machine

Java Programming: From Problem Analysis to Program Design, Second Edition 34

Programming Example:

Candy Machine

Java Programming: From Problem Analysis to Program Design, Second Edition 37

Programming Example:

Candy Machine

Java Programming: From Problem Analysis to Program Design, Second Edition 38

Chapter Summary

ยŒ Creating classes ยŒ Members of a class: ยŒ private ยŒ protected ยŒ public ยŒ static ยŒ Implementing classes ยŒ Various operations on classes ยŒ Constructors ยŒ Finalizers ยŒ Method toString ยŒ Abstract data types

Java Programming: From Problem Analysis to Program Design, Second Edition 39

Chapter Summary