- Introduction
- Super Simple Formula
- Your First Program
- Variables, Methods and Parameters
- Variables in Programming and Algebra
-
Math and Comparison Operators - If Statement
- While and For Loops
- Loopy Thinking
-
Data Types - Booleans
- Logical Operators (and Booleans)
- Output and Printing
- Printing and Loops
-
Arrays - Arrays and Loops
- Array Loop Practice
-
What's Next? - Quick Reference
Quick Reference
Quickly lookup Java basics, and load code examples directly into editor.
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if(condition){
//doSomething
}
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i < 10){
System.out.println(i);
i = i + 1;
}
}
System.out.println("");
The above prints text and add a newline afterwards.
System.out.print
prints without a newline, but you can add in spaces " " to separate text.
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets []
. This code creates an empty 5-cell array:
int[] ar1 = new int[5];
This shortcut creates an array with numbers 0 to 4:
int[] nums = {0,1,2,3,4};
You can use for-loop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Your First Program
Now we will do the same simple problem by coding it. You are again given two variables a
and b
. You will need to add them together again, but you will also have to return their sum.
To return something, just type return
. For example, to return the number 102, you can just type return 102;
. Here are some more examples:
return a;
return 5 - 3;
return b + 11;
Instructions
- Complete the code below so it returns the sum of
a
andb
. - When you think its correct, click on "Submit" at the bottom to run the code.
The Code
In each programming challenge, you will be given a task. You will be provided with code and need to focus on the part that looks like this:
public static int doStuff(int a, int b){
//your code here
}
(int a, int b)
means you are given two integers a
and b
as input. You should replace the entire line //your code here
with your own code (//
marks a comment in code.) Make sure you return something with the keyword return
.
Note: In Java, ordinary lines of code need to end with a semi-colon ;
so remember to put one at the end of your line of code.
Input/Output Details
In these programming challenge, your code will automatically run on different "test cases" or sets of numbers. You need to return the correct output for each given input of a
and b
.
You can view some of the input and correct output in the table below, and can compare your results with the correct ones.
Challenge
You are given two variables a
and b
as input. Can you return the sum of a
and b
?
(For example, if a
is 2 and b
is 3, your program should return 5.)
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Jarek James
Mar 14, 11:53 AMThis bit is rather scary after you finish reading the whole thing. It is however very simple.
Amos
Mar 20, 6:12 PMIt looks very hard and difficult but just you wait and see when you apply it
Anesi
Jun 13, 6:57 PMCool
Adhurim Esati
Jul 1, 5:53 AMcorrect me if i'm wrong
"return" has no mathematical function. It is to like "return 5+5" to return the sum of 5+5 to the screen so when the program runs it shows the sum of 5+5 ?
I know I'm not clear but if someone understands pls reply :P
Learneroo
Jul 1, 7:03 PMreturn is used in a method to return a value to the code that called it (see next page). This is similar to how a mathematical function returns a value.
Alger
Jul 14, 8:54 AMIt will be easy if you read the article carefully
Mark Herbert
Sep 3, 12:43 PMits not as complicated as it looks
Patrick
Dec 19, 2:25 PMI read quick help