Abstract Data Types - PowerPoint PPT Presentation

Loading...

PPT – Abstract Data Types PowerPoint presentation | free to download - id: 7e16ce-MmI4Z



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Abstract Data Types

Description:

Title: Abstract Data Types Author: cyfair Last modified by: John Spicer Created Date: 3/27/2006 12:52:25 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 67
Provided by: cyf65
Learn more at: http://instantjava.tripod.com
Category:
Tags: abstract | data | types | write

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Abstract Data Types


1
Abstract Data Types
2
Objective
  • Understand the concept of an ADT (Abstract Data
    Type)
  • Learn how to implement an ADT.

3
Abstract Data Types
  • An Abstract Data Type (ADT) is defined as a set
    of data and the operations that can be performed
    on the data.

4
What Does Abstract Mean?
5
Abstract Art
6
Abstract Art
  • Railway Crossing by Fernand Leger 1919

7
Abstract Art
  • In abstract art you really dont know what the
    painting is until you are told. You could say
    that the details of the painting are hidden from
    you.

8
What Does Abstract Mean?
  • In programming Abstract means that the focus (of
    a class) is on the public fields and methods. The
    actual implementation is not defined, and does
    not affect the use of the abstract data type.

9
What Does Abstract Mean?
  • In programming Abstract means that the focus (of
    a class) is on the public fields and methods. The
    actual implementation is not defined, and does
    not affect the use of the abstract data type.
  • The focus is on the interface, not on the
    implementation.

10
Example of an ADT
  • The String class is an ADT.

How is the String class implemented?
How does substring work?
11
  • An ADT is represented by an interface, which
    shields the implementation. Users of an ADT are
    concerned with the interface, not the
    implementation. This supports the principle of
    information hiding, or protecting the program
    from design decisions that could change.

12
Implementation of an ADT
  • ADTs are implemented as Classes
  • They can contain both public and private fields
  • They can contain both public and private methods
  • The public fields and methods constitutes the
    interface

13
Top Down Design
14
Top Down Design (contd)
  • Start JCreator.
  • Create a new file called Human.java.
  • Save the new file in your Lab18\AlleyOop folder.

15
Top Down Design (contd)
  • WAC (write a class) that stores the stages of
    human evolution, time period in which they
    lived, and the traits of different species of
    humans in the evolutionary tree. Call the class
    Human. Human is an Abstract Data Type and WILL
    NOT contain a main method.

16
Human ADT
  • public class Human

17
Instance Fields
  • An Abstract Data Type (ADT) is defined as a set
    of data and the operations that can be performed
    on the data.

18
Instance Fields
  • public class Human
  • private String species
  • private String period
  • private String trait

19
Methods
  • An Abstract Data Type (ADT) is defined as a set
    of data and the operations that can be performed
    on the data.

20
Constructor
  • A constructor is a method that has the same name
    as the class and no return type. Constructors are
    used to initialize an instance of the class.
    Constructor are automatically invoked when the
    object is instantiated.

21
Constructors
  • public Human()
  • public Human(String species,
  • String period,
  • String trait)
  • this.species species
  • this.period period
  • this.trait trait

22
Accessors
An Accessor  is a member method that accesses the
contents of an object but does not modify that
object. An accessor simply returns the value of
one of the member variables.
23
Accessors
  • public String getSpecies()
  • return species

24
Accessors
  • public String getSpecies()
  • return species
  • public String getPeriod()
  • return period

25
Accessors
  • public String getSpecies()
  • return species
  • public String getPeriod()
  • return period
  • public String getTrait()
  • return trait

26
A Mutator  is a member method that can modify an
object. In the simplest case, a mutator simply
assigns a new value to one of the member
variables
Mutators
27
Mutators
  • public void setSpecies(String species)
  • this.species species

28
Mutators
  • public void setSpecies(String species)
  • this.species species
  • public void setPeriod(String period)
  • this.period period

29
Mutators
  • public void setSpecies(String species)
  • this.species species
  • public void setPeriod(String period)
  • this.period period
  • public void setTrait(String trait)
  • this.trait trait

30
Mutators
  • public void setSpecies(String species)
  • this.species species
  • public void setPeriod(String period)
  • this.period period
  • public void setTrait(String trait)
  • this.trait trait

If we omit the mutator methods then the class
becomes immutable the contents of the fields
cannot be changed.
31
toString() Method
  • Returns a String that represents the current
    Object.

32
toString() Method
  • _at_Override
  • public String toString()

