Old notes from 2023

Agenda

  • What is Java

    • A JIT (compiled “Just-In-Time”) language

      • Compiled into Java bytecode

      • Then bytecode is translated into machine code on target platform

      • Makes code more cross-platform than C/C++, which is directly compiled

    • Statically typed

      • Every variable has a “type”

      • E.g. “int” == integer number, “double” == decimal number, “string” == text, etc

      • Lower case type is primitive type: basic type included with java

      • Upper case type is class type: complex type, see next

  • Object oriented programming

    • Everything is an “object” of a certain “class”

    • Each class can have “variables” and “functions”

    • There’s also “constructors”, which are methods that run when you create an object

    • There’s also modifiers: “public”, “private”, “readonly”, “static”

    • Private things can only be accessed inside the object

    • Static are the same for all object instances of a class

    • Readonly cant be modified after creation, all readonly are static

    • Btw, “variable” == “member” == “attribute” and “function” == “method”

    • Python and C++ also have classes

    • Example class:

Car.java
 1class Car {  
 2	public Engine engine = new Engine();  
 3public float fuel = 0; // Unit is gallons  
 4public float mileage = 0; // Unit is miles  
 5	  
 6// The constructor  
 7public Car(float fuel, float mileage) {  
 8	this.fuel = fuel;  
 9	this.mileage = mileage;  
10}
11
12// This method drives the car by X miles  
13public void driveCar(float miles) {  
14	this.mileage += miles;  
15	this.fuel -= miles / 33.0; // 33 miles per gallon  
16}
17
18// Adds fuel to car  
19	public void addFuel(float fuel) {  
20		this.fuel += fuel;  
21	}  
22}	  
Main.java
1// ...  
2Car hondaCivic = new Car(0, 0); // Brand new car with zero mileage  
3hondaCivic.addFuel(10.0f); // Filled it up  
4hondaCivic.driveCar(30.0f); // Drove the car to IMSA  
5// ...  
  • Libraries

    • Other code you import, typically written by other people

    • Consists of classes

    • Standard library is included with java

    • WPILib is the library we use for FRC

    • It has stuff like classes for motors and sensors

    • We also have a library for our gyroscope written by the manufacturer

    • And also our own library for reusable code: Titan Algorithms

  • Walkthrough practice: inventory manager

Item.java
1class Item {  
2	public string Name;  
3	public int Quantity;
4
5	public Item(string name, int quantity) {  
6		this.Name = name;  
7		this.Quantity = quantity;  
8	}  
9}  
Inventory.java
 1class Inventory {  
 2private ArrayList<Item> items = new ArrayList<Item>();
 3
 4public Inventory() {}
 5
 6public void addItem(Item item) {  
 7items.add(item);  
 8}
 9
10public String search(String query) {  
11	String result = "";  
12	for (int i = 0; i < items.size(); i++) {  
13		If (items.get(i).Name.contains(query)) {  
14			// Yes you can add strings  
15			// "\n" or "\r\n" on Windows is special character for new line  
16			result += items.get(i).Name + "\r\n";  
17		}  
18}   
19}	  
20}  
Main.java
 1import java.util.Scanner; // Import standard library for print, this is done automatically by VSCode
 2
 3// Name of our program  
 4class InventoryManager {  
 5	public static void main(String[] args) {  
 6 		// Java’s print function  
 7    		System.out.println("Welcome to inventory manager.");  
 8    		System.out.println("Type ‘/add [item name] [quantity]’ to add an item. No spaces in name.");  
 9		System.out.println("Type ‘/search [item name]’ to find an item.");  
10		System.out.println("Type ‘/exit’ to leave the program.");
11
12		// Scanner object used to record user input  
13Scanner inputScanner = new Scanner(System.in);
14
15// Our inventory  
16Inventory inventory = new Inventory();
17
18		// While loops run if the condition is true  
19		boolean isRunning = true;  
20		while (isRunning) {  
21String userInput = inputScanner.nextLine(); // Get command input  
22String[] splittedCommand = userInput.split("\\s+"); // Array with each word
23
24			If (userInput.startsWith("/exit")) {  
25				isRunning = false;  
26System.out.println("Exiting.");  
27			} else if (userInput.startsWith("/add")) {  
28				// Item count is a string but has to be converted to an integer type  
29				Item newItem = new Item(splittedCommand[1], Integer.parseInt(  
30splittedCommand[2]));  
31				inventory.addItem(newItem);  
32System.out.println("Item added.");  
33			} else if (userInput.startsWith("/search")) {  
34				String query = splittedCommand[1];  
35				System.out.println(inventory.search(query));  
36			} else {  
37				System.out.println("Unknown command.");  
38			}  
39		}  
40	}  
41}