Sei sulla pagina 1di 22

 

LBYCPEI - Object Oriented Programming Laboratory 


AY 2019-20, Term 3 
INSTRUCTORS: Dr. Melvin Cabatuan, Ph.D., Engr. John Anthony Jose, Engr. Mark Lester Padilla, and Dr. Melchizedek Alipio, Ph.D. 
Coordinator: melvin.cabatuan@dlsu.edu.ph 

Module 1: Introduction to Java, Object-Oriented Paradigm, 


and basic UML diagrams  

Java®  programming  language  is  a  general-purpose,  concurrent,  class-based,  object- 


oriented  language.  The  Java  programming  language  is  related  to  C  and  C++  but  is 
organized  rather  differently,  with  a  number  of  aspects  of  C  and  C++  omitted  and  a  few 
ideas  from other languages included. For example, Java does not support pointers explicitly 
as  in  C/C++,  but  Java  use  pointers implicitly for manipulations of references although these 
pointers  are  not  available  for  outside  use.  The  Java  programming  language  is  a  relatively 
high-level  language,  in  that  details of the machine representation are not available through 
the  language.  It  includes  automatic  storage  management,  typically  using  a  garbage 
collector,  to  avoid the  safety  problems  of  explicit  deallocation  (as  in  C's  free  or  C++'s 
delete). 

Fig 1. Java Program Execution 

 

 

 
 

The  Java  programming  language  is  normally  compiled  to  the  bytecode  instruction  set  and 
binary  format  defined  in  the  Java  Virtual  Machine  (JVM)  Specification  (Fig.  1).  Java  compiler 
produces  intermediate  code  that it stores in files called class files. Those class files are then 
combined  with  other  class  files  and  libraries  to  produce  a  complete  version  of  the 
intermediate  program  with  everything  it  needs  linked  together.  Java  provides  a  dynamic 
load  feature.  It  loads  and  links  the  class  when  it  refers  to  a  class  for  the  first  time  at 
runtime,  not  compile  time.  JVM's  class  loader  executes  the  dynamic  load.  Runtime  Data 
Areas are the memory areas assigned when the JVM program runs on the OS. The bytecode 
that  is  assigned  to  the  runtime  data  areas  in  the  JVM  via  class  loader  is  executed  by  the 
execution engine. The execution engine reads the Java Bytecode in the unit of instruction.  

Object-Oriented Programming 

At  the  center  of  Java  is  object-oriented  programming  (OOP).  The  object-oriented 
methodology  is  inseparable  from  Java,  and  all  Java  programs  are,  to  at  least  some  extent, 
object-oriented.  Because  of  OOP’s  importance  to  Java,  it  is  useful  to  understand  OOP’s 
basic  principles  before  you  write  even  a  simple  Java  program.  One  way  of  understanding 
OOP  is  to  contrast  it  with  the traditional structured programming approach encouraged by 
languages  such  as  C  and  Pascal.  Structured  languages  are  characterized  by  their  support 
for  stand-alone  subroutines,  local  variables,  and  rich  control  constructs.  Although 
structured  languages  are  a  powerful  tool,  even  they  reach  their  limit  when  a  project 
becomes  too  large.  Prior  to  the  invention  of  OOP,  many  projects  were  nearing  the  point 
where the structured approach no longer works. 

OOP  took  the  best  ideas  of  structured  programming  and combined them with several new 


concepts.  The  result  was  a  different  way  of  organizing  a  program.  In  the  most  general 
sense,  a  program can be organized in one of two ways: around its code (what is happening) 
or  around  its  data  (what is being affected). Using only structured programming techniques, 
programs  are  typically  organized  around  code.  This  approach  can  be  thought  of  as  “code 
acting  on  data.”  Object-oriented  programs  work  the  other  way around. They are organized 
around  data,  with  the  key  principle  being  “data  controlling  access  to  code.”  In  an 
object-oriented  language,  you  define the data and the routines that are permitted to act on 

 

 

 
 

