Java Programming Essentials and OOP, COE 301 – Study Notes
offline

Difficulty: Beginner to Intermediate | Prerequisites: Basic Java syntax, variable declaration, loops.

Big Picture

This document covers the Java language mechanics and object-oriented programming concepts tested in COE 301. The exam checks whether you can trace expression evaluation (including type casting and operator precedence), manipulate Strings, use Scanner and ArrayList, and implement classes that extend a superclass. The Critter framework is a recurring exam scenario where you write subclasses that override specific methods to produce defined behaviours. If you can read a short Java snippet and predict its output, and if you understand how inheritance and polymorphism work, you are in good shape.


TL;DR

Java expression evaluation follows strict precedence rules, and casting can change the result dramatically. Strings are indexed from 0, and substring is inclusive of the start index and exclusive of the end. ArrayList elements shift left when one is removed. Inheritance lets a subclass object be assigned to a superclass reference (upcasting), and the super keyword calls the parent's constructor. The Critter framework tests your ability to implement fight, eat, and getMove with stateful behaviour.


Key Terms

Type casting (explicit cast)

Converting a value from one type to another using syntax like (int) 2.75. Casting to int truncates the decimal part (it does not round). The cast operator has higher precedence than arithmetic operators like * and /.

Integer division

Division between two int values in Java truncates the result. For example, 51 / 30 gives 1, not 1.7.

double

A floating-point numeric type. Any arithmetic expression involving at least one double operand produces a double result.

String.substring(start, end)

Returns the substring from index start (inclusive) to index end (exclusive). Characters are indexed from 0.

String.charAt(index)

Returns the single character at the given index. Index 0 is the first character.

Scanner

A class for reading input. A Scanner can be created from System.in, a File, or a String. Creating a Scanner from a String lets you parse tokens from a single line independently, which is useful in line-oriented file processing.

ArrayList

A resizable list backed by an array. Key methods: size() returns an int, get(int pos) retrieves an element, remove(int pos) removes an element and shifts all subsequent elements left (their indices decrease by 1), add(int pos, E element) inserts at a position.

Inheritance

A mechanism where one class (the subclass) extends another (the superclass), inheriting its fields and methods. A subclass object can be assigned to a superclass reference. This is called upcasting.

Polymorphism

The ability of a superclass reference to point to a subclass object and call the subclass's overridden methods at runtime. Room r = new Classroom(10, 50); is legal because Classroom extends Room.

super (keyword)

Used in a constructor to call the parent class's constructor, initialising inherited fields before the subclass adds its own.

final (keyword)

Prevents a variable from being changed after initialisation. A final variable is effectively a constant.

Math.random()

Returns a double greater than or equal to 0.0 and strictly less than 1.0. The range is [0.0, 1.0).

Critter (framework)

A simulation framework where animal-like objects move on a grid and fight each other. Each Critter subclass overrides methods like fight, eat, getMove, and optionally toString. Direction constants include NORTH, SOUTH, EAST, WEST, and CENTER.

Attack (enum)

The possible fight actions in the Critter framework: ROAR, POUNCE, SCRATCH, and FORFEIT.


Core Content

Java Expressions and Type Casting

The cast operator (int) has higher precedence than *, /, +, and -. This means (int) 2.75 * 10 first casts 2.75 to 2, then multiplies by 10, giving 20. If you want to cast the entire product, use parentheses: (int)(2.75 * 10) gives 27.

Another common exam expression: 10 + 3 * 17 / 30 + 2.5 * 2.0. Step through it:

  • 3 * 17 = 51 (integer multiplication)

  • 51 / 30 = 1 (integer division truncates)

  • 2.5 * 2.0 = 5.0 (double multiplication)

  • 10 + 1 + 5.0 = 16.0 (promoted to double because of 5.0)

  • Result: 16.0, type double

Another pattern: (int)(5 * 2 / 1.5). Here, 5 * 2 = 10, then 10 / 1.5 = 6.666..., then the cast truncates to 6.

String Methods

"The University of Texas".substring(4, 14) returns "University". Index 4 is 'U', index 14 is exclusive, so the last character included is at index 13 ('y').

Note: the model answer sheet for this exam prints "Universit" (without the final 'y'), but counting the characters carefully, index 4 through index 13 (inclusive) gives "University" (10 characters: U-n-i-v-e-r-s-i-t-y). There is a discrepancy in the source material here.

"Theory".charAt(1) returns 'h'. Index 0 is 'T', index 1 is 'h'.

Scanner for File Processing

Creating a Scanner from a String (Scanner line = new Scanner(str)) lets you parse tokens from an individual line independently. This is useful in line-oriented file processing because you can:

  • Read a full line from a file Scanner with nextLine()

  • Create a new Scanner from that line

  • Use nextInt(), next(), hasNext() on just that line's content without accidentally reading into the next line

ArrayList Operations

  • size() returns an int (the number of elements)

  • get(int pos) retrieves the element at that index

  • remove(int pos) removes the element and shifts all subsequent elements left by one index

  • add(int pos, E element) inserts at the specified position

When you remove an element at index 2, the element that was at index 3 moves to index 2, the element at index 4 moves to index 3, and so on. This matters when iterating: if you remove while looping forward, you can skip elements.

Inheritance and Polymorphism

If Classroom extends Room, then Room r = new Classroom(10, 50); is legal. This is upcasting: a subclass object can be stored in a superclass reference. The reference type (Room) determines which methods are visible at compile time, but the object type (Classroom) determines which overridden method runs at runtime.

