Introduction to Classes and Objects - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Introduction to Classes and Objects

Description:

... object. Terminology. Method header / body. public String toString ... Dialog is dismissed, execution resumes. An Input Dialog. import javax.swing.JOptionPane; ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 36
Provided by: drtimm8
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Classes and Objects


1
Introduction to Classes and Objects
  • Dr. Tim Margush
  • University of Akron

2
Goals
  • Differentiate between classes and objects
  • Enumerate the basic components of a class
  • Define and use a custom class
  • Distinguish between primitive and reference types

3
What is a Class?
  • A description of the capabilities and attributes
    of an object
  • attributes, properties, fields, data members
  • methods, actions, messages
  • In Java, all classes are subordinate to the class
    Object
  • In Java, a class is sometimes called an object
    factory

4
What is an Object?
  • A block of memory holding an encoded
    representation of an instance of a class
  • In Java, objects are usually referred to by a
    reference variable
  • Reference variables contain address information
    specifying the location of the encoded data
    representing the object

5
Class Essentials
  • Access type
  • Class name
  • Parent name
  • Attributes
  • Methods
  • Documentation

6
Defining the Human Class
  • //A pretty useless class
  • public class Human
  • private String name
  • private char sex
  • public String toString()
  • return name " " sex

7
Creating a Human
  • //Using the class Human
  • public class TestHuman
  • public static void main(String args)
  • Human me new Human()
  • System.out.println(me.toString())

Error!
8
Initialization
  • //A pretty useless class
  • public class Human
  • private String name
  • private char sex
  • These instance variables are not initialized
    (properly)
  • Default initialization is usually not sufficient

9
Constructor
  • //A pretty useless class
  • public class Human
  • private String name
  • private char sex
  • public Human()
  • name "Adam"
  • sex 'M'

10
Creating a Human
  • //Using the class Human
  • public class TestHuman
  • public static void main(String args)
  • Human me new Human()
  • System.out.println(me.toString())

Adam M
11
Terminology
  • Declare a variable
  • String name
  • Human me
  • Initialize a variable
  • name "Adam"
  • me new Human()
  • Instantiate an object

12
Terminology
  • Method header / body
  • public String toString()
  • Method call
  • me.toString()
  • Constructor
  • public Human()

13
Parameter
  • //A more flexible, but still useless class
  • public class Human
  • private String name
  • private char sex
  • public Human(String aName)
  • name aName
  • sex 'M'

14
Argument
  • //Using the class Human
  • public class TestHuman
  • public static void main(String args)
  • Human me new Human("Tim Margush")
  • System.out.println(me.toString())

15
Parameters
  • Human me new Human("Tim Margush")
  • public class Human
  • private String name
  • public Human(String aName)
  • name aName
  • sex 'M'

Tim Margush
16
Terminology
  • Parameter List
  • public Human(String aName, char aSex)
  • Packages
  • default package (no import needed)
  • class TestHuman uses class Human
  • import java.util.Scanner
  • class TestHuman might use a Scanner
  • java.lang.String
  • java.lang. is implicitly imported

17
Terminology
  • Return Type
  • public String toString()
  • public static void main(String args)
  • Local variable
  • Human me
  • Instance variable (field)
  • private String name
  • Holds an attribute of the object

18
Getters and Setters
  • Use public methods to set or get instance
    variables
  • public void setSex(char s)
  • if (s'M') sex 'M'
  • if (s'F') sex 'F'
  • public char getSex()
  • return sex

19
Terminology
  • Primitive types
  • Data types directly supported by the language
  • int a long b boolean done char initial
  • These memory locations contain data encoded
    according to the type of the variable
  • Reference Types
  • Data refering to an object a pointer to the
    object
  • String x Human me
  • These variables contain a reference to an object

20
Default Constructor
  • An explicit constructor with no parameters
  • Performs "customized default" initialization
  • The implicit constructor provided by the compiler
  • Performs "Java default" initialization
  • zero, '\0' (nul), false, null
  • Exists ONLY if there are no explicit constructors

21
Constructor Invocation
  • new Human()
  • public Human()
  • new Human('F')
  • public Human(char s)
  • new Human("Harold")
  • public Human(String n)
  • new Human("Fred", 'M')
  • public Human(String n, char s)

22
Floating Point vs Integer
  • byte, short, int, long
  • 1/8, 2/16, 4/32, 8/64 bytes/bits
  • 2bits distinct values
  • Range is -2bits-1 through 2bits-1-1
  • float, double
  • 4/32, 8/64 bytes/bits
  • Fewer than 2bits distinct values
  • Range includes selected rational numbers

23
Precision
  • Floating point data is sometimes precise
  • 1.0 is stored exactly
  • 0.1 is never exact the closest rational in the
    set of double data values is chosen
  • Floating point operations often magnify errors
  • println(0.16f 10) displays 1.5999999

24
Precision
  • Conditions can be misleading
  • float x
  • //some computation to determine x
  • if (x ! 0)
  • x may be 0.0000000000000000012 due to floating
    point error
  • if (Math.abs(x) lt 1e-6)

25
Floating Point IO
  • double x in.nextDouble()
  • println(x)
  • printf(".3fn", x)
  • printf("7.0f", x)

26
Literals
  • Data values specified directly in the source
  • 32 46.7 true '\n' "Joe"
  • Literals imply a data type
  • int double boolean char String
  • Special notation
  • 32.7f 1f 9876543210L 037 0xFF 3e2

27
GUI Dialogs
  • Used to display messages or acquire input via a
    GUI container
  • Standard dialogs are available in
    javax.swing.JOptionPane
  • MessageDialog
  • InputDialog

28
A Message Dialog
  • import javax.swing.JOptionPane
  • public class TestDialogs
  • public static void main(String args)
  • JOptionPane.showMessageDialog(
  • null, "Greetings Earthling!")
  • System.out.println("Continuing...")

29
Modal Dialog
  • Pauses execution until the dialog is dismissed
  • Dialog is displayed, execution pauses

30
Modal Dialog
  • Pauses execution until the dialog is dismissed
  • Dialog is displayed, execution pauses
  • Dialog is dismissed, execution resumes

31
An Input Dialog
  • import javax.swing.JOptionPane
  • public class TestDialogs
  • public static void main(String args)
  • String sAge JOptionPane.showInputDialog(
  • null, "How old are you?")
  • int age Integer.parseInt(sAge)
  • JOptionPane.showMessageDialog(null, "You
    are " age " years old. ")

32
Summary
  • A class is a factory for the instantiation of an
    object
  • Each object has its own memory, and consequently
    its own characteristics.
  • Each object shares methods with all other objects
    of the same class
  • Methods can accept arguments and return a value

33
Summary
  • The new operator allocates memory for an object,
    invokes a constructor to initialize the object,
    and evaluates to a reference to the new object
  • Local variables are allocated when their method
    is called and must be initialized before use

34
Summary
  • Instance variables
  • Are generally private
  • Are allocated when an object is created
  • Have a default initialization
  • Often initialized in constructors
  • Get and Set methods are used to access and modify
    instance members

35
Summary
  • Variables can be a primitive or reference type
  • Floating point types are not precise, but useful
    to represent some rational numbers
Write a Comment
User Comments (0)
About PowerShow.com