COMPROG-1-reviewer-for-finals
COMPROG-1-reviewer-for-finals
FLOWCHART
✅
Functions and its shape:
✅
1. Start points, end points. - OBLONG
✅
2. Shows relationship - ARROW
✅
3. input or output - PARALLELOGRAM
✅
4. process, action, or function - RECTANGLE
✅
5. question to be answered (yes/no or true/false) - DIAMOND
✅
6. depict a step (preparation) - HEXAGON
7. connects separate elements across one page - CIRCLE
C. RETURN TYPES
- The return type specifies what a method will return when it is called.
• void - doesn’t return anything
• int - returns an integer (whole number, 2005)
• float - returns a (6-7 decimal digits) (decimal, add: letter f after decimal, 10.11f )
• double - return a (15-17 decimal digits) (there is no need to add f, “5.75”)
• char - returns a single character (single character, add: ‘ ‘ between, ‘b’)
• boolean - returns a boolean value true / false
• String - returns a series of characters (word / sentence, add: “ “ between, “hello world”)
• array[] - returns an array ( int[ ], float[ ], char [ ], boolean [ ] )
• List<String> - returns a collection List<String> groceryList = new ArrayList<>();
groceryList.add(“Apples”);
groceryList.add(“Bananas”);
D. DATA TYPES
- data types determine the type of variable declared
• int - integer
• float/double - decimals
• char - single character
• boolean - true or false
• String - words, text
• array[] - collection of data arranged sequentially; linear data structure
ARITHMETIC OPERATORS
- An operator is used to process an arithmetic, relational and logical action in the program
- ARITHMETIC OPERATORS - allows mathematical actions in the program
import java.util.Scanner;
System.out.println("");
System.out.println("Choose an operation:");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.println("5. Modulus (%)");
System.out.println("");
System.out.print("Enter the number corresponding to the operation: ");
int choice = scanner.nextInt();
int result;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("Addition (+): " + num1 + " + " + num2 + " = " + result);
break;
case 2:
result = num1 - num2;
System.out.println("Subtraction(-): " + num1 + " - " + num2 + " = " + result);
break;
case 3:
result = num1 * num2;
System.out.println("Multiplication(*): " + num1 + " * " + num2 + " = " + result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
System.out.println("Division(/): " + num1 + " / " + num2 + " = " + result);
} else {
System.out.println("Error: Division by zero (0) is not allowed.");
}
break;
case 5:
result = num1 % num2;
System.out.println("Modulus(%): " + num1 + " % " + num2 + " = " + result);
break;
default:
System.out.println("Invalid operation selected.");
break;
}
scanner.close();
}
}
SCANNER
- The Scanner class is used to obtain input from various sources, including user input from the console. It
is part of the java.util package.
Example:
import java.util.Scanner;
scanner.nextLine();
System.out.print("Enter your favorite color: ");
String favoriteColor = scanner.nextLine();
System.out.println("");
System.out.println("Hello, " + name + "! You are " + age + " years old.");
System.out.println("Your favorite color is " + favoriteColor);
System.out.println("is " + name + " have Money pa? " + (haveMoney ? "Madame HAHAHAHA" : "ubos na HUHUHU"));
System.out.println("You have " + howMuch + " on your wallet");
scanner.close();
}
}
System.out.println("Available fruits:");
for (int i = 0; i < fruits.length; i++) {
System.out.println(i + ": " + fruits[i]);
}
System.out.print("Please enter the number of the fruit you want to choose: ");
int choice = scanner.nextInt();
scanner.close();
}
}
Use Case: Basic User Authentication
import java.util.Scanner;
System.out.print("Username: ");
String username = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
scanner.close();
}
}
CONTROL STRUCTURES
- Control structures determine the flow of execution in a program.
- The main types include:
1. Conditional Statements
2. Loops
1) Conditional Statements
a) if statement
- Executes a block of code if a condition is true.
Example:
public class Main {
public static void main(String[] args) {
int number = 8;
if (number % 2 == 0) {
System.out.println("The number " + number + " is even.");
}
}
}
b) if-else statement
- Executes one block of code if the condition is true and another block if it is false.
Example:
int number = 10;
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Non-positive number");
}
c) switch statement
- Selects one of many code blocks to execute based on the value of a variable.
Example:
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
2) LOOPS
- Are used for executing a block of code repeatedly
- Types: (for, while, do-while)
do-while - Similar to the while loop, but guarantees that the block of code will execute at least
once before checking the condition.
B
MODULUS
- The modulus operator (%) is used to find the remainder of a division operation. It is commonly used in
various scenarios, such as determining if a number is even or odd.
int sum = 0;
System.out.println(fruits[0]); // Apple
System.out.println(fruits[1]); // Banana
System.out.println(fruits[2]); // Cherry
}
}
System.out.println(charac[0]);
}
}
COMBINATION
OF SCANNER, CONTROL STRUCTURE, MODULUS, STRINGS, ARRAYS
Example:
import java.util.Scanner;