that  data.  Thus,  a data type defines precisely what sort of operations can be applied to that 


data. 

Unified Modeling Language (UML) 

Unified  Modeling  Language  (UML)  is  a  graphical  language  for  visualizing,  specifying, 
constructing,  and  documenting  the  artifacts  of  a  software-intensive  system.  It  offers  a 
standard  way  to  write  a  system’s  blueprints,  including  conceptual  things  such  as  business 
processes  and  system  functions  as  well  as  concrete  things  such as programming language 
statements,  database  schemas,  and  reusable  software  components.  It  is  a  standardized 
general-purpose  modeling  language  in  the  field  of  object-oriented  software  engineering. 
UML  includes  a  set  of  graphic  notation  techniques  to  create  visual  models  of 
object-oriented  software systems. UML combines techniques from data modeling, business 
modeling,  object  modeling,  and  component  modeling  and  can  be  used  throughout  the 
software development life-cycle and across different implementation technologies. 

UML diagrams represent two different views of a system model: 

●   Static  (or  structural)  view  -  emphasizes  the  static  structure  of  the  system  using 
objects, attributes, operations, and relationships.  

Ex: Class diagram, Composite Structure diagram.  

● Dynamic  (or behavioral) view - ​emphasizes the dynamic behavior of the system by 
showing collaborations among objects and changes to the internal states of objects. 

  Ex: Sequence diagram, Activity diagram, State Machine diagram. 

The  focus  in  this  laboratory  course  is  with  structural  diagrams which emphasize the things 
that  must be present in the system being modeled. Since they represent the structure, they 
are used extensively in documenting the software architecture of software systems. 

Class Diagram 

Describes  the  structure  of  a  system  by  showing  the  system’s  classes,  their  attributes,  and 
the relationships among the classes. 

Example: 

 

 

 
 

UML  class  notation  is  a  rectangle  divided  into  three  parts:  ​class  name​,  ​attributes​,  and 
operations​.  Names  of  abstract  classes,  such  as  ​Payment​,  are  in  italics.  Relationships 
between  classes  are  the  connecting  links.

Three kinds of relationships of class diagram 

A. Association 

A  relationship  between  instances  of  the  two  classes.  There  is  an  association  between  two 
classes  if  an  instance  of  one  class  must  know about the other in order to perform its work. 
In a diagram, an association is a link connecting two classes. 

B. Aggregation 

An  association  in  which  one  class  belongs  to  a  collection.  An  aggregation  has  a  diamond 
end  pointing  to  the  part  containing  the  whole.  In  our  diagram,  ​Order  has  a  collection  of 
OrderDetails​. 

C. Generalization  

An  inheritance  link  indicating  one  class  is  a  superclass  of  the  other.  A  generalization  has  a 
triangle  pointing  to  the  superclass. ​Payment is a superclass of ​Cash​, ​Check​, and ​Credit​. An 
association  has  two  ends.  An  end  may  have  a  role  name  to  clarify  the  nature  of  the 
association. For example, an ​OrderDetail​ is a line item of each O
​ rder​. 

 

 

 
 

A  ​navigability  arrow  on  an  association  shows  which  direction  the  association  can  be 
traversed  or  queried.  An  ​OrderDetail can be queried about its ​Item​, but not the other way 
around.  The  arrow  also lets you know who "owns" the association's implementation; in this 
case, O
​ rderDetail​ has an I​ tem​. Associations with no navigability arrows are bi-directional. 

Summary of UML Class Relations 

The  ​multiplicity  of  an  association  end  is  the  number  of  possible  instances  of  the  class 
associated  with  a  single  instance  of  the  other  end.  Multiplicities  are  single  numbers  or 
ranges  of  numbers.  In our example, there can be only one ​Customer for each ​Order​, but a 
Customer​ can have any number of ​Orders​. 

