






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to Object-Oriented Programming (OOP) in Java, focusing on the implementation of Abstract Data Types (ADTs) for strings, colors, and pictures. the creation and manipulation of strings using Java's built-in String ADT, as well as the implementation of custom Color and Picture ADTs. The examples demonstrate various operations, such as getting and setting color components, creating grayscale versions of pictures, and scaling images.
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







h t t p : / / i n t r o c s. c s. p r i n c e t o n. e d u
R O B E R T S E D G E W I C K
K E V I N W A Y N E
An Interdisciplinary Approach
C S. 8. A. A D T s. O v e r v i e w
Primitive types
machine representations
machine instructions.
A data type is a set of values and a set of operations on those values.
An abstract data type is a data type whose representation is hidden from the client.
We want to write programs that process
other types of data.
Colors, pictures, strings,
...
Object-oriented programming (OOP).
Create your own data types.
Best practice: Use abstract data types (representation is hidden from the client ).
Impact: Clients can use ADTs without knowing implementation details.
Next lecture: how to implement your own ADTs
An object holds a data type value.
Variable names refer to objects.
Examples (stay tuned for details)
5
public class String
String(String s)
create a string with the same value
int length()
string length
char charAt(int i) ith character
String substring(int i, int j) ith through (j-1)st characters
boolean contains(String sub) does string contain sub_?_
boolean startsWith(String pre) does string start with pre_?_
Java's String ADT allows us to write Java programs that manipulate strings.
The exact representation is hidden (it could change and our programs would still work).
A String is a sequence of Unicode characters.
defined in terms of its ADT values (typical)
stay tuned for more complete API later in this lecture
We have already been using ADTs!
6
To use a data type, you need to know:
How to construct new objects.
To construct a new object
Use data type name to specify type of object.
To apply an operation (invoke a method)
Use object name to specify which object.
operation is to be applied.
Q. What is an abstract data type?
Q. What is a data type?
A. A set of values and a set of operations on those values.
Q. What is an abstract data type?
Q. What is a data type?
A. A set of values and a set of operations on those values.
A. A data type whose representation is hidden from the client.
13
Goal. Write a Java program to generate Albers
% java AlbersSquares 0 64 128 105 105 105
% java AlbersSquares 251 112 34 177 153 71
% java AlbersSquares 28 183 122 15 117 123
14
public class AlbersSquares
public static void main(String[] args)
int r1 = Integer.parseInt(args[0]);
int g1 = Integer.parseInt(args[1]);
int b1 = Integer.parseInt(args[2]);
Color c1 = new Color(r1, g1, b1);
int r2 = Integer.parseInt(args[3]);
int g2 = Integer.parseInt(args[4]);
int b2 = Integer.parseInt(args[5]);
Color c2 = new Color(r2, g2, b2);
StdDraw.setPenColor(c1);
StdDraw.filledSquare(.25, .5, .2);
StdDraw.setPenColor(c2);
StdDraw.filledSquare(.25, .5, .1);
StdDraw.setPenColor(c2);
StdDraw.filledSquare(.75, .5, .2);
StdDraw.setPenColor(c1);
StdDraw.filledSquare(.75, .5, .1);
create first color
create second color
draw first square
draw second square
% java AlbersSquares 0 64 128 105 105 105
Def. The monochrome luminance of a color quantifies its effective brightness.
examples
Applications (next)
Choose colors for displayed text.
NTSC standard formula for luminance: 0.299r + 0.587g + 0.114b.
import java.awt.Color;
public class Luminance
public static double lum(Color c)
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
return .299r + .587g + .114*b;
public static void main(String[] args)
int r = Integer.parseInt(args[0]);
int g = Integer.parseInt(args[1]);
int b = Integer.parseInt(args[2]);
Color c = new Color(r, g, b);
StdOut.println(Math.round(lum(c)));
% java Luminance 0 64 128
52
Q. Which font colors will be most readable with which background colors on a display?
Rule of thumb. Absolute value of difference in luminosity should be > 128.
255 76 179 24
76 255 255 52
255 179 255 255 203
52 24 52 203 255
17
Goal. Convert colors to grayscale values.
Fact. When all three R, G, and B values are the same,
resulting color is on grayscale from 0 (black) to 255 (white).
Q. What value for a given color?
A. Its luminance!
examples
method for Luminance library
18
Q. How does Java represent color? Three int values? Packed into one int value?
A. We don't know. The representation is hidden. It is an abstract data type.
An object reference is analogous to a variable name.
Possible memory representation of red = new Color(255, 0, 0)
and gray = new Color(105, 105, 105);
105 105 105 x y 255 0 0
y red
memory
address
gray x
René Magritte. This is not a pipe.
Java. These are not colors.
public static Color toGray(Color c)
int y = (int) Math.round(lum(c));
Color gray = new Color(y, y, y);
return gray;
Object-oriented programming. A natural vehicle for studying abstract models of the real world.
It is a picture of a painting of a pipe.
Surrealist computer scientist:
Neither is this.
This is not a pipe.
import java.awt.Color;
public class Grayscale
public static void main(String[] args)
Picture pic = new Picture(args[0]);
for (int col = 0; col < pic.width(); col++)
for (int row = 0; row < pic.height(); row++)
Color color = pic.get(col, row);
Color gray = Luminance.toGray(color);
pic.set(col, row, gray);
pic.show();
25
create a new picture
fill in each pixel
% java Grayscale mandrill.jpg
Q. What is the effect of the following code (easy question)?
26
Picture pic = new Picture(args[0]);
for (int col = 0; col < pic.width(); col++)
for (int row = 0; row < pic.height(); row++)
pic.set(col, row, pic.get(col, row));
pic.show();
Q. What is the effect of the following code (easy question)?
Picture pic = new Picture(args[0]);
for (int col = 0; col < pic.width(); col++)
for (int row = 0; row < pic.height(); row++)
pic.set(col, row, pic.get(col, row));
pic.show();
Q. What is the effect of the following code (not-so-easy question)?
Picture pic = new Picture(args[0]);
for (int col = 0; col < pic.width(); col++)
for (int row = 0; row < pic.height(); row++)
pic.set(col, pic.height()-row-1, pic.get(col, row));
pic.show();
Q. What is the effect of the following code (not-so-easy question)?
29
Picture pic = new Picture(args[0]);
for (int col = 0; col < pic.width(); col++)
for (int row = 0; row < pic.height(); row++)
pic.set(col, pic.height()-row-1, pic.get(col, row));
pic.show();
Q. What is the effect of the following code?
30
Picture source = new Picture(args[0]);
int width = source.width();
int height = source.height();
Picture target = new Picture(width, height);
for (int col = 0; col < width; col++)
for (int row = 0; row < height; row++)
target.set(col, height-row-1, source.get(col, row));
target.show();
Q. What is the effect of the following code?
Picture source = new Picture(args[0]);
int width = source.width();
int height = source.height();
Picture target = new Picture(width, height);
for (int col = 0; col < width; col++)
for (int row = 0; row < height; row++)
target.set(col, height-row-1, source.get(col, row));
target.show();
Goal. Write a Java program to scale an image (arbitrarily and independently on x and y ).
Source: mandrill.jpg
300x
% java Scale mandrill.jpg 500 500
500x
% java Scale mandrill.jpg 600 200
600x
% java Scale mandrill.jpg 200 400
200x
99x
% java Scale mandrill.jpg 99 99
C S. 8. C. A D T s. I m a g e s
C S. 8. D. A D T s. S t r i n g s
A String is a sequence of Unicode characters.
public class String
String(String s) create a string with the same value
int length() string length
char charAt(int i) ith character
String substring(int i, int j) i th through (j-1) st characters
boolean contains(String sub) does string contain sub?
boolean startsWith(String pre)
does string start with pre?
boolean endsWith(String post) does string end with post?
int indexOf(String p) index of first occurrence of p
int indexOf(String p, int i) index of first occurrence of p after i
String concat(String t) this string with t appended
int compareTo(String t) string comparison
String replaceAll(String a, String b) result of changing a s to b s
String[] split(String delim)
strings between occurrences of delim
boolean equals(Object t)
is this string's value the same as t 's?
Java's ADT allows us to
write Java programs
that manipulate strings.
defined in terms of its ADT values (typical)
Is the string a palindrome?
Find lines containing a specified string in StdIn
*Search for .edu hyperlinks in the text file on StdIn
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
41
Pre-genomics era. Sequence a human genome.
Post-genomics era. Analyze the data and understand structure.
Goal. Write a Java program to find genes in a given genome.
Genomics. Represent genome as a string over A C T G alphabet.
Gene. A substring of genome that represents a functional unit.
Made of codons (three A C T G nucleotides ).
Succeeded by TAG, TAA, or TGA ( stop codon).
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
start gene stop start
stop gene
42
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
start gene stop
public class Gene
public static boolean isPotentialGene(String dna)
if (dna.length() % 3 != 0) return false;
if (!dna.startsWith("ATG")) return false;
for (int i = 0; i < dna.length() - 3; i+=3)
String codon = dna.substring(i, i+3);
if (codon.equals("TAA")) return false;
if (codon.equals("TAG")) return false;
if (codon.equals("TGA")) return false;
if (dna.endsWith("TAA")) return true;
if (dna.endsWith("TAG")) return true;
if (dna.endsWith("TGA")) return true;
return false;
public static void main(String[] args)
StdOut.println(isPotentialGene(args[0]));
% java Gene ATGCATAGCGCATAG
true
% java Gene ATGCGCTGCGTCTGTACTAG
false
% java Gene ATGCCGTGACGTCTGTACTAG
false
Algorithm. Scan left-to-right through dna.
If start codon ATG found, set beg to index i.
i
codon
beg output remainder of input string
start stop
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
start gene stop start
gene stop
Implementation. Entertaining programming exercise!
Goal. Write a Java program to find genes in a given genome.
Possible memory representation of
x 15 x+9 4 x+1 4
x
a a c a a g t t t a c a a g c
genome
memory
address
length
t s
Implications
(s == t) is false (because it compares addresses).
Java String interface is more complicated than the API.