1 of 72

Getting Set Up + Java Syntax

2 of 72

Github

  • Github is a platform for storing your code online and collaborating with a team on projects
  • Make an account!
  • https://github.com/
  • Slack your username to us so we can add you to Team4159/2020

3 of 72

Java Basics

EVERY STATEMENT ENDS WITH A SEMICOLON!

  • Variables
  • Mathematical Operations
  • System.out.print
  • Comments

4 of 72

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)

doubledecimal 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

5 of 72

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’;

6 of 72

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]

7 of 72

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

8 of 72

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

9 of 72

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

10 of 72

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)

11 of 72

More Info

  • Java is statically typed - that means once you declare a variable, you can’t change its type
    • String a = “HI”
    • int b = 1
    • You cannot do: a = b

12 of 72

Data Structures: Arrays

  • Arrays are used to store multiple variables of the same type
  • There are two ways to initialize an array.

1)

int list[] = new int[4];

list[0] = 4;

list[1] = 1;

list[2] = 5;

list[3] = 9;

13 of 72

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

  • You can’t change the size of an array (add or remove elements)
  • You can edit the array
  • ex:

list[0] = 1;

System.out.println(list[]);

> 1, 1, 5, 9

14 of 72

Arraylists

  • What if you want to be able to add/remove elements?
    • That’s where arraylists come in!
  • Arraylists hold multiple variables like arrays, but they are much more flexible. You can add, remove, and insert elements.
  • Arraylists are NOT initialized/declared the same way as arrays.

15 of 72

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

16 of 72

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?

17 of 72

Kahoot!

18 of 72

Homework

  1. Make sure you’ve made a GitHub
  2. Write a program that declares and initializes all the different types of variables you learned today.
  3. Have your program perform all the different mathematical operations you learned
  4. Trinket
  5. Have your program print something out

If you need any help definitely ask/Slack us (Mia Chen, Timmy Dang)! Setting things up can be very confusing and we understand :)

19 of 72

Resources

  • US!!!!!
    • If you need anything, feel free to slack us!
  • CodeAcademy.com

20 of 72

Java Logic

21 of 72

If/else statements

  • If/else statements are used to check if a condition is true before running a specific block of code

<

22 of 72

23 of 72

Loops

Let’s say you wanted to add up all the numbers from 1-10.

You could do this:

24 of 72

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.

  • This lets you write less code, making your program cleaner and less repetitive
  • There are 2 main types of loops:
    • For
    • While

25 of 72

The For Loop

1

26 of 72

The While Loop

1

27 of 72

Functions (aka Methods)

  • Functions are a way of breaking your code up into parts that perform a specific function
  • Functions:
    • Have a name
    • Can take in parameters/arguments
    • Have a return type

28 of 72

Functions

function name

return type

What your function does

29 of 72

Calling a Function

Hi, Mia

30 of 72

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)

31 of 72

Void Functions

function name

return type

What your function does

Void:

  • Void methods don’t return anything
  • Instead, they perform a specific action or task
    • ex: printing something or setting a variable equal to something

32 of 72

Non-void functions

function name

return type

What your function returns

Int/char/float/double/String/boolean, etc:

  • This tells us what type of value the function will return
  • If a function’s return type is int, it’ll return an int - simple!

33 of 72

10

34 of 72

Arguments (aka Parameters)

  • Arguments are variables that functions can take in and then do something with the value of the variable
  • Arguments are put in the parentheses following the function name
  • Giving a function argument(s) is calling ‘passing’ a variable
  • Functions do not have to take in arguments

public void noArgumentFunction()

public void argumentFunction(int a, String b)

35 of 72

arguments

36 of 72

Kahoot!

37 of 72

Object-Oriented Programming

38 of 72

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{

}

39 of 72

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{

}

40 of 72

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:

41 of 72

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;

}

}

}

42 of 72

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;

}

}

43 of 72

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

44 of 72

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);

}

45 of 72

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;

}

46 of 72

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();

47 of 72

EXTENDING

If you want another class to inherit the methods and variables of a class, you use the keyword extends.

48 of 72

Super, Sub, Base, Derived, Parent, Child classes

Fruit

Apple

Super, Base, Parent

all synonyms

Sub, Derived, Child all synonyms

49 of 72

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

50 of 72

OVERRIDING

Overriding means replacing an inherited function

51 of 72

POLYMORPHISM

Polymorphism means same name different meanings

52 of 72

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.

53 of 72

Protected

  • Like private, but access is also given to any classes that inherit from this class

  • Can be a better choice than private if you are going to create other classes that extend the class

54 of 72

Special Types of Classes: Interfaces & Abstract

  • Interfaces/Abstracts are like templates for actual classes that share similar functions
  • They specify what a class does and how
  • You cannot instantiate interfaces or abstracts
  • Interfaces have all blank methods (nothing inside) while abstract methods can have blank or complete methods

55 of 72

56 of 72

Example of an Interface

57 of 72

Example of an Abstract Class

58 of 72

Kahoot!

59 of 72

Getting Set Up for FRC

60 of 72

Installing Java

61 of 72

Installing Java - Windows

62 of 72

IntelliJ

  • IntelliJ is a Java IDE (Integrated Development Environment)
  • Basically, it’s a program that lets you write and run your code
  • Install the community edition (link)
  • Instructions from IntelliJ
  • Also install the latest version of Java & the Java SDK
    • SDK (Software Development Kit): Collection of tools that you need to develop an application **correct this later, JDK includes JRE and maybe just windows needs an account?

63 of 72

FRC Plugins

64 of 72

The Terminal (Mac)

  • Search up ‘terminal’

65 of 72

Git Bash - Windows

  • https://gitforwindows.org/
  • Install
  • We’ll be using Git Bash - a terminal emulator so you can use Git from the command line

66 of 72

Command Line

  • The terminal is a command-line interface
    • This is where you can “talk” to your computer and give them instructions to do something (commands)

  • We will be using terminal to navigate throughout folders in our computer and interact with Github
    • Cloning folders
    • Pushing, pulling, branching

67 of 72

Navigating your computer’s files

  • Follow along on the projector screen!
  • Cheat sheet:
    • cd directoryname: change directory to directoryname
    • ls: list everything in the directory
    • pwd: present working directory
    • cd .. : goes back to the previous directory
    • touch filename: creates a file named filename in the present working directory

68 of 72

Git commands & workflow

  • Follow along on the projector screen!
  • We will be creating a repository in Github, cloning it into our computer, creating a branch, adding some files/edits, committing, pushing to our repository, opening a pull request, and merging
  • Don’t worry if this seems very complicated! It’s a little scary and hard to remember at first but you’ll get the hang of it with practice!

69 of 72

Git Cheatsheet

  • git branch
  • git checkout <branchname>
  • git checkout -b <branchname>
  • git pull
  • git add <filename>
  • git commit -m “message”
  • git push <branch>
  • **windows terminal

70 of 72

Additional Resources

71 of 72

Kahoot!

72 of 72

Homework