Most common multiplicities 

Multiplicities  Meaning 

0..1  zero  or  one  instance.  The  notation  n  .  .  m  indicates n 


to m instances. 

0..* or *  no limit on the number of instances (including none). 

1  exactly one instance 

1..*  at least one instance 

  

 

 

 
 

Terminology 

● Abstraction  -  is  the  process  of defining an object or a collection of data and a set of 


operation  on  that  object/data,  by  specifying  what  operations  can  be  done  on  the 
object/data,  not  how  they  are  done.  In  other  words,  abstraction  separates  the 
interface  from the implementation so a programmer or user only has to understand 
the  interface,  the  commands  to  use,  and  not  how  the  internal  structure  is 
implemented.  
● Class  -  is  a  blueprint  or  prototype  that  defines  the  variables  and methods common 
to  all  objects  of  a  certain  kind.  It  specifies  both  the  data  and  the  code  that  will 
operate  on  that  data. Java uses a class specification to construct objects. Objects are 
instances  of  a  class.  Thus,  a  class  is  essentially  a  set  of  plans  that  specify  how  to 
build an object. 

Sample UML Class Diagram of Pokemon Object 

● Encapsulation  -  is  a  programming  mechanism  that  binds  together  code  and  the 
data  it  manipulates, and that keeps both safe from outside interference and misuse. 
In  an  object-oriented  language,  code  and  data  can be bound together in such a way 
that  a  self-contained  black  box  is  created.  Within  the box are all necessary data and 
code. When code and data are linked together in this fashion, an object is created. 
● Inheritance  -  is  the  process  by  which  one  object  can  acquire  the  properties  of 
another  object.  Using  inheritance,  an  object  need  only  define  those  qualities  that 
make  it  unique  within  its  class.  It  can  inherit  its  general  attributes  from  its  parent. 
Thus,  it  is  the  inheritance  mechanism  that  makes  it  possible  for  one  object  to  be  a 
specific instance of a more general case. 

 

 

 
 

● Object  -  Software  objects are conceptually similar to real-world objects: they consist 


of  state  and  related  behavior.  An  object  stores  its  state  in  ​fields  (variables)  and 
exposes  its  behavior  through  ​methods  (functions  in  some  programming languages). 
Methods  operate  on  an  object's  internal state and serve as the primary mechanism 
for  object-to-object  communication.  Hiding  internal  state  and  requiring  all 
interaction  to  be  performed  through  an  object's  methods  is  known  as  ​data 
encapsulation​ — a fundamental principle of object-oriented programming. 
● Polymorphism  -  is  the  quality  that  allows  one  interface  to  access  a general class of 
actions.  It  is  often  expressed  by  the  phrase  “one  interface,  multiple  methods.”  This 
means  that  it  is possible to design a generic interface to a group of related activities. 
Polymorphism  helps reduce complexity by allowing the same interface to be used to 
specify  a  general  class  of  action.  It  is  the  compiler’s  job  to  select  the  specific  action 
(i.e., method) as it applies to each situation. 

Laboratory Familiarization Activities: 

1. Hello LBYCPEI World! 

● Compile and run the following sample “ Hello World!” program: 

Main.java 

 

 

 
 

Output: 

The  one  shown  at  the  top  of  the  program  is  called  a  ​multiline  comment​.  This  type  of 
comment  must  begin  with  /*  and  end  with  */.  Anything  between  these  two  comment 
symbols  is  ignored  by  the  compiler.  As  the  name  suggests,  a  multiline  comment  may  be 
several lines long. 

The next line of code in the program is shown here: 

