Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

JAVA NOTES FOR MACKUP EXAM BSIT 5Th SEMESTER

While Loop:
Here is an example of a while loop in Java:

public class WhileLoopExample {


public static void main(String[] args) {
int i = 1;

// Loop as long as i is less than or equal to 10


while (i <= 10) {
System.out.println(i);
i++;
}
}
}

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.

Here's how the while loop works in this example:

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

// Loop as long as i is less than or equal to 10


do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

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:

public class ForLoopExample {


public static void main(String[] args) {
// Loop through the numbers 1 to 10
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}

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.

ALL THREE LOOPS COMBINE:

public class LoopExample {


public static void main(String[] args) {
System.out.println("For Loop:");
// Use a for loop to iterate 10 times
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}

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.

// Declare a variable to store the menu option


int menuOption;

// Prompt the user to enter a menu option


System.out.println("Please enter a menu option (1, 2, 3, or 4):");
menuOption = input.nextInt();

// Use a switch statement to execute the corresponding code block


switch (menuOption) {
case 1:
// Code to be executed if menuOption is 1
System.out.println("You selected option 1.");
break;
case 2:
// Code to be executed if menuOption is 2
System.out.println("You selected option 2.");
break;
case 3:
// Code to be executed if menuOption is 3
System.out.println("You selected option 3.");
break;
case 4:
// Code to be executed if menuOption is 4
System.out.println("You selected option 4.");
break;
default:
// Code to be executed if menuOption is none of the above options
System.out.println("Invalid menu option. Please try again.");
break;
}

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.

public class MyClass {


// Class variables
int x;
int y;

// Constructor
public MyClass(int x, int y) {
this.x = x;
this.y = y;
}

// Method to print the values of x and y


public void printValues() {
System.out.println("x = " + x + " 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:

MyClass myObj = new MyClass(10, 20);

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:

public class MyClass {


// Method to calculate the sum of two integers
public int sum(int a, int b) {
return a + b;
}

// Method to calculate the sum of two doubles


public double sum(double a, double b) {
return a + b;
}

// Method to calculate the sum of two strings


public String sum(String a, String b) {
return a + b;
}
}

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:

MyClass myObj = new MyClass();

// Call the sum() method with two integers


int result1 = myObj.sum(10, 20);

// Call the sum() method with two doubles


double result2 = myObj.sum(10.5, 20.5);

// Call the sum() method with two strings


String result3 = myObj.sum("Hello ", "World!");

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:

// Base class (superclass)


public class Vehicle {
// Class variables
private String make;
private String model;
private int year;
// Constructor
public Vehicle(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}

// Getter method for make


public String getMake() {
return this.make;
}

// Getter method for model


public String getModel() {
return this.model;
}

// Getter method for year


public int getYear() {
return this.year;
}
}

// Derived class (subclass)


public class Car extends Vehicle {
// Class variable
private int numDoors;

// Constructor
public Car(String make, String model, int year, int numDoors) {
super(make, model, year);
this.numDoors = numDoors;
}

// Getter method for numDoors


public int getNumDoors() {
return this.numDoors;
}
}
In the example above, the Vehicle class is the superclass and the Car class is the subclass. The Car class
extends the Vehicle class, which means it inherits all the properties and methods of the Vehicle class. The
Car class also has its own class variable numDoors and a constructor that takes additional arguments to
initialize the numDoors variable.
To create an instance of the Car class and call its constructor, we can use the following code:

Car myCar = new Car("Toyota", "Camry", 2010, 4);

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:

public class IfElseExample {


public static void main(String[] args) {
int x = 5;

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

public class Main {

public static void main(String[] args)

Scanner input = new Scanner(System.in);

System.out.print("Enter total marks: ");

int totalMarks = input.nextInt();

System.out.print("Enter marks obtained: ");

int marksObtained = input.nextInt();

float percentage = (marksObtained * 100) / totalMarks;

System.out.println("Percentage: " + percentage + "%");


if (percentage >= 90) {
System.out.println("Grade: A");
} else if (percentage >= 80) {
System.out.println("Grade: B");
} else if (percentage >= 70) {
System.out.println("Grade: C");
} else if (percentage >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}

You might also like