Unit 3 - Loops and Classes

  • Source Code: Human-readable text written using the Java programming language

  • Two-step Process: Compiling and Running

  • Compiling: Translation of source code into lower-level code called Bytecode

  • Compiling also checks for errors

  • Bytecode is sent over the internet, which is run on the local system.

  • Java apps require specific structure

    • public static void main method (entry point)

  • When you click the jGrasp “compile” button, you are running a separate program called javac

  • The compiler first checks for syntax errors

  • Class Name: UPPERCASE; Objects: LOWERCASE

  • Class Name: UPPERCASE;

  • Objects: lowercase;

  • Methods: camelCase;

  • Constructors: Same as Class Name.

  • Java compilers check for errors

    • Misspelling error = lexical error message

    • Syntax error = breaking rules of forming valid commands (grammatical errors)

    • Runtime error = when program runs and it crashed

    • Logic errors = when program runs but does not accomplish required task, which is the programmer’s fault

  • To help correct logic errors —> can try to comment out blocks of code

  • Process of finding errors and correcting them: Debugging

  • Errors themselves: Bug

  • Data type specifies how different kinds of data are stored and manipulated

  • Classes define complex types; encapsulate data and methods

  • Objects are accessed by references (act like little pointers)

  • Primitive data types —> not accessed by reference; stored directly in variables

    • int for integers

    • double for decimal

    • boolean for true/false

  • Type declaration assigns a name to memory space and assigns a value

  • Constructor method: method that creates an object and initializes its private data

  • The no-arg (default) constructor is easy to call, but always instantiates an object in the default state

  • A subclass inherits methos but not constructors; needs explicit call

  • The name of constructor method must match name of class

  • Constructors are NOT labeled with keyword

  • the call must find corresponding constructor in class in hierarchy above it

    • If compiler is unable to find corresponding constructors, it reports an error

  • If you write a constructor with arguments, then you should also define a no-arg constructor

  • Class method stores code that’s accessible to anyone, without first created an object

    • Mathematical formulas

    • Often controls environments for other objects

    • All class methods are marked b keyword static

    • Usually placed before main method

  • Instance method must be instantiated before being used

  • Other names for superclass:

    • base class

    • parent class

  • Oher names for subclass

    • derived class

    • child class

  • Not only do the subclasses inherit methods and fields, they also inherit the name of superclass

  • For loops

    • Each for loop has three parts

      • where k begins (Initialization)

      • where k ends (condition)

      • how k increases (Increment)

        • the command k++ causes the value of k to increase by 1

  • Any for loop can be rewritten as a while loop

  • For loops: definite loops

  • While loops: indefinite loops

  • If statement only makes one decision (NO SUCH THING AS IF-LOOPS!)

    • acts as a guard to prevent the program from crashing in certain situations

    • checks its condition once

  • Void methods take action and change the situation

  • Return methods provide info about a robot’s situation

  • Not operator: !

    • not - true = false

    • not - false = true

    • ! goes in front of the whole expression