public​ ​class​ Main { 

This  line  uses  the  keyword  class to declare that a new class is being defined. As mentioned, 


the  class  is  Java’s  basic  unit  of  ​encapsulation​.  Main  is  the  name  of  the  class.  The  class 

 

 

 
 

definition begins with the opening curly brace ( { ) and ends with the closing curly brace ( } ). 
The elements between the two braces are members of the class. 

The next line of code is shown here: 

public​ ​static​ ​void​ main ​(String args[])​ { 

This  line  begins  the  ​main( )  method.  As  mentioned  in terminology, in Java, a subroutine is 


called  a  ​method​.  This  is  the  line  at  which  the  program  will  begin  executing.  All  Java 
applications begin execution by calling m
​ ain( )​.   

The ​public keyword is an access modifier. An ​access modifier determines how other parts 
of  the  program  can  access  the  members of the class. When a class member is preceded by 
public​,  then  that  member  can  be  accessed  by  code  outside  the  class  in  which  it  is 
declared.  (The  opposite  of  ​public  is  ​private​,  which  prevents  a  member  from  being  used 
by  code  defined  outside  of  its class.) In this case, ​main( ) must be declared as public, since 
it must be called by code outside of its class when the program is started.  

The  keyword  ​static  allows  ​main( )  to  be  called  before  an  object  of  the  class  has  been 
created.  This  is  necessary  because  ​main( )  is  called  by  the  JVM  before  any  objects  are 
made. The keyword v
​ oid​ simply tells the compiler that ​main( )​ does not return a value. 

As  stated,  ​main( )  is  the  method  called  when  a  Java  application  begins.  Any  information 
that  you  need  to  pass  to  a  method  is  received  by  variables  specified  within  the  set  of 
parentheses  that  follow  the  name  of  the  method.  These  variables  are  called  ​parameters​.  If 
no  parameters  are  required  for  a  given  method,  you  still  need  to  include  the  empty 
parentheses.  In  ​main( )  there  is  only  one  parameter,  ​String args[ ]​,  which  declares  a 
parameter  named  ​args​.  This  is  an  array  of  objects  of  type  String.  Objects  of  type  String 
store  sequences  of  characters.  In  this  case,  args  receives  any  command-line  arguments 
present when the program is executed. 

The  last  character  on  the  line  is  the {. This signals the start of main( )’s body. All of the code 


included  in  a  method  will  occur  between  the  method’s  opening  curly  brace  and  its  closing 
curly brace. 

The next line of code is shown here. Notice that it occurs inside main( ). 

 

 

 
 

System.out.println(​"Hello LBYCPEI!"​); 

This  line  outputs  the  string "Hello LBYCPEI!" followed by a new line on the screen. Output is 


actually  accomplished  by  the built-in ​println( ) method. In this case, ​println( ) displays 
the  string  that  is  passed  to  it.  As  you  will  see,  ​println( )  can  be  used  to  display  other 
types  of  information,  too.  The  line  begins  with  ​System.out​.  System  is  a  predefined  class 
that  provides  access  to  the  system,  and  out  is  the  output  stream  that  is  connected  to  the 
console.  Thus,  ​System.out  is  an  object that encapsulates console output. The fact that Java 
uses an object to define console output is further evidence of its object-oriented nature. 

Finally,  notice  that  a  Main.class  file  is created in the output after compiling and running the 


source code which corresponds to Java Program Execution as shown in Fig 1. 

2. Hello World Console Program and Graphics Program! 

Starter Project: h
​ ttps://github.com/melvincabatuan/ACMStarter_HelloWorld  

Throughout  the  course,  we  will  be  using  the  ACM  (Association  for  Computing  Machinery) 
Java Libraries which includes built-in Console based and Graphics based objects: 

Documentation: ​https://cs.stanford.edu/people/eroberts/jtf/javadoc/student  

 
10 
 

 
 

A. Running Hello World Console Program 


● Compile  and  run  the  “Hello  World!”  program  in  ACM’s  ConsoleProgram  and 
GraphicsProgram.  This  procedure  requires  the  acm.jar  which  will  be  provided  by 
your instructor. ( ​https://cs.stanford.edu/people/eroberts/jtf/​ ) 
● Adding .jar dependency in IntelliJ IDE: 

File  ->  Project  Structure  ->  Modules  ->  Dependencies  ->  ‘+’  ->  JARs  or  dir…  ->  (select 
acm.jar location)  

 
acm.jar as module dependency 

As  you’ve  experienced  in  your  previous  programming courses, the simplest program to get 


started  is  the  console  program  where  you  would  see  black  screen  window  popping  up 
when  you  run  the  application.  So,  we  start  now  by  running  Hello  World  using  the  ACM 
package.  The  difference  here  compared  to  the  previous  activity  is  that  we  ​extend  the 
ConsoleProgram as shown below: : 

​/*
* File: Main.java
* ---------------------
* Remember, if you change the class name, you'll need to change the filename so that it
matches.
* Then you can extend GraphicsProgram, ConsoleProgram, or DialogProgram as you like.
*/
import​ acm.program.*;
public​ ​class​ Main ​extends​ ConsoleProgram {
public​ ​void​ run​()​ {
println(​"Hello LBYCPEI OOP!"​);
}
// Solves java.lang.NoClassDefFoundError
public​ ​static​ ​void​ main ​(String [] args)​{
(​new​ Main()).start(args);
} } 

Expected Output: 

 
11 
 

 
 

At  this  point,  try  to  run  the  code.  You  should  now  see  a  dialog  pop  up  similar  to  the  one 
shown in the figure below. 

B. Running Graphics program “Hello World!” 

Say we want to create an app just like a Steam Client where users can now interact via 
mouse, then what we want is a Graphical User Interface. To get started with the ACM 
Graphics counterpart, we would now extend the GraphicsProgram rather than 
ConsoleProgram as shown below: 

/*
* File: Main.java
* ---------------------
* This class is a blank one that you can change at will. Remember, if you change
* the class name, you'll need to change the filename so that it matches.
* Then you can extend GraphicsProgram, ConsoleProgram, or DialogProgram as you like.
*/
import​ acm.graphics.GLabel;
import​ acm.program.*;
public​ ​class​ Main ​extends​ GraphicsProgram {
public​ ​void​ run​()​ {
GLabel text = ​new​ GLabel(​"Hello LBYCPEI OOP!"​);
add(text, ​100​, ​100​);
}
// Solves java.lang.NoClassDefFoundError
public​ ​static​ ​void​ main ​(String [] args)​{
(​new​ Main()).start(args);
}

 
12 
 

 
 

After  building  and  running  the  program,  a  dialog  should  pop  up  similar  to  the  one  shown 
below. Note: that is a Label component (i.e. a text box that you can’t edit). 

Laboratory Exercises: 

1. Hello Karel Object! 

​ ttps://github.com/melvincabatuan/KarelStarter  
Starter Project: h

As  defined  in  the  terminology,  an  object  stores  its  state  in  ​fields (variables) and exposes its 
behavior  through  ​methods​.  In  this  activity,  we will be exploring a simple object called Karel ‘ 
the Robot’.  

A.) Draw  the  UML  class  diagram  of  Karel  the  Robot  based  on  the  simplified  Karel.java 
provided  by  your  instructor.  (Hint:  Review  the  sample  UML  class  diagram  of 
Pokemon object given in Terminology section of a Class) 

 
13 
 

 
 

Karel UML Class Diagram 

B.) With  the  knowledge  of  Karel  object  attributes  and  behavior,  solve  a  simple 
story-problem  in  Karel’s  world.  Suppose  that  Karel  has  settled  into  its  house,  which 
is the square area in the center of the following diagram: 

Karel  starts  off  in  the  northwest  corner  of  its  house  as  shown  in  the  diagram.  The 
problem  you  need  to  get Karel to solve is to collect the newspaper—represented (as 
all  objects  in  Karel’s  world  are)  by  a  beeper—from  outside the doorway and then to 
return  to  its  initial  position,  facing  east.  (Problem  taken  from  cs106a  course  in 
Stanford University)(Troccoli, 2017) 

Note:  ​The  laboratory  instructor  will  provide  you  with  the  starter  files  and 
demonstrate how to run Karel’s instructions in the world​.  

Thus, all you have to do is write the sequence of commands necessary to have Karel  

1.) Move to the newspaper 

2.) Pick it up, and 

3.) Return to its starting point.  

