Getting Set Up + Java Syntax
Github
Java Basics
EVERY STATEMENT ENDS WITH A SEMICOLON!
Variables
Variables contain a data value.
There are six main data types:
String → text, words, sentences (i.e “Hello, welcome to Team 4159!”)
int → whole numbers/integers (i.e 5)
float → decimal number with up to 6/7 digits after the decimal point (i.e 4.5948549)
double → decimal number with up to 15/16 digits after the decimal point (i.e 4.5948549374)
char → any single letter or character (i.e ‘a’ or ‘@’)
boolean → only two values: true or false
Declaring & Initializing Variables
Declaring →
(data type) [variable name]
Initializing →
[variable name] = {value}
Put them together! For example:
String welcome = “Wassup!”;
int num = 5;
boolean boo = true;
char letter = ‘a’;
Mathematical Operations
Addition → +
Subtraction → -
Multiplication → *
Division → /
This only returns integers. ex: 5/2 will round 2.5 down to 2 and return 2.
Modulus → %
returns the remainder
[ex: 5 % 2 = 1 or 5 % 10 = 5]
More Mathematical Operations!
++ → add current value by 1
-- → subtract current value by 1
+=5 → add current value by 5
-=2 → subtract current value by 2
*=7 → multiply current value by 7
/=3 → divide current value by 3
Mathematical Operations
EXAMPLES:
String sentenceOne = “Hello! ”;
String sentenceTwo = “How are you?”;
String sentence = sentenceOne + sentenceTwo;
System.out.println(sentence); //Will print out “Hello! How are you?”
int numOne = 5;
int numTwo = 3;
int num = numOne + numTwo;
System.out.println(num) //Will print out 8
System.out.print & System.out.println
These two are called methods.
They both print out messages on the console, but println creates a new line.
Look here for an example:
https://repl.it/@timmydang27/Hello-world
Comments
Comments keep your code organized. If you are working on a coding project with a large group, comments should help tell your collaborators what you are doing. There are two main ways you can add comments --
// text
(used for single line comments)
Or
/*
text
*/
(used for multiple lines of comments)
More Info
Data Structures: Arrays
1)
int list[] = new int[4];
list[0] = 4;
list[1] = 1;
list[2] = 5;
list[3] = 9;
int list[] = {4, 1, 5, 9};
2)
variable type
array name + square brackets []
contents of the array
curly braces
0 1 2 3
index of the array (the position a specific element is in)
ex: 5 is in index 2
list[0] = 1;
System.out.println(list[]);
> 1, 1, 5, 9
Arraylists
ArrayList<Integer> list = new ArrayList<Integer>();
Adding to the end of an arraylist
list.add(4);
list.add(1);
list.add(5);
Adding to a specific position in the arraylist
list.add(3, 9);
Printing an arraylist
System.out.print(list())
Printing a specific element from the arraylist
System.out.println(list.get(0))
> 4
Removing from an arraylist
list.remove(0);
>1,5,9
ArrayList Checkin
I create an arraylist called list.
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(1);
list.add(5);
list.add(9);
System.out.println(list()); —> > [4, 1, 5, 9]
Then I run:
list.remove(2);
What will System.out.println(list.get(2)) return?
Kahoot!
Homework
If you need any help definitely ask/Slack us (Mia Chen, Timmy Dang)! Setting things up can be very confusing and we understand :)
Resources
Java Logic
If/else statements
<
Loops
Let’s say you wanted to add up all the numbers from 1-10.
You could do this:
But that would take way too much space and time!
Loops allow you to repeat something over and over again - they basically loop whatever you want them to do.
The For Loop
1
The While Loop
1
Functions (aka Methods)
Functions
function name
return type
What your function does
Calling a Function
Hi, Mia
Return Types
When your function ‘returns’ something, that means that it will return to the code that called it and provide something (ex: a value) to it
*Functions don’t have to return anything (void functions)
Void Functions
function name
return type
What your function does
Void:
Non-void functions
function name
return type
What your function returns
Int/char/float/double/String/boolean, etc:
10
Arguments (aka Parameters)
public void noArgumentFunction()
public void argumentFunction(int a, String b)
arguments
Kahoot!
Object-Oriented Programming
Classes
When we look around us in “real life” we see the things around us as objects.
In Java, we make models of these objects using the word class.
class Something{
}
Objects & Classes
You wouldn't call a bicycle "Two wheels, a handlebar, a seat, some pedals and a frame."
Instead, you would use one name that describes the entire object: bicycle.
So if you were creating a bicycle class, it would be:
class Bicycle{
…
}
Objects & Classes
A class is a collection of member variables and member functions (also called "methods") that model the data and behavior of an object. For example:
Inflating Balloon
Let's say I want to make an animation of a balloon that gets bigger, that is, it "inflates"
I'll start by asking myself what that balloon has and what it does
And then I'll write a class that models the object. A balloon has a size and a x position and a y position. It also inflates, so we will need a function for that too.
class Balloon{
int mySize, myX, myY;
void inflate(){
mySize=mySize+1;
}
}
}
Constructor
All classes have a constructor. If you do not write one, there is a default argument constructor that is not shown. Recall code from the last slide. What is the size and position of the balloon? It must be initialized in the constructor. So:
class Balloon
{
int mySize, myX, myY;
Balloon(int x, int y)
{
mySize = 0;
myX = x;
myY = y;
}
void inflate()
{
mySize=mySize+1;
}
}
Constructor
The constructor is a function that must have the same name as the class.
It doesn't have a return type, not even void.
It initializes the member variables, that is, it sets them equal to their first values
Constructor
Once I've written my balloon class, I can use it to build a new Balloon() (called an instance) in my program.
Balloon bob;
void setup()
{
size(300,300);
bob = new Balloon(150,150);
}
Constructor
Notice that 150 is copied into the x and y parameters.
Then myX and myY are initialized to be 150.
bob = new Balloon(150,150);
//calls the constructor
Balloon(int x, int y)
{
mySize = 0;
myX = x;
myY = y;
}
The dot operator
Once you have made an instance of a class, you can access its parts with a dot.
Balloon bob; //the declaration
bob = new Balloon(150,150); //the initialization
//other java code not shown
System.out.println(bob.myX);
bob.inflate();
bob.show();
EXTENDING
If you want another class to inherit the methods and variables of a class, you use the keyword extends.
Super, Sub, Base, Derived, Parent, Child classes
Fruit
Apple
Super, Base, Parent
all synonyms
Sub, Derived, Child all synonyms
Objects and Classes Review
A class models an complex object
A class is a collection of variables and functions
We call those variables and functions members of that class
The member variables represent what the object has
The member functions (also called methods) represent what the object does
→ A special function that initializes the member variables is called the constructor
https://github.com/tidang/OldMacDonald
OVERRIDING
Overriding means replacing an inherited function
POLYMORPHISM
Polymorphism means same name different meanings
Encapsulation
Encapsulation is used to help the program run. It restricts or allows data to be accessed on how you encapsulate it. You MUST encapsulate all member methods.
Public is used for most member functions.
Private is used for most member variables.
Protected
Special Types of Classes: Interfaces & Abstract
Example of an Interface
Example of an Abstract Class
Kahoot!
Getting Set Up for FRC
Installing Java
Installing Java - Windows
IntelliJ
FRC Plugins
The Terminal (Mac)
Git Bash - Windows
Command Line
Navigating your computer’s files
Git commands & workflow
Git Cheatsheet
Additional Resources
Kahoot!
Homework