

































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
Material Type: Notes; Professor: Vidal; Class: ALGORITHMIC DESIGN I; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Unknown 1989;
Typology: Study notes
1 / 41
This page cannot be seen from the preview
Don't miss anything!


































After studying this chapter, you should be able to: • Chapter Objectives Describe models • Describe the relationship between objects and classes • Understand the syntax and semantics of a simple Java program • Write object-oriented programs that simulate robots • Understand and fix errors that can occur when constructing aprogram • Read documentation for classes • Apply the concepts learned with robots to display a window as usedin a graphical user interface
Information, called
attributes
Services
that either change the attributes (a
command
) or
answer a question about attributes (a
query
A program may havemany similar objects
-^ Objects can bevisualized with an object diagram
shows attributenames and values
Conc ert d ate: 28 -M arch-20 08 perfo rm er: To ro nto Sym ph on y un so ld Tick ets : 35 A, 35 B, 3 5Cso ld Tick ets : 10 A, 10 B, ..., 3 4Z , ...
35 D, ...
Conce rt date: 2 1-M arch-20 08 p erfo rm er: Great B ig Sea un so ld Tickets : 1 0D, 22 H , 2 5A ,
2 5B, 25 C, 28 Z ,...
Sold Seats : 1 0A, 10 B, 10 C,
..., 22 N, 2 2P , ...
Conc ert date: 22 -M arch-2 00 8 perform er: Great Big Sea u ns oldT ickets: 35 A, 3 5B, 35 Cs oldT ickets: 10 A, 1 0B, ..., 34 Z ,
... 35D , ...
Type ofobject Attributevalues
Attributenames
have the same kind of information
-^ offer the same services are called a
class
Classes arerepresented with a class diagram
Concert
dateperformerunsoldTicketssoldTicketsConcert(date, performer)numTicketsSold( )valueOfTicketsSold( )performerName( )performanceDate( )sellTicket(seatNumber)
Name ofthe class Attributes Services
Quick Quiz
1.Draw an object diagram for the robot labelled “M”on the previous slide.Hint: Three
Concert
object diagrams areshown to the right. 2.Draw your object diagram again after the robot hasexecuted the followingcommands: move()pickThing()
Conc ert d ate: 28 -M arch-20 08 perfo rm er: To ro nto Sym ph on y un so ld Tick ets : 35 A, 35 B, 3 5Cso ld Tick ets : 10 A, 10 B, ..., 3 4Z , ...
35 D, ...
Conce rt date: 2 1-M arch-20 08 p erfo rm er: Great B ig Sea un so ld Tickets : 1 0D , 22 H , 2 5A ,
2 5B, 25 C, 28 Z ,...
Sold Seats : 1 0A, 10 B, 10 C,
..., 22 N , 2 2P , ...
Conc ert date: 22 -M arch-2 00 8 perform er: Great Big Sea u ns oldT ickets: 35 A, 3 5B, 35 Cs oldT ickets: 10 A, 1 0B, ..., 34 Z ,
... 35D , ...
Type ofobject Attributevalues
Attributenames
Quick Quiz Solutions
Robot
currrentAvenue: 1
currentStreet: 0
direction: WESTbackpack: (em pty
Robot
currrentAvenue: 0
currentStreet: 0
direction: WESTbackpack: one
thing
Solutions may also containattributes for the label and color.
Two robots running a “relay.” 1.4: An Example Program (1/3)
Initial Situation Final Situation“B” picks up thebaton and takes itto “K”, whofinishes the race.
1.4: An Example Program (2/3)
// Set up the initial situation City beijing = new City();Robot ben = new Robot(beijing, 2, 0, Direction.SOUTH);Robot karel = new Robot(beijing, 2, 3, Direction.SOUTH);Thing baton = new Thing(beijing, 3, 0);Wall finishLine = new Wall(beijing, 3, 6, Direction.EAST);karel.setLabel(
"K"
);
ben.setLabel(
"B"
);
// Run the relay ben.move(); // bwbben.turnLeft();ben.pickThing();ben.move();ben.move();ben.move();ben.putThing();karel.move();karel.turnLeft();karel.pickThing();karel.move();karel.move();karel.move();karel.putThing();
1.4.5: Tracing a Program (1/2)
ben
karel
baton
Program Stmt
str
ave
dir
bp
str
ave
dir
bp
str
ave
2
0
S^
--^
2
3
S^
--^
3
0
ben.move();
3
0
S^
--^
2
3
S^
--^
3
0
ben.turnLeft();
3
0
E^
--^
2
3
S^
--^
3
0
ben.pickThing();
3
0
E^
ba
2
3
S^
--^
3
0
ben.move();
3
1
E^
ba
2
3
S^
--^
3
1
ben.move();
3
2
E^
ba
2
3
S^
--^
3
2
ben.move();
3
3
E^
ba
2
3
S^
--^
3
3
1.4.5: Tracing a Program (2/2)
ben
karel
baton
Program Stmt
str
ave
dir
bp
str
ave
dir
bp
str
ave
3
3
E^
ba
2
3
S^
--^
3
3
ben.putThing();
3
3
E^
--^
2
3
S^
--^
3
3
karel.move();
3
3
E^
--^
3
3
S^
--^
3
3
karel.turnLeft();
3
3
E^
--^
3
3
E^
--^
3
3
karel.pickThing();
3
3
E^
--^
3
3
E^
ba
3
3
karel.move();
3
3
E^
--^
3
4
E^
ba
3
4
karel.move(); etc.
3
3
E^
--^
3
5
E^
ba
3
5
1.5: Compiling and Executing Programs
Three kinds of errors: • 1.5.1: Compile-Time Errors Compile-Time Errors
The compiler can’t translate your program into an executableform because your program doesn’t follow the language’s rules.
-^ Examples:
karel.move;
instead of
karel.move();
-^ Public class RobotRelay
instead of
public class RobotRelay
-^ Unmatched braces; a
{^
without a corresponding
}
Run-Time Errors
-^ Intent (Logic) Errors
Three kinds of errors: • 1.5.3: Intent (Logic) Errors Compile-Time Errors • Run-Time Errors • Intent (Logic) Errors
The compiler can translate your program and it runs tocompletion, but it doesn’t do what you want it to.
-^ Example: In the relay race, the programmer forgets to instructkarel to turn left after picking up the baton. Initial Situation
Correct Final
Incorrect Final
Patterns are fragments of code that appear repeatedly.We give them names and learn them so that:1.7: Patterns
we can recognize when they are being used
-^ we can discuss them easily with others -^ we can apply them in new situations When patterns are used in the text, an icon and the pattern nameappears in the margin. Discussed in detail later in the chapter.