Even  though  the  program  is  short,  it  is  still  worth  getting  practice  with 
decomposition.  In  your  solution,  include  a  method  for  the  first  and  third  steps 
above. 

 
14 
 

 
 

Draw  the  UML  Class  diagram  of  your  final  MyKarel  class  based  on  MyKarel.java  in 
relation to the Karel class in activity 3-A. 

Set-up: 

Go to Newspaper: 

 
15 
 

 
 

Pick Newspaper: 

Return to starting point 

 
16 
 

 
 

● C.)  Karel  has  been  hired  to  repair  the  damage  done  to  the  Quad  in  the  1989 
earthquake.  In  particular,  Karel is to repair a set of arches where some of the stones 
(represented  by  beepers,  of  course)  are  missing  from  the  columns  supporting  the 
arches, as follows: (Troccoli, 2017) 

As  shown  above, when Karel is done, the missing stones in the columns should be replaced 


by  beepers.  Karel’s  final  location  and  the  final  direction  it  is  facing  at the end of the run do 
not  matter.  Your  program  should work on the world shown above, but it should be general 
enough to handle any world that meets the following conditions: 

• Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in 
Karel’s beeper bag. 

• The columns are exactly four units apart, on 1st, 5th, 9th Avenue, and so forth. 

•  The  end  of  the  columns  is  marked  by  a  wall  immediately  after  the  final column. This wall 
section  appears  after  13th  Avenue  in  the  above  example,  but  your  program  should  work 
for any number of columns. 

