BM

Unit 1 - Java Robotics Lab Vocabulary

Objective and Background

  • This unit introduces Java basics in the context of Lab 00–Lab 05 using Karel the Robot (KARE12) framework.
  • Text editor recommendation: jGRASP (free from Auburn University).
  • Setup guidance: Ask the teacher about the text editor and Java environment being used.
  • Demonstration map/editor workflow: create and save Java files, compile, and run to observe robot behavior on maps.

Lab Setup and Specification

  • Create a new Java file Lab00 in the Unit1 folder.
  • The sample Lab00.java loads a map and configures the display:
    • Display.openWorld ("maps/first.map");
    • Display.setSize (10, 10);
    • Display.setSpeed (5);
  • Import statements bring in classes from packages, e.g.,
    • import edu.fcps.kare12.Display;
    • import edu.fcps.kare12.Robot;
  • Class declaration and structure:
    • public class Lab00
    • public static void main(String[] args)
    • The main method executes statements in order.
  • The code creates a Robot object and invokes its methods:
    • Robot karel = new Robot();
    • karel.move();
    • karel.pickBeeper();
    • karel.move();
    • karel.putBeeper(); (example from the notes)
    • karel.turnLeft();
  • Key terminology:
    • A class is a blueprint; an object is an instance created from the class.
    • A constructor initializes an object; the new keyword instantiates the object.
    • A line like //Torbert, e-mail: smtorbert@fcps.edu is a comment and not compiled.
  • File naming rule: the file name must exactly match the public class name (e.g., Lab00.java for public class Lab00).
  • After writing code:
    • Save, Compile, and Run (CSD). You should see a map and a robot performing tasks.
  • The initial state of a Robot and what can be customized via constructors:
    • Example constructors shown in the notes:
    • Robot ophelia = new Robot();
    • Robot horatio = new Robot(5, 4, Display.SOUTH, 37);
  • Explanation of the sample state:
    • A Robot’s initial state includes its position, direction, and beepers.
    • The openWorld method is a class method of the Display class.
  • Map and coordinate background:
    • Maps are Cartesian; bottom-left corner is (1, 1).
    • Maps stored in Unit1/maps.
    • Default maps may include specific walls (e.g., southern and western infinite walls).
  • Quick conceptual notes:
    • The compiler ignores whitespace; formatting is not important for correctness, but readable code is encouraged.
    • Braces denote blocks; indentation is for readability and isn’t required by the compiler.

Basic Object-Oriented Concepts: Classes, Objects, and Methods

  • A class contains data (fields) and behavior (methods).
  • An object is an instance of a class and can perform actions via its methods.
  • Example analogy: A class is like a house blueprint; an object is the actual house built from the blueprint.
  • Instance methods vs class methods:
    • Instance methods (e.g., move, turnLeft) require an object reference.
    • Class methods (static) belong to the class as a whole (e.g., Lab04.takeTheField).
  • The main method is a class method: public static void main(String[] args).

Identifiers, Naming Conventions, and Syntax

  • Identifiers can include letters, digits, and underscores; they cannot begin with a digit and cannot be Java keywords.
  • Java is case-sensitive: lisa != Lisa.
  • Naming convention: class names start with an uppercase letter; methods/variables start with lowercase; constants use ALL CAPS (e.g., EAST).
  • The concept of instantiation: creating an object from a class using new.

Maps, Coordinates, and Display Details

  • Maps use Cartesian coordinates; bottom-left is (1, 1).
  • Predefined maps are stored in Unit1/maps.
  • Display.openWorld and related Display methods configure the viewing window, map, size, and speed.
  • The default map used when none is specified is a simple 10x10 grid with two infinite walls on the southern and western edges.

Inheritance: Athlete extends Robot

  • Inheritance allows a subclass to reuse the superclass’s methods and fields without rewriting them.
  • The keyword extends expresses an is-a relationship: Athlete is a Robot.
  • Subclasses do not inherit constructors; they must call a superclass constructor via super(…).
  • Example skeleton:
    • public class Athlete extends Robot {
    • public Athlete() { super(1, 1, Display.NORTH, Display.INFINITY); }
    • public Athlete(int x, int y, int dir, int beep) { super(x, y, dir, beep); }
    • public void turnAround() { /* implement */ }
    • public void turnRight() { /* implement */ }
    • }
  • The object hierarchy for the labs typically follows: Object -> Item -> Robot -> Athlete (and subclasses like Climber, Racer, etc.).
  • Practical notes:
    • A driver class (e.g., Lab02) contains the main method and may instantiate an Athlete object and invoke its methods.
    • A resource class (e.g., Athlete) contains the domain logic (methods like turnRight/turnAround) that operate on its own data.
  • Example object creation from the notes:
    • Athlete ariadne = new Athlete(1, 2, Display.EAST, 10);

Constructors and the super() Chain

  • A constructor initializes an object’s fields and performs any necessary setup.
  • The super(…) call forwards constructor arguments to the superclass constructor.
  • Inheritance chain example: Climber extends Athlete, which extends Robot, which extends Item, which extends Object.
  • Rules about constructors:
    • The constructor name must match the class name.
    • Constructors are not labeled with a return type (no void, etc.).
    • The super(…) call must resolve to a matching constructor in the superclass chain.
    • If you provide a parameterized constructor, you should also provide a no-argument constructor if the chain might need it later.
  • Example context from the notes:
    • The 4-arg Robot constructor might be public Robot(int x, int y, int dir, int beep).
    • Athlete’s constructor calls super to initialize the Robot fields.

