Professional Documents
Culture Documents
JAVA NOTES FOR MACKUP EXAM BSIT 5Th SEMESTER
JAVA NOTES FOR MACKUP EXAM BSIT 5Th SEMESTER
While Loop:
Here is an example of a while loop in Java:
In this example, the while loop will iterate 10 times, starting at 1 and ending at 10. On each iteration, the
value of i will be printed to the console.
The while keyword is followed by a continuation condition in parentheses (). This condition is a
Boolean expression that determines whether the loop will continue. In this case, the loop will
continue as long as i is less than or equal to 10.
The loop body is the code that will be executed on each iteration of the loop. In this case, the
body of the loop is a single statement that prints the value of i to the console and increments i by
1.
Do While Loop:
Here is an example of a do-while loop in Java:
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
n this example, the do-while loop will also iterate 10 times, starting at 1 and ending at 10. On each
iteration, the value of i will be printed to the console.
The key difference between a while loop and a do-while loop is that the while loop only executes
if the continuation condition is true before the first iteration, whereas a do-while loop is
guaranteed to execute at least once because the continuation condition is checked at the end of
the loop body, after the first iteration.
For Loop:
Here is an example of for loop in Java:
In this example, the for loop will iterate 10 times, starting at 1 and ending at 10. On each iteration, the
value of i will be printed to the console.
Here's how the for loop works in this example:
The for keyword is followed by parentheses () containing the loop control statement.
The loop control statement has three parts, separated by semicolons ;:
The initialization part, where a loop control variable is initialized. In this case, the variable i is
initialized to 1.
The continuation condition, where a boolean expression is specified that determines whether the
loop will continue. In this case, the loop will continue as long as i is less than or equal to 10.
The update part, where the loop control variable is updated on each iteration. In this case, i is
incremented by 1 on each iteration.
The loop body is the code that will be executed on each iteration of the loop. In this case, the
body of the loop is a single statement that prints the value of i to the console.
System.out.println("While Loop:");
// Use a while loop to iterate 10 times
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
System.out.println("Do-While Loop:");
// Use a do-while loop to iterate 10 times
i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
In this program, each of the three loops will iterate 10 times, starting at 1 and ending at 10. On each
iteration, the value of the loop control variable (i) will be printed to the console.
Switch Statement:
In Java, a switch statement allows us to execute a block of code among several options. It is a multi-way
branch statement that provides an efficient way to transfer control to one of many possible code blocks
based on the value of an expression.
In the example above, the switch statement compares the value of the menuOption variable to the values
of each case label inside the statement. If a match is found, the code associated with that case label is
executed. The break statement is used to exit the switch statement and prevent any further code from
being executed. The default case is used to handle any values that don't match any of the other case labels.
Constructor:
In Java, a constructor is a special method that is used to initialize an object. It has the same name as the
class and is called when an instance of the class is created.
// Constructor
public MyClass(int x, int y) {
this.x = x;
this.y = y;
}
In the example above, the MyClass class has two class variables x and y and a constructor that takes two
arguments x and y. Inside the constructor, the this keyword is used to refer to the current instance of the
class and assign the values of the arguments to the class variables.
To create an instance of the MyClass class and call the constructor, we can use the following code:
This will create an instance of the MyClass class and call the constructor, passing the values 10 and 20 as
arguments. These values will be assigned to the class variables x and y respectively.
We can then call the printValues() method on the object to print the values of x and y:
myObj.printValues(); // Output: x = 10 y = 20
In this way, constructors are used to initialize objects and give them initial values when they are created.
Method Overloading:
In Java, method overloading is a concept where multiple methods with the same name can be defined in a
class, but with different parameters. This allows for methods with the same name to be called with
different sets of parameters, which can be useful for performing similar operations on different data types.
Here is an example of method overloading in Java:
In the example above, the MyClass class has three methods named sum() which perform the same
operation (addition) but on different data types (integers, doubles, and strings). This allows us to call the
sum() method with different arguments and get the correct result based on the data type of the arguments.
To call the sum() method with different sets of arguments, we can use the following code:
In the code above, we first create an instance of the MyClass class and then call the sum() method three
times with different sets of arguments. The Java compiler will automatically select the correct method to
call based on the data type of the arguments passed to the sum() method.
In this way, method overloading allows us to define multiple methods with the same name but different
parameters, which can be useful for performing similar operations on different data types.
Inheritance:
In Java, inheritance is a concept where a class (called the subclass) can inherit the properties and methods
of another class (called the superclass). This allows for code reuse and helps to organize classes into a
hierarchy based on their common characteristics.
Here is an example of inheritance in Java:
// Constructor
public Car(String make, String model, int year, int numDoors) {
super(make, model, year);
this.numDoors = numDoors;
}
This will create an instance of the Car class and call its constructor, passing the values "Toyota",
"Camry", 2010, and 4 as arguments. These values will be used to initialize the make, model, year, and
numDoors variables of the Car object.
If statements:
if (x > 10) {
System.out.println("x is greater than 10");
} else if (x > 5) {
System.out.println("x is greater than 5 but less than or equal to 10");
} else {
System.out.println("x is less than or equal to 5");
}
}
}
In this program, the if statement checks if the value of the x variable is greater than 10. If this condition is
true, then the code inside the if block will be executed and the program will print "x is greater than 10" to
the console. If the condition is false, then the else if block will be executed, which checks if x is greater
than 5 but less than or equal to 10. If this condition is true, then the program will print "x is greater than 5
but less than or equal to 10" to the console. If this condition is false, then the code inside the else block
will be executed, which simply prints "x is less than or equal to 5" to the console.
This is just a simple example to illustrate how if, else if, and else statements work in Java. You can use
these statements in many different ways to create more complex and powerful programs.
Mashahoooooooooor Example