•  The  top  of  the  column  is  marked  by  a  wall,  but  Karel  cannot  assume  that  columns  are 
always five units high, or even that all columns are the same height. 

•  Some  of  the  corners  in the column may already contain beepers representing stones that 


are still in place. Your program should not put a second beeper on these corners. 

For examples, there are several sample worlds in the starter project, and your program 

 
17 
 

 
 

should work correctly with all of them. 

Draw  the  UML  Class  diagram  of  your  final  StoneKarel  class  based  on  StoneKarel.java  in 
relation to the Karel class in activity 3-A. 

● D.) In the next activity, Karel should create a checkerboard pattern of beepers 
inside an empty rectangular world, as illustrated in the following before-and-after 
diagram. (Karel’s final location and the final direction it is facing at the end of the run 
do not matter.) (Troccoli, 2017)

Karel  starts  at  1st  Avenue  and  1st  Street,  facing  east,  with  an  infinite  number  of 
beepers in its bag. The initial state of the world includes no interior walls or beepers. 
Your  program  should  put  the  beepers  in  exactly  the  squares  shown  and  start 
checkering  by  putting  a  beeper  down  on  (1,  1).  For  example,  in  the  picture  above, 
Karel  has  placed  beepers  at  (1,  1),  (3,  1),  …  (2,  2),  (4,  2),  etc.  If  you  were  to  put 
beepers  on  the  opposite  squares,  such  as (2, 1), (4, 1), …, (1, 2), (3, 2), etc., this would 
also  be  a  checkerboard  pattern,  but  it  would  not exactly match the expectations for 
this problem. In a 1x1 world, Karel should place a beeper on (1, 1). 

As  you  think  about  how  to  solve  the  problem,  you  should  make  sure  that  your 
solution  works  with  checkerboards  that  are  different  in  size  from  the  standard  8x8 
checkerboard  shown  in  the  example  above.  As  an  example,  odd-sized 

 
18 
 

 
 