The super keyword in a constructor calls the parent class's constructor. It must be the first statement in the subclass constructor. This initialises the inherited fields before the subclass adds its own.

The final keyword prevents a variable from being reassigned. It makes the variable a constant.

Critter Framework: JumpingBean

The JumpingBean follows specific rules:

  • Sits still (returns Direction.CENTER) until it wins a fight

  • On winning a fight, picks a random direction (NORTH or WEST using Math.random() < 0.5) and moves in that direction

  • First win: 2 moves. Second win: 4 moves. Third win: 6 moves. Each subsequent win adds 2.

  • Fights with Attack.SCRATCH when sitting still, but Attack.FORFEIT if currently in a movement phase

  • Needs instance variables to track: total steps to move, current step count, and chosen direction

Valid Critter direction constants: Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.CENTER. There is no Direction.STAY.

Critter Framework: Yak

The Yak Critter requires overriding three methods: fight, eat, and getMove. The toString method does not need to be overridden for the Yak.

Harry Potter Currency (printMoney)

The wizarding currency system: 29 Knuts = 1 Sickle, 17 Sickles = 1 Galleon. Therefore 1 Galleon = 29 × 17 = 493 Knuts. If 17 Sickles = 1 Galleon, then 34 Sickles = 2 Galleons.


Common Misconceptions

  • Students often think (int) 2.75 * 10 casts the entire product. The cast applies only to 2.75 first (giving 2), then the multiplication happens. Parentheses change everything.

  • Students sometimes confuse substring(4, 14) as inclusive on both ends. The end index is exclusive.

  • Students forget that x = 5 is an assignment in Java, not a boolean comparison. It does not produce a boolean value. Use x == 5 for comparison.

  • Students assume Direction.STAY is a valid Critter constant. It is not. The correct "do nothing" direction is Direction.CENTER.


Why It Matters / Exam Flags

⚠️ Cast-precedence questions appear repeatedly. Memorise: the cast (int) binds tighter than * and /.

⚠️ String indexing starts at 0, and substring end index is exclusive. Count characters carefully.

⚠️ JumpingBean: the exam asks you to implement fight and getMove. Track your instance variables, your step counter, and the direction. Remember FORFEIT during movement, SCRATCH when still.

⚠️ Know the Harry Potter currency conversions: 29 Knuts = 1 Sickle, 17 Sickles = 1 Galleon, 493 Knuts = 1 Galleon.

⚠️ The Yak Critter overrides fight, eat, and getMove only. Not toString.

⚠️ Math.random() returns [0.0, 1.0). The 1.0 is exclusive.


Quick Self-Test

  1. What is the result of (int) 2.75 * 10? (20)

  1. What does "Theory".charAt(1) return? ('h')

  1. True or false: x = 5 is a valid boolean expression in Java. (False, it is an assignment.)

  1. What does a JumpingBean do when it fights during its movement phase? (FORFEIT)

  1. How many Knuts equal one Galleon? (493)


Practice Q&A

Q: What is the resulting value and type of the Java expression 10 + 3 * 17 / 30 + 2.5 * 2.0?

A: 16.0, type double. Integer division of 51/30 gives 1, and the 2.5 * 2.0 promotes the result to double.

Q: What is the output of "The University of Texas".substring(4, 14)?

A: "University". Index 4 is 'U' and the end index 14 is exclusive.

Q: What is the purpose of the super keyword in a constructor?

A: It calls the superclass constructor to initialise inherited fields before the subclass initialisation.

Q: In the Room/Classroom hierarchy, is Room r = new Classroom(10, 50); legal?

A: Yes. A subclass object can be assigned to a superclass reference (upcasting/polymorphism).

Q: What is the return type of ArrayList.size()?

A: int.

Q: Which keyword prevents a variable from being changed after initialisation?

A: final.

Q: What is the range of Math.random()?

A: [0.0, 1.0). Greater than or equal to 0.0, strictly less than 1.0.

Q: Which of these are valid Java expressions that result in a boolean type: true && false, 5 > 2, Math.random() > 0.5, x = 5?

A: true && false, 5 > 2, and Math.random() > 0.5 are all boolean. x = 5 is an assignment, not boolean.

Q: Which directions can a JumpingBean choose after winning a fight?

A: NORTH or WEST (chosen randomly).

Q: Which are valid Critter direction constants: Direction.NORTH, Direction.CENTER, Direction.STAY, Direction.WEST?

A: Direction.NORTH, Direction.CENTER, and Direction.WEST. Direction.STAY does not exist.


Connections to Other Topics

Type casting and operator precedence connect to every programming task you will encounter in Java. Understanding how ArrayList works under the hood (shifting elements on remove) prepares you for data-structure courses. Inheritance and polymorphism are the foundation of object-oriented design and will recur in every course that uses Java. The Critter framework is a concrete exercise in state machines, where instance variables track which state the object is in and methods define transitions.


Related Terms / Search Tags

Java, type casting, int cast, double, integer division, operator precedence, String, substring, charAt, indexOf, Scanner, file processing, line-by-line parsing, ArrayList, size, get, remove, add, shift, inheritance, extends, polymorphism, upcasting, downcasting, super, final, constant, Math.random, random number, Critter, Direction, CENTER, NORTH, WEST, SOUTH, EAST, Attack, SCRATCH, FORFEIT, ROAR, POUNCE, JumpingBean, Yak, getMove, fight, eat, getReversedSubList, printMoney, Knuts, Sickles, Galleons, Harry Potter currency, COE 301, University of Texas