0% found this document useful (0 votes)
5 views

COMPROG-1-reviewer-for-finals

The document outlines various programming concepts including flowchart symbols, access modifiers, return types, data types, arithmetic operators, and control structures in Java. It provides examples of using the Scanner class for user input, implementing conditional statements and loops, and working with arrays and strings. Additionally, it includes use cases demonstrating basic user authentication, fruit selection, and grocery list management.

Uploaded by

simonlara410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

COMPROG-1-reviewer-for-finals

The document outlines various programming concepts including flowchart symbols, access modifiers, return types, data types, arithmetic operators, and control structures in Java. It provides examples of using the Scanner class for user input, implementing conditional statements and loops, and working with arrays and strings. Additionally, it includes use cases demonstrating basic user authentication, fruit selection, and grocery list management.

Uploaded by

simonlara410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

A.

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

B. PART OF ACCESS MODIFIER


- controls the visibility or accessibility of classes, methods, and variables
1. Access Modifier (public, private, protected, default)
public class Main {
}
2. Classname (pangalan ng system)
public class VidVivbe {
}
3. Keyword (static, return, new, this, abstract, implements)
public static void main(String[] args) {
}

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the 1st whole number: ");


int num1 = scanner.nextInt();

System.out.print("Enter the 2nd whole number: ");


int num2 = scanner.nextInt();

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");


String name = scanner.nextLine();

System.out.print("Enter your age: ");


int age = scanner.nextInt();

scanner.nextLine();
System.out.print("Enter your favorite color: ");
String favoriteColor = scanner.nextLine();

System.out.print("May pera ka pa? (true/false): ");


boolean haveMoney = scanner.nextBoolean();

System.out.print("Magkano na lang ang pera mo?: ");


double howMuch = scanner.nextDouble();

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();
}
}

Use Case: Choose Available Fruits


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry", "Grapes", "Strawberry"};

System.out.println("Available fruits:");
for (int i = 0; i < fruits.length; i++) {
System.out.println(i + ": " + fruits[i]);
}

Scanner scanner = new Scanner(System.in);

System.out.print("Please enter the number of the fruit you want to choose: ");
int choice = scanner.nextInt();

if (choice >= 0 && choice < fruits.length) {


System.out.println("You have chosen: " + fruits[choice]);
} else {
System.out.println("Invalid choice. Please select a number between 0 and " + (fruits.length - 1) + ".");
}

scanner.close();
}
}
Use Case: Basic User Authentication
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String correctUsername = "admin";


String correctPassword = "p@ssw0rd";

System.out.print("Username: ");
String username = scanner.nextLine();

System.out.print("Password: ");
String password = scanner.nextLine();

if (username.equals(correctUsername) && password.equals(correctPassword)) {


System.out.println("");
System.out.println("Login successful!");
System.out.println("Welcome, " + username + ".");
} else {
System.out.println("");
System.out.println("Invalid username or password. Please try again.");
}

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)

for - used when you know the number of iterations in advance

while - executes a block of code as long as the condition is true.

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.

Example: (10/3= 3 r.1)


int a = 10; Use Case: To check if a number is even or odd:
int b = 3; int number = 5;
int remainder = a % b; if (number % 2 == 0) {
System.out.println("Even");
System.out.println(remainder); } else {
// remainder will be 1 System.out.println(“Odd”);
}

To check the Even numbers from 0-100:


public class Main { To check the Odd numbers from 0-100:
public static void main(String[] args) { public class Main {
System.out.println("Even numbers from 0 to public static void main(String[] args) {
100:"); System.out.println("Odd numbers from 0 to
100:");
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) { // Check if the number is even for (int i = 0; i <= 100; i++) {
System.out.println(i); if (i % 2 != 0) { // Check if the number is odd
} System.out.println(i);
} }
} }
} }
}
STRINGS
- Strings in Java are objects that represent a sequence of characters. They are immutable, meaning
once created, their values cannot be changed.

Common String Methods:


1) .length()
- returns the length of a string.
2) .toUpperCase()
- converts to uppercase letters.
3) .toLowerCase()
- converts to lowercase letters.
4) .indexOf(string variable)
- returns the position of the first occurrence of specified character(s).
5) .charAt(int index)
- returns the character at the specified index in a string.
6) .replace(‘character to find’, ‘character to replace with)
- returns the character at the specified index in a string.
7) .substring()
- Returns a substring from the string.
8) .trim()
- removes whitespace from both ends of a string.
9) .equals()
- method compares two strings, and returns true if the strings are equal, and false if not.
ARRAYS
- Arrays are used to store multiple values of the same type in a single variable. They have a fixed size,
and their elements can be accessed using an index.

a. Declaring and Initializing Arrays


int[] numbers = new int[5]; // Declares an array of integers with size 5
numbers[0] = 10; // Assigns 10 to the first element
b. Example of Array Initialization
int[] numbers = {1, 2, 3, 4, 5}; // Initializes an array with values
c. Accessing Array Elements
int firstNumber = numbers[0]; // firstNumber will be 1
d. Iterating Through an Array
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

Use Case: Add the stored numbers


public class Main {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

int sum = 0;

for (int i = 0; i < numbers.length; i++) {


sum += numbers[i]; // Add each element to sum
}

System.out.println("The sum of the array elements is: " + sum);


}
}

Use Case: Print all the Fruit values


public class Main {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry", "Grapes", "Strawberry"};

for (int i = 0; i < fruits.length; i++) {


System.out.println(fruits[i]);
}
}
}
Use Case: Print Specific Fruit values
public class Main {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry", "Grapes", "Strawberry"};

System.out.println(fruits[0]); // Apple
System.out.println(fruits[1]); // Banana
System.out.println(fruits[2]); // Cherry
}
}

Use Case: Characteristics of Simon Lara


public class Main {
public static void main(String[] args) {
String[] charac = {"Mabait", "Mataray", "Pogi", "Hehe, Malikot"};

System.out.println(charac[0]);
}
}
COMBINATION
OF SCANNER, CONTROL STRUCTURE, MODULUS, STRINGS, ARRAYS

Example:

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Prompt user for the number of grocery items


System.out.print("Enter the number of grocery items: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

// Create an array to store the grocery items


String[] groceryItems = new String[n];

// Read grocery items from user


for (int i = 0; i < n; i++) {
System.out.print("item " + (i + 1) + ": ");
groceryItems[i] = scanner.nextLine(); // Read the item as a string
}

// Check if the number of items is even or odd


if (n % 2 == 0) {
System.out.println("You have an even number of grocery items.");
} else {
System.out.println("You have an odd number of grocery items.");
}

// Display the grocery list


System.out.println("\nYour Grocery List:");
for (int i = 0; i < groceryItems.length; i++) {
System.out.println((i + 1) + ". " + groceryItems[i]);
}

// Close the scanner


scanner.close();
}
}
Explanation of the Program
1. Import Statement:
● The program starts with import java.util.Scanner;, which allows the use of the Scanner
class for reading user input.
2. Main Method:
● The execution begins in the main method.
3. Creating a Scanner Object:
● Scanner scanner = new Scanner(System.in); creates a Scanner object to read input
from the console.
4. Prompting for Number of Items:
● The program prompts the user to enter the number of grocery items they want to add to
the list.
5. Reading the Number of Items:
● int n = scanner.nextInt(); reads the integer input for the number of items. The
scanner.nextLine(); is called afterward to consume the newline character left in the
buffer.
6. Creating an Array:
● String[] groceryItems = new String[n]; creates a string array to hold the grocery items.
7. Reading Grocery Items:
● A for loop iterates n times, prompting the user to enter each grocery item. Each item is
stored in the groceryItems array.
8. Checking Even or Odd:
● The program uses an if statement to check if the number of items (n) is even or odd
using the modulus operator (n % 2). It prints a corresponding message.
9. Displaying the Grocery List:
● Another for loop iterates through the groceryItems array and prints each item along with
its index.
10. Closing the Scanner:
● Finally, scanner.close(); is called to close the Scanner object.

You might also like