checkerboards  are  tricky,  and  you  should  make  sure  that  your  program  generates 
the following pattern in a 5x3 world: 

Another  special  case  you  need  to  consider  is  that  of  a  world  which  is  only  one 
column wide or one row high. The starter folder contains several sample worlds that 
test  these  special  cases,  and  you  should  make  sure  that  your  program  works  for 
each of them. 

Draw the UML Class diagram of your final CheckerboardKarel class solution. 

● E.) Program Karel to place a single beeper at the centroid of the world. 

Remember  that  you  must  solve  this  problem  using  only  the  syntax shown in 
the Karel course reader; ​You may not use Java variables​. 

Sample Centroid Karel Solutions 

Draw the UML Class diagram of your final CentroidKarel class solution. 

 
19 
 

 
 

2. OOP Design 

● Draw  the  UML  Class  diagram  of  the  KarelDemo  application  utilized  in  activity  1-B 

which  consists  of  Karel  class,  MyKarel  class,  and  (Main  class)  highlighting  their  UML 
relationships as discussed in the UML section above.  
● The  stanford.karel  package  includes  a  SuperKarel  object  that  includes  several  new 
features  while  retaining  the  basic  Karel  methods.  The  SuperKarel  class  includes 
definitions  for  turnRight()  and  turnAround().  The  SuperKarel  implementations  of 
these  methods  tie into the internal workings of Karel to implement these operations 
more  efficiently.  If  you  use  turnRight()  in  a  SuperKarel  subclass,  your  program  will 
make  an  immediate  right  turn  and  not  go  through  the  process  of  turning  left  three 
times.  Given  the  stated  information,  draw  the  UML  Class  diagram  of  SuperKarel  in 
relation to Karel utilizing the Object-Oriented design approach. 
● The  SuperKarel  class  allows  Karel  to  paint  the  corner  on  which  it  is  standing  using 
the instruction: 

paintCorner(color); 

The  value  enclosed  in  the  parentheses—which  is  called  an  ​argument  in  the 
terminology of programming languages—may be any of the following: 

BLACK  BLUE  CYAN  DARK_GRAY  GRAY 

GREEN  LIGHT_GRAY  MAGENTA  ORANGE  PINK 

RED  WHITE  YELLOW  null   

The  null  color  value  indicates  that  a  corner  has  no  color, which means that the little 
cross  in  the  center  shows  through.  When  you  create  a  new  Karel  world,  all  corners 
initially have null as their color. 

Repeat  exercise  1-D,  but  instead  of  creating  a  checkerboard  pattern  of  beepers, 
make a checkerboard pattern of green boxes using the method:  

paintCorner(color) 

 
20 
 

 
 

Draw  the  UML  Class  diagram  of  this  application  which  consists  of  Karel  class, 
SuperKarel  class,  MyKarel  class,  and  Main  class  highlighting  their UML relationships 
as discussed in the UML section above.  

References: 

● Gosling, J., Joy, B., Steele, G., Bracha, G., & Buckley, A. (2015). T​ he Java language 
specification​. Oracle America, Inc. 
● Roberts, E. (2005). Karel the robot learns java. Department of Computer Science 
Stanford University. 
● Roberts, E. (2008). The Art & Science of Java. Pearson. 
● Oracle. Java Documentation. ( ​https://docs.oracle.com/en/java​ ) 
● Schildt, H. (2017). Java: A Beginner's Guide, 7th Edition. McGraw-Hill Education. 
● Troccoli, N. (2017). CS 106A: Assignment #1: Karel the Robot. Stanford University 
● UML basics: The component diagram. 
http://www.ibm.com/developerworks/rational/library/dec04/bell/ 

   

 
21 
 

 
 

Appendix: 

​ ( from Karel The Robot Learns Java by E. Roberts ) 

 
22 

Potrebbero piacerti anche