33
toString() Method
  • _at_Override
  • public String toString()
  • return species ( period
  • )\n trait

34
equals Method
The equals() method of the Object class acts the
same as the operator that is, it tests for
object identity rather than object equality. The
implicit contract of the equals() method,
however, is that it tests for equality rather
than identity. Thus most classes will override
equals() with a version that does field by field
comparisons before deciding whether to return
true or false.
35
equals Method
  • _at_Override
  • public boolean equals(Object obj)

36
equals Method
  • public boolean equals(Object obj)
  • if (obj instanceof Human)

37
equals Method
  • public boolean equals(Object obj)
  • if (obj instanceof Human)
  • return false

38
equals Method
  • public boolean equals(Object obj)
  • if (obj instanceof Human)
  • Human h (Human) obj
  • return false

39
equals Method
  • public boolean equals(Object obj)
  • if (obj instanceof Human)
  • Human h (Human) obj
  • return species.equals(h.species)
  • return false

40
Top Down Design (contd)
Compile Human.java
41
Using The Human Class
42
Top Down Design (contd)
  • WAP called Lab18. Lab18 will read in the stage of
    evolution (species), time period, and trait of
    different species of man in the human
    evolutionary tree. Store them in an arrray of
    Human objects. Oubput all the Humans in the array
    using the toString method of the Human class.

43
Top Down Design (contd)
  • Create a new file called Lab18.java.
  • Save the new file in your Lab18/AlleyOop folder.

44
Lab18
import java.util.Scanner import
java.io. public class Lab18
45
Instance Fields
private Human humans new Human25 private
int count 0
46
Main Method
public static void main(String args) Lab18
lab new Lab18() lab.input() lab.output()

47
input()
48
input()
public void input()
49
input()
public void input() try catch
(IOException e) System.out.println(e)
System.exit(0)
50
input()
try Scanner reader new Scanner(new
File(humans.dat))
51
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\n
)
52
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\n
) while (reader .hasNext())

53
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\n
) while (reader.hasNext()) String
species reader .next()
54
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\
n) while (reader.hasNext()) String
species reader.next() String period
reader.next()
55
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\
n) while (reader.hasNext()) String
species reader.next() String period
reader.next() String trait
reader.next()
56
input()
try Scanner reader new Scanner(new
File(humans.dat)) reader.useDelimiter(/\r\
n) while (reader.hasNext()) String
species reader.next() String period
reader.nest() String trait
reader.next() humanscount new
Human(species, period, trait)
57
output()
58
output()
public void output()
59
output()
public void output() for (int i 0 i lt
count i)
60
output()
public void output() for (int i 0 i lt
count i) System.out.println(humansi)

61
Top Down Design (contd)
Compile and Run Lab18.java
Ramapithecus (14-8 Million Years Ago) A
small ape-like creature that probably walked
upright. Australopithecus ramidus (4.4 Million
Years Ago) 1.2 Meters tall, some teeth
were human-like. Australopithecus Afarensis
(4-2.8 Million Years Ago) Brain was on
third that of modern humans. Australopithecus
Africanus (3-2 Million Years Ago) Upright
with large teeth shaped like those of a
human. Australopithecus Robustus (2-1.5 Million
Years Ago) The ultimate chewing
machine. Australopithecus Boisei (1.8 Million
Years Ago) The height of a modern man but
with a structure resembling the gorilla. Homo
Habilis (2-1.5 Million Years Ago) Brain
half the size of a modern human. May have built
the first shelters. Homo erectus (1.6 million to
200,000 years ago) Left Africa and spread
throughout Asia and Europe. Homo Sapiens (400,000
to 40,000 years ago) Appearance very
similar to a modern human. Earliest Homo sapiens
were Neanderthals. Homo Sapiens Sapiens (40,000
years ago to present) Earliest Homo
sapiens sapiens were Cromagnons.
62
output()
Lets look at one field in the Human class.
public void output() for (int i 0 i lt
count i) System.out.println(humansi)

63
output()
Lets look at one field in the Human class.
public void output() for (int i 0 i lt
count i) System.out.println(humansi.ge
tSpecies())
64
Top Down Design (contd)
Compile and Run Lab18.java
Ramapithecus Australopithecus ramidus Australopith
ecus Afarensis Australopithecus
Africanus Australopithecus Robustus Australopithec
us Boisei Homo Habilis Homo erectus Homo
Sapiens Homo Sapiens Sapiens
65
Questions?
66
Begin Lab 18
About PowerShow.com