Driver vs Resource Classes; Main Method vs Object Methods

  • Driver class: contains the main method and orchestrates the program; ability to run without having to instantiate the class.
  • Resource class: contains the actual behavior; actions are performed on instances of the resource class.
  • Conceptual difference:
    • Driver class can run directly (via main).
    • Resource class must be instantiated (via new) before its methods can be invoked.
  • Lab04 demonstrates a class method takeTheField(Athlete arg) that operates on an Athlete passed as an argument.

The Compile-Run Cycle and Java Build Process

  • Compilation step: javac LabXX.java compiles source to bytecode (.class).
  • The compiler performs several checks: lexical errors, syntax errors, type correctness, and missing constructors.
  • Runtime: java LabXX.class executes the main method and runs the program.
  • The flow is often summarized as:
    • Write code -> save -> javac LabXX.java -> LabXX.class -> java LabXX
  • Important point:
    • A resource class’s methods are invoked via objects; a driver class’s main method is invoked by the runtime.

Primitive vs Object Types

  • Primitive data types: int, double, boolean (stored directly in memory).
  • Object types are accessed via references (pointers to heap-allocated objects).
  • Example of the three steps when writing: Robot karel = new Robot();
    • Creates a reference named karel
    • Allocates a new Robot object
    • Assigns the reference to point to that object
  • Multiple references can refer to the same object.

For-Loops: Definite Iteration and Syntax

  • For-loops enable repeating a block a known number of times:
    • Example: for (int k = 1; k <= 6; k++) { karel.move(); karel.putBeeper(); }
  • Loop control variables:
    • Initialization: int k = 1
    • Condition: k <= 6
    • Update: k++ (increment by 1)
  • The loop runs exactly 6 times; after the loop, the value of k is 7.
  • Variants:
    • for (int k = 1; k <= n; k++) { … } // repeats n times
  • Braces: braces are optional for single-statement bodies.

Lab Racer: For-Loops and Advanced Methods

  • Racer extends Athlete; supports moving multiple steps and manipulating beepers.
  • Constructor pattern:
    • public Racer(int y) { super(1, y, Display.EAST, 0); }
  • New capabilities include:
    • sprint(int n): moves n times using a for-loop.
    • put(int n): places n beepers using a for-loop.
    • pick(int n): picks up n beepers using a for-loop.
    • shuttle(int spaces, int beepers): moves across spaces and handles beepers (to be implemented with loops).
  • A class method such as runTheRace may be used to coordinate multiple Racer instances, illustrating static methods and parameterized control.

Discussion: Error Handling and Debugging

  • Types of errors:
    • Lexical errors: misspelled tokens; detected by the compiler.
    • Syntax errors: incorrect grammar; detected by the compiler.
    • Runtime errors: errors during execution (e.g., moving into a wall or manipulating non-existent beepers).
    • Logic errors: the program runs but fails to meet the intended goal.
  • Debugging workflow:
    • Generate CSD (Compile-Save-Run) after a few lines to isolate syntax errors.
    • Comment out sections to isolate logic errors; in jGrasp, use Ctrl-/ to toggle comments.
  • Lab01 exercises involve injecting and observing errors to understand compiler messages.

UML: Relationships Between Classes

  • UML diagrams illustrate relationships between interacting classes with arrows:
    • Dashed, horizontal arrows indicate has-a (composition/association) relationships.
    • Thick, vertical arrows indicate is-a (inheritance) relationships.
  • Example from notes:
    • Lab02 is composed of Lab02, Display, Robot, and Athlete; label arrows with isa or hasa.
    • Lab01 includes Lab01, Display, and Robot with has-a relationships.

Quick Reference: Core Concepts and Terms

  • Compile: process of turning source code into bytecode.
  • Bytecode: the portable instructions executed by the Java Virtual Machine (JVM).
  • main method: entry point for runnable Java applications; public static void main(String[] args).
  • super: invokes a superclass constructor from a subclass constructor.
  • extends: declares inheritance between classes.
  • static: denotes a class method (belongs to the class, not to an instance).
  • For-loops: used for definite iteration; syntax for numeric control variable.
  • Driver vs Resource class distinction: driver runs main; resource classes contain object behavior invoked via instances.

Selected Exercises and Answers (from Unit 1 pages)

  • 1) What is compiling? Turning source code into bytecode.
  • 2) Define bytecode. The result of compiling, which enables the program to run on the JVM.
  • 3) To call the superclass constructor, use the super keyword.
  • 4) Syntax errors involve violations of Java grammar.
  • 5) The extends keyword denotes inheritance.
  • 6) If a line is preceded by //, that line is a comment and not compiled nor run.
  • 7) In the Karel-the-Robot hierarchy, the precise is-a relation is Athlete extends Robot (Robot is not a sub-type of Athlete).
  • 8) A resource class may have more than one constructor; it is not required to have multiple constructors, but you can define them.
  • 9) The default Athlete faces north.
  • 10) Passing different arguments to a constructor creates objects in different states.
  • 11) Example: Athlete alice = new Athlete();

Notes on Map Locations and File Paths

  • Lab-specific map setup uses file paths like maps/first.map and requires proper pathing relative to the project structure.
  • Ensure that map files exist in the appropriate Unit1/maps directory when loading with Display.openWorld.

Practical Takeaways

  • Always ensure the file name matches the public class name (e.g., Lab00.java for Lab00).
  • Understand the separation between driver classes (with main) and resource/domain classes (like Robot, Athlete).
  • Practice with for-loops to perform repetitive robot actions, particularly with move(), putBeeper(), and pickBeeper().
  • Recognize the importance of constructors and the super call in establishing the initial state of objects across an inheritance chain.