Java is a programming language that our team uses to program the robot. Java is also an object-oriented programming language, which means its work is done by having objects that different actions, or methods, are called on. There is a lot of learn about when it comes to learning Java. However, this page will teach you the basics to help create a good foundation for learning Java.
First: Java uses keywords are written in English, this means you will see parts of code that are in English. However it will be very clear that's Java code and what isn't. Java Code will be in a different font and highlighted in a light grey. You can see an example below:
int robotnumber = 6201;
System.out.print("Robots!");
else if (robot == "good){
Second: We will be using code exercises to help you learn how to program. In these code exercise, you will be writing code in a text editor. A text editor allows you to write and run code. You can see an example of what the text editor looks like below. When you have to do a code exercises, a template of the code will be given to you and it will include a comment to show you were your code should go. All you need to do is read the instructions, write the code, then click on the blue "Run" button near the bottom right corner. If you get stuck, answers will be provided on in the "Code Exercises Answers", but you should try to figure out the code first.
On the bottom of the text editor is the output console. This is where your code shows any text that gets printed to the screen. To see what this would look like, go the example below and click on the blue "Run" button!
You should see the words "Hello!" in the box under "Output". If you were able to see the words, congratulations! You ran your first program! Next we will learn how to write code. First we will start off with learning what variables are.
Printing Information to the Output Screen
In Java, we print out information to the output screen. We can do this by using a print statement in Java that looks like the following:
System.out.print() allows us to display any information we want on to the screen. You put what you want to show up on the screen in between the parentheses. If you want to print out "robots are cool", like we saw in the example above, you put "robots are cool" in between the parentheses so it looks like
System.out.print("robots are cool");
The quotes around the text are needed because it lets Java know that it's a string. You will learn what this means later.
If you want to print a variable, we will learn what this means later too, you can just print the variable name with no quotes around it. You can see this in the example below where it takes the number 9 and prints the variable name without the quotes.
You will also see System.out.println(), this is similar to System.out.print() but it has println. This means that it prints on a new line. You can see that in the example below
Variables are containers that are used to store information in a computer program. When programming, we give variables a name so we can use them in our code. In Java, variables look like:
int robot = 2;
You might have seen variables before in math class! We use variables in our math problems. Chances are, you probably have seen math problems such as 3 + x = 5 or 12x - 3 = 4. The x in those two problems are variables because they hold information which are numbers. As you saw above, we do something similar when programming.
To learn what the different parts of a variable are and what the different parts mean, we will start off with learning about data types
Data types refer to the type of value that our variable has. It helps tells our program more information about our variable and what the variable can and can not do. In Java, some data types include numbers and some include words. These different type of data types are:
int
double
Boolean
String
Char
There are other types of data types in Java but the ones mentioned above are the ones we use the most when programming our robots.
int are integers or numbers that are positive or negative. Int only allows numbers without decimals. One example of an int would be 12.
double are numbers that are positive or negative. Unlike int, double allows numbers with decimals. One example of a double would be 34.1.
boolean are either True or False.
String is a variable that holds words.
char is a variable that holds one letter.
Now that we know about different data types, lets learn about how to create variables.
We can use the information we learned above to create variables. Variables have a certain way to be set up or they will not work when you run your code. To create a variable you have to specify the data type, give the variable a name, and set it equal to a value.
An example of a variable is:
int robot = 2;
To break down what this means:
int means that the information that the variable will be storing is a number.
robot is the name of the variable. Variable names can not have spaces. They usually have uppercase letter signaling new words – as in myVariable, and highScore. The name of the variable can be anything you want it to be.
= means you are setting the variable "robot" equal to whatever is after the equal sign. In this case it is a number.
2 is the value
; is telling the program that the line is over. This is needed for all lines of code except for “if” loops, “while” loops, and methods. We will learn more about those in the later sections.
With Strings, they are defined differently. The set up for a String looks like the following:
String RobotName = “Bonk”;
The double quotations are needed so Java knows that it’s a string.
Note: You only need to place the data type in front of the variable when you create the variable for the first time. Once the variable is created you can just use the variable name. You can run the code below to see that it adds the numberOfCats, which is 4, and the numberOfDogs, which is 3, correctly and then prints the variable "numberOfAnimals"
In this exercise
Create a variable that is an int, name it RobotArm and set it equal to 6.
Create a variable that is an double, name it RobotMotor and set it equal to 6.2
Create a variable that is a boolean, name it RobotStatus and set it equal to True
Create a variable that is a String, name it MyName and set it equal to your name
Operators: Basic Math
In Java, we use math operators to perform operations on variables and values. This means we can use math to adjust or change the values of the variables in our code.
The math operators we can use in java are:
Addition +
Subtraction -
Multiplication *
Division /
Lets start off with addition. We will use the variable called "FavoriteNumber" and this variable will be a int. This means that "FavoriteNumber" can be any number that isn't a decimal. So it can be a number like 6 or 2 but it can not be a number like 6.2.
Below we have some code. What do you think the code below will do? Think about it for a second then click on the run button.
If you guessed that it will print 8, then you are right! It prints 8 because the variable FavoriteNumber is being set to what 6 + 2 is and if we do the math we can know that 6 + 2 ends up being 8.
We can do the same thing with subtraction, Multiplication, and Division. Below we have more code! What do you think the code below will do? Think about it for a second then click on the run button.
answer1 will be 2 because 4 -2 is equal to 2. answer2 will be 6 because 3 * 2 is equal to 6. And lastly answer3 will be 5 because 10/2 is equal to 5!
In this exercise, we will
Create a variable that is an int and name it "mystery1"
set "mystery1" equal to 4 + 10
Create a variable that is an int and name it "mystery2"
Set "mystery2" equal to 2 * 8
Operators: Addition and Subtraction
Now that we learned about the basics of operators, lets look at how we would use these in code!
Lets use a bake sale as an example. Pretend you have a bake sale. We can use subtraction to show how many cookies we have left. What you do think it will print out? Once you thought about it, click the run button below
When you run this code, it will print the number 5. This is because numberOfCookies is equal to 10, and amountSold is equal to 5. If you replaced the variable name with the numbers, it looks like the following:
numberOfCookies = 10 - 5
We know that 10 - 5 is 5 so NumberOfCookies is now set to a new amount which is 5.
Lets use our bake sale example to learn about addition! Look at the code below and think about it. What you do think it will print out? Once you thought about it, click the run button below
When you run this code, it will print out the number 8. The reasoning is similar to the example above. When we replace the variable names with the numbers, it looks like the following:
numberOfCookies = 5 + 7;
We know that 5 + 7 is 8 so NumberOfCookies is now set to a new amount which is 8
In this exercise:
Create a variable that is an int that is called animalsInZoo, this will hold the amount of zebras plus the amount of giraffes
Then print your animalsInZoo variable in a print statement
Run your code! If it works, move on to the next step
Two of the giraffes get moved to another zoo where they can make new friends. Therefore you need to subtract 2 from the number of giraffes and store the result in a int variable called currentAmountOfGiraffes
Then print your currentAmountOfGiraffes variable in a print statement
Operators: Multiplication and Division
Using multiplication and division is similar to using addition and subtraction in code. Lets go over an example of how multiplication and division are used.
We can learn how to use multiplication and division by thinking like we are writing a math problem. Lets say you have an int that is equal to 5 and another int that is equal to 3. You want to multiple these two variables together, but how you would do it?
You can multiple these two variables together by creating a new int called c and setting it equal to a * b. The * sign is what we use for multiplication. It would look like the following:
int c = a * b;
What do you think c will be? Think about it, then run the code below
The code will print out 15! This is because a is equal to 5 and b is equal to 3 and when you multiply them, you get 15. If you replace the variable name with the numbers, it looks like the following:
int c = 5 * 3;
We know that 5 * 3 is 15 so c is now set to a new amount which is 15
Now it's learn about division. Lets say you have the variable d that is equal to 6 and a variable e that is equal to 2. You can divide these two variables together by creating a new int called f and setting it equal to d / e. The / sign is what we use for division. It would look like the following:
int f = d / e;
What do you think f will be? Think about it, then run the code below
The code will print out 3! This is because d is equal to 6 and e is equal to 2 and when you divide them, you get 3. If you replace the variable name with the numbers, it looks like the following:
int f = 6 / 2;
We know that 6 / 2 is 3 so f is now set to a new amount which is 3
In this exercise:
create an int variable called numberOfCookies and set it equal to 20
create an int variable called numberOfPeople and set it equal to 5
You want to figure out how many cookies that each person gets. To do that, create a int variable called cookiesPrePeople that is set equal to the numberOfCookies divided by numberOfPeople . Run your code!
🎉Congratulation! 🎉
You learned some of the basics of Java. Now we will move on to the last part which is Conditionals and While Loops. You can click the button below to begin!