Adv' Topic 6'3, 13'7 Enum - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Adv' Topic 6'3, 13'7 Enum

Description:

Inherit a toString() and clone() method. toString() Just returns the objects name as a String ... clone() Just returns the object, no copy is created. Adding methods ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 12
Provided by: ameet9
Category:
Tags: adv | clone | enum | inherit | topic

less

Transcript and Presenter's Notes

Title: Adv' Topic 6'3, 13'7 Enum


1
Adv. Topic 6.3, 13.7 - Enum
2
Enumerated Types
  • How do we represent a finite set of values?
  • Set of nucleotides, amino acids
  • Marriage status
  • Automaton types
  • So far we arbitrarily give values
  • AutomatonTypes were classes with a String value
  • MARRIED 1 SINGLE 0
  • A 0 C 1 G 2 T 3

3
Use enum
  • Problem with those approaches is that there are
    no constraints ? bugs
  • base 5
  • marriageStatus 2
  • Solution - Enumerated Types enum
  • Special class in Java 1.5.0
  • Enumerated types are used to represent cases
    where there are a finite number of possible values

4
  • public enum FilingStatus SINGLE, MARRIED
  • ----
  • Usage
  • FilingStatus status FilingStatus.SINGLE
  • if(status FilingStatus.SINGLE)...

5
Example
  • public class TaxReturn
  • public enum FilingStatus SINGLE, MARRIED
  • private FilingStatus status
  • public TaxReturn(double inIncome, FileStatus
    inStatus)

6
Outside class access
  • Can be treated as a class field
  • TaxReturn tr new TaxReturn(income,
    TaxReturn.FilingStatus.SINGLE)
  • public enum Base A, C, G, T

7
AT 13.7
  • Enumeration is just a special class
  • No constructor. Why?
  • All enumerated classes extend Enum
  • Inherit a toString() and clone() method
  • toString()
  • Just returns the objects name as a String
  • FilingStatus.SINGLE.toString() ? SINGLE
  • clone()
  • Just returns the object, no copy is created

8
Adding methods
  • Can add methods/constructors to an enumerated
    type
  • All instances must be created in the class though
  • public enum CoinType
  • private double value
  • PENNY(0.01), NICKEL(0.05), DIME(0.1),
    QUARTER(0.25)
  • CoinType(double aValue)
  • value aValue
  • public double getValue()
  • return value

9
  • Have four instances
  • CoinType.PENNY, CoinType.NICKEL, etc
  • All have a value associated with them now
  • CoinType.PENNY.getValue()
  • Not the same as Coin class
  • Coin class could create multiple objects(many
    pennies in piggy bank)
  • CoinType only describes types of coins

10
AutomatonType revisited
  • AutomatonType was a more generic enumerated type
  • We made constructor private ? no new objects
    could be created
  • All public constants
  • All have a string value associated
  • How would our AutomatonType class look with enum?
  • What could we add to store arrays?

11
  • public enum AutomatonType GLIDER, LONER, EMPTY,
    SPIRAL
  • Does our game of life class have to change?
Write a Comment
User Comments (0)
About PowerShow.com