






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























A data type represents a set of possible values, such as {..., -2, -1, 0 , 1 , 2 , ...}, or {true, false}
Some operations only make sense when applied to certain kinds of data--multiplication, searching
A String requires more and different storage than a boolean
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%
boolean char, byte, short, int, long float, double
a set of values a data representation a set of operations
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
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
Is written with letters, and its arguments are enclosed in parentheses: toString(), Math.abs(n) Has any (predetermined) number of arguments
There are many ways you could insert a new node into a list:
What is easiest for the programmer?
Let the programmer direc...^ Provide many different ...^ Provide methods for onl...
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
a set of values a set of operations, which can be applied uniformly to all these values
What part of a Data Type does an ADT leave out?
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
The user need not know the representation The user should not be allowed to tamper with the representation Solution: Make all data private
Solution: Use setters and getters
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)
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 ρ