Docsity

Data Types, Abstract Data Types, and their Implementation in Java, Slides of Programming Languages

Partial preview of the text

Download Data Types, Abstract Data Types, and their Implementation in Java and more Slides Programming Languages in PDF only on Docsity!

Abstract Data Types

Data types I

 We type data--classify it into various categories--

such as int, boolean, String, Applet

 A data type represents a set of possible values, such as {..., -2, -1, 0 , 1 , 2 , ...}, or {true, false}

 By typing our variables, we allow the computer to

find some of our errors

 Some operations only make sense when applied to certain kinds of data--multiplication, searching

 Typing simplifies internal representation

 A String requires more and different storage than a boolean

What can we abstract away?

A. The set of values

B. The data representation

C. The set of operations

D. More than one of the above

E. None of the above

The set of values The data representation^ The set of operations More than one of the above

None of the above

0% 0% 0% 0% 0%

Primitive types in Java

 Java provides eight primitive types:

 boolean  char, byte, short, int, long  float, double

 Each primitive type has

 a set of values  a data representation  a set of operations

 These are “set in stone”—there is nothing the

programmer can do to change anything about them

Classes in Java

 A class is a data type

 The possible values of a class are called objects  The data representation is a reference (pointer) to a block of storage  The structure of this block is defined by the fields (both inherited and immediate) of the class  The operations on the objects are called methods

 Many classes are defined in Java’s packages

 You can (and must) define your own, as well

Methods and operators

 An operator typically

 Is written with non-alphabetic characters: +, *, ++, +=, &&, etc.  Is written as prefix, infix, or postfix: -x, x+y, x++  Has only one or two arguments, or operands

 A method (or function ) typically

 Is written with letters, and its arguments are enclosed in parentheses: toString(), Math.abs(n)  Has any (predetermined) number of arguments

Insertion into a list

 There are many ways you could insert a new node into a list:

  • As the new first element
  • As the new last element
  • Before a given node
  • After a given node
  • Before a given value
  • After a given value
    • Before the nth^ element
    • After the nth^ element
    • Before the nth^ from the end
    • After the nth^ from the end
    • In the correct location to keep the list in sorted order

What is easiest for the programmer?

A. Let the programmer directly

access and manipulate the next

fields of cells

B. Provide many different methods

to insert an element into a list

C. Provide methods for only the

most common ways to insert an

element into a list

Let the programmer direc...^ Provide many different ...^ Provide methods for onl...

Efficiency

 A list is just a sequence of values—it could be

implemented by a linked list or by an array

 Inserting as a new first element is efficient for a linked list representation, inefficient for an array  Accessing the nth^ element is efficient for an array representation, inefficient for a linked list  Inserting in the nth^ position is efficient for neither

 Do we want to make it easy for the user to be

inefficient?

 Do we want the user to have to know the

implementation?

Abstract Data Types

 An Abstract Data Type (ADT) is:

 a set of values  a set of operations, which can be applied uniformly to all these values

 To abstract is to leave out information, keeping

(hopefully) the more important parts

 What part of a Data Type does an ADT leave out?

Data Structures

 Many kinds of data consist of multiple parts, organized (structured) in some way

 A data structure is simply some way of organizing a value that consists of multiple parts  Hence, an array is a data structure, but an integer is not

 When we talk about data structures, we are talking about the implementation of a data type

 If I talk about the possible values of, say, complex numbers, and the operations I can perform with them, I am talking about them as an ADT

 If I talk about the way the parts (“real” and “imaginary”) of a complex number are stored in memory, I am talking about a data structure

 An ADT may be implemented in several different ways  A complex number might be stored as two separate doubles, or as an array of two doubles, or even in some bizarre way

Data representation in an ADT

 An ADT must obviously have some kind of

representation for its data

 The user need not know the representation  The user should not be allowed to tamper with the representation  Solution: Make all data private

 But what if it’s really more convenient for the user

to have direct access to the data?

 Solution: Use setters and getters

Naming setters and getters

 Setters and getters should be named by:

 Capitalizing the first letter of the variable (first becomes First), and  Prefixing the name with get or set (setFirst)  For boolean variables, replace get with is (for example, isRunning)

 This is more than just a convention—if and when you

start using JavaBeans, it becomes a requirement

What’s the point?

 Setters and getters allow you to keep control of your implementation

 For example, you decide to define a Point in a plane by its x-y coordinates:  class Point { public int x; public int y; }

 Later on, as you gradually add methods to this class, you decide that it’s more efficient to represent a point by its angle and distance from the origin, θ and ρ

 Sorry, you can’t do that—you’ll break too much code that accesses x and y directly

 If you had used setters and getters, you could redefine them to compute x and y from θ and ρ