>
double kilometers = (100 * 1.609344);
int highScore = 50;
if (highScore > 25) {
highScore = 1000 + highScore; // add bonus points
}
int myVariable = 50;
myVariable++;
myVariable--;
System.out.println("This is a test");
int anotherVariable = 50;
myVariable--;
System.out.println("myVariable = " + myVariable);
if (myVariable == 0) {
System.out.println("It's now zero");
}
The if statement checks a condition and executes code if the condition is true.
The code within the code block associated with the if statement is executed only if the condition evaluates to true.
It's important to include a code block even for single statements to enhance code readability.
boolean gameOver = true;
int score = 5000;
if (score == 5000) {
System.out.println("Your score was 5000");
}
In this example, the if statement checks if the score is equal to 5000.
If the condition is true, the associated code block is executed.
An if statement can be extended with 'else-if' and 'else' blocks.
'Else-if' blocks provide additional conditions to check if the initial condition is false.
The 'else' block is executed if none of the previous conditions are true.
Conditions are evaluated in order, and the first true condition's code block is executed.
if (score < 5000) {
System.out.println("Your score was less than 5000");
} else {
System.out.println("Got here");
}
In this example, the else block is executed when the if condition is false.
if (score < 5000 && score > 1000) {
System.out.println("Your score was less than 5000 but greater than 1000");
} else if (score < 1000) {
System.out.println("Your score was less than 1000");
} else {
System.out.println("Got here");
}
This example demonstrates the use of 'else-if' and 'else' blocks.
The code within the first true condition's block is executed.
If no conditions are true, the 'else' block is executed.
Java's description of a method is that it declares executable code that can be invoked, passing a fixed number of values as arguments.
Methods are executed by calling or invoking them, and they have a name, parameters, and a method body.
Methods help reduce code duplication by allowing code to be written once and reused multiple times.
They enable passing data to the method as arguments, allowing the same method code to work with different inputs.
// Code duplication example
// Problem: Changing code in one place may lead to errors in other similar code blocks
// Solution: Methods can help eliminate code duplication
public class MainChallenge {
public static void main(String[] args) {
// ...
int finalScore = score + (levelCompleted * bonus);
// ...
}
}
This code duplication example highlights the challenge of maintaining similar code blocks and the potential for errors.
// Basic method structure
public static void methodName() {
// Method body or code block
// Code to be executed when the method is called
}
Methods are defined with a name, a parameter list (if any), and a code block.
The public static void keywords indicate accessibility and return type.
public static void calculateScore() {
// Method body
// Code to calculate the score
}
Creating a new method named calculateScore to encapsulate score calculation logic.
Method names follow lower camel case naming convention.
public static void main(String[] args) {
// ...
calculateScore(); // Calling the method
// ...
}
Methods are called or invoked using their names followed by parentheses.
The method code block is executed when the method is called.
// Method declaration with parameters
public static void calculateScore(boolean gameOver, int score, int levelCompleted, int bonus) {
// Method body using parameters
// Code that uses the provided arguments
}
Methods can accept arguments (parameters) to work with specific data passed from the calling code.
Arguments are defined in the method declaration, specifying data type and variable name.
public static void main(String[] args) {
// ...
calculateScore(true, 800, levelCompleted, bonus); // Calling method with arguments
// ...
}
When calling a method with arguments, provide values or expressions that match the parameter types.
Arguments must be provided in the exact order and type defined in the method declaration.
Methods can be declared to have return types, similar to how variables have types.
Return types indicate the type of value the method will return when it completes execution.
The return statement is used to send a value back from the method to the calling code.
public static int methodName() {
// Method body
// Code to calculate and return an int value
return result;
}
Methods with return types specify the type of data they will return in the declaration.
The return statement is used to provide the value to be returned.
public static int calculateScore() {
// Method body
int finalScore = score + (levelCompleted * bonus);
return finalScore; // Returning the calculated value
}
Methods can return a value by using the return statement followed by the value.
The value being returned must match the declared return type of the method.
public static void main(String[] args) {
// ...
int highScore = calculateScore();
System.out.println("The highScore is " + highScore);
// ...
}
The returned value from a method can be assigned to a variable and used in the calling code.
Calling a method with a return type in an expression allows for data exchange between the method and the calling code.
public static void main(String[] args) {
// ...
calculateScore(); // Return value ignored
// ...
}
If the returned value from a method is not used or assigned, it is ignored by the calling code.
The compiler and IDE do not raise an error for ignoring the returned value.
public static void main(String[] args) {
// ...
System.out.println("The next highScore is " + calculateScore());
// ...
}
Method calls that return a value can be used directly within expressions.
The returned value is treated as a part of the expression, like any other variable.
public static int calculateScore(String playerName, int score) { ... }
public static int calculateScore(int score) { ... }
public static int calculateScore() { ... }
public static void printInfo(String name, int age) { ... }
public static void printInfo(int age, String name) { ... } // Invalid (same parameter types but different order)
public static void printInfo(String name, int id) { ... } // Invalid (different parameter types)
public static int printInfo(String name, int age) { ... } // Invalid (different return type)
public static int calculateScore(String playerName, int score) { ... }
public static int calculateScore(int score) { ... }
public static int calculateScore() { ... }