COMPUTER SCIENCE Summer 2007
Review for Unit 1 Quiz Labs00-04
- p. 3 Discussion: File Management
- Source code - your Java program .java
- Compiling, bytecode - .class
- Source code translated to bytecode
- Running the bytecode
- Background of JKarel class Robot
- Lab00:Hello Robot - classes, objects, sending messages (Robot commands), program structure and syntax
- JGrasp - a text editor
- Java comments - begin with //
- import keyword
- class and filename must match
- left braces and right braces
- Java applications have a main method
- calling Class methods, ex. World.setSize(8,8)
- Java classes: Robot, World
- creating a Java object with new: karel, an instance
- calling instance methods: ex. karel.move()
- World methods (commands) are World.readWorld(), World.setSize(), World.setSpeed()
- Robot methods (commands) are move(), turnLeft(), pickBeeper(), putBeeper()
runtime errors: move() into a wall. pickBeeper() when there is no beeper. putBeeper() when there are no beepers.
- Robot is a class (begins with a capitol letter)
- karel is an object of a Robot (is instantiated with new)
- Lab00 Exercises p. 5
- Robot object's default initial conditions
- x,y coordinates and avenues, streets in the Robot World
- Directions Robot can face
- Number of beepers the Robot has
- What do particular instances, ophelia and horatio, of Robot's know
- How to write command to create an instance of a Robot with particular x y location, direction, and number of beepers
- Default Robot world, setting the size of a Robot World
- How to move a Robot, put down and pick up beepers
- What does class World know, what does class Robot know?
- Java Applications p. 6
- Standard structure of a Java application - your program
- main method - public static void main(String[] args)
- How to name your Java file - same name as class
- .java
- Compiling, checking for syntax errors - javac
- bytecode - .class
- Running bytecode - java
- The need for a main method
- chart sequence at bottom of page, compiling, syntax errors, running, run time errors
- Lab01: Student and Teacher - p. 7
- World structure - avenues (x) run n/s and streets (y) run e/w
- (1,1) at lower left
- identifier: name of a class, object, method - begins with a letter, any sequence of letters, numbers, underscore. Case sensitive, cannot be a Java keyword like class.
- Class names begin uppercase. Method names start lowercase, then uppercase for more than one word in the name, like pickBeeper(). Constants are all caps - World.EAST
- instantiate - create an instance (an object)of a class. Use the word new
- objects know how to do things via methods
- send a message to an object with "dot" notation: maria.move()
- Exercises Lab01 p. 8
- know how to read an API, for example - Robot, World, Athlete
- valid identifiers
- locations on the grid for World, avenues (x) and streets (y)
- identifying the size of a World, using World.setSize()
- nouns are like objects, verbs are like methods
- private attributes of each Robot object
- Errors - p. 9
- Errors at compile time - lexical (using an unknown or misspelled identifier) and syntax (broken a grammatical rule like leaving off a semicolon)
- Errors not caught by the compiler: Runtime (robot runs into a wall) and logic (program completes, but the result is incorrect) errors.
- Use the CSD from JGrasp editor, can help quickly identify some syntax errors before compiling. CSD also helps identation of your program, makes the program easier to read.
- Using commented out sections to debug logic errors. Isolate small sections of code to verify for correctness. // for single lines, and /* ... */ for multiple lines/blocks of comments.
- debugging - from Grace Hopper
- Lab02- Escape the Maze
- Inheritance - using extends. Athlete extends Robot
- Defining new instance methods in addition to move(), pickBeeper(), turnLeft() - turnAround() and turnRight()
- Exercises Lab02 p. 11
- Writing a sequence of commands to accomplish a task with Robot and beeper
- classes used so far - World, Robot, Lab00, Lab01
- isa and hasa relationships - isa deals with inheritance, Athlete isa Robot (not Robot isa Athlete), hasa is like contains - Lab02 hasa Athlete
- Discussions p. 12
- Resource (Athlete) vs Driver (Lab02) classes. Driver classes contain main
- References (pointers) vs Type declarations
- references use new to create instances of an object
Robot karel - creates a reference (pointer)
new Robot() instantiates a new object of Robot class
= assigns the new object to the reference pointer.
- Objects know private data keeping track of their state or attributes kept in fields (such as position and number of beepers). Each object has its own copy of instance fields - karel and pete Robots each can have different locations/number of beepers.
- Primitive data types are stored in memory locations, not as pointers.
- int, double, boolean are examples
- public Athlete(int x, int y, int dir, int beep) creates 4 memory spaces for integers but does not assign values.
- int, double, boolean are primitive data types (direct references), robots are references to objects (indirect references).
- Discussion - constructors p. 13
- Constructors specify the private data of an object when it is instantiated with "new".
- Using a newly written constructor, Athlete's can specify their x/y position, direction, and number of beepers at instantiation.
- Athletes have 2 constructors - a default constructor with 0 arguments and a constructor with 4 arguments.
- Each Athlete constructor uses super to call the constructor for Robot - Athlete's superclass (Athlete extends Robot).
- The 0 argument constructor (the default constructor) uses automatically the values: 1,1,World.NORTH, World.INFINITY for the x/y position, direction and number of beepers.
- The 4 argument constructor allows the programmer to specify x/y position, direction, and number of beepers at instantion.
Ex. Athlete sue = new Athlete(3,4, World.EAST, 10)
Ex. of default constructor: Athlete sam = new Athlete()
- The name of the constructor is the same as the name of the class
- Constructors are not labeled with a keyword such as void or static.
- Lab03 Climb Every Mountain, p. 14
- Climber extends Athlete (which extends Robot)
- Class hierarchy:
- Climber's superclass is Athlete. Athlete's superclass is Robot.
- Robot's subclass is Athlete. Athlete's subclass is Climber.
- Climber has a 1 arg constructor. Look at the super call. The on argument, x, is used in the super call to Athlete's 4 argument constructor. It specifies the x position of the Athlete (a Climber).
- Climber has no 0 arg constructor.
Climber pete = new Climber() will not compile.
Climber pete = new Climber(5) will. The x position of pete is 5
- Note the class hierarchy diagram: Climber isa Athlete. Athlete isa Robot. The root here is Robot.
- All class in Java isa Object.
- Know the words superclass and subclass and what they mean. Athlete is a superclass of Climber. Climber is a subclass of Athlete.
- Execises Lab03 and Discussion - Passing Arguments, p. 15
- Writing a 1 argument constructor for Class Elf, a subclass of Robot.
- Writing a 2 argument constructor for Class Spartan, a subclass of Athlete
- Passing arguments to constructors. actual (in the driver program) and formal (in the Class definition) arguments
- Passing objects as arguments. takeTheField(maria)
- Lab04 Take the Field, p. 16
- instance methods - known for all instances of an object. All Robots know how to turnLeft(), move(), pickBeeper().
- class methods - known only for a particular class. Lab04 class knows how to takeTheField.
- class methods usually use static as part of their definition
- The class method takeTheField() passes an object as an argument - takeTheField(maria) ... not maria.takeTheField()