SAMPLE PROGRAM ( REVIEWER)
ARRAY
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
OUTPUT :
Accessing Elements of Array:
First Element: 12
Second Element: 4
Third Element: 5
Fourth Element: 2
Fifth Element: 5
AGE CALCULATOR
//the program works on Java 8 and later versions
import java.time.LocalDate;
import java.time.Period;
import java.util.Scanner;
public class AgeCalculatorExample1
{
public static void main(String args[])
1
{
System.out.print("Enter date of birth in YYYY-MM-DD format: ");
Scanner scanner = new Scanner(System.in);
//reads the date of birth from the user
String input = scanner.nextLine();
scanner.close();
//the parse() method obtains an instance of LocalDate from a text string such as 199
2-08-11
LocalDate dob = LocalDate.parse(input);
//prints the age
System.out.println("You are " + calculateAge(dob)+" years old.");
}
//the method calculates the age
public static int calculateAge(LocalDate dob)
{
//creating an instance of the LocalDate class and invoking the now() method
//now() method obtains the current date from the system clock in the default time z
one
LocalDate curDate = LocalDate.now();
//calculates the amount of time between two dates and returns the years
if ((dob != null) && (curDate != null))
{
return Period.between(dob, curDate).getYears();
}
else
{
return 0;
}
}
}
Output :
2
Enter date of birth in YYYY-MM-DD format: 1999-09-23
You are 22 years old.
MILKTEA ORDER SYSTEM
import java.util.Scanner;
public class MilkTeaOrderSystem {
// Menu items and their respective prices
private static String[] flavors = {"Classic", "Taro", "Matcha"};
private static double[] prices = {3.50, 4.00, 4.50};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean orderAgain = true;
while (orderAgain) {
System.out.println("Welcome to MilkTea Shop!");
System.out.println("Menu:");
displayMenu();
System.out.print("Enter the number of your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
System.out.println("You selected " + flavors[choice - 1] + " milktea.");
System.out.println("What size would you like? (S/M/L)");
String size = scanner.next().toUpperCase();
double price = calculatePrice(choice, size);
System.out.println("Your total is: $" + price);
break;
case 0:
orderAgain = false;
System.out.println("Thank you for visiting! Goodbye.");
break;
default:
System.out.println("Invalid choice. Please enter a number from the menu.");
}
}
scanner.close();
}
// Method to display the menu
private static void displayMenu() {
3
for (int i = 0; i < flavors.length; i++) {
System.out.println((i + 1) + ". " + flavors[i] + " - $" + prices[i]);
}
System.out.println("0. Exit");
}
// Method to calculate the total price
private static double calculatePrice(int choice, String size) {
double price = 0;
switch (size) {
case "S":
price = prices[choice - 1];
break;
case "M":
price = prices[choice - 1] + 1.00;
break;
case "L":
price = prices[choice - 1] + 2.00;
break;
default:
System.out.println("Invalid size. Assuming small size.");
price = prices[choice - 1];
}
return price;
}
}
ENROLLMENT SYSTEM FOR SHS STUDENT
import java.util.ArrayList;
import java.util.Scanner;
public class EnrollmentSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
boolean continueEnrollment = true;
while (continueEnrollment) {
System.out.println("Senior High School Enrollment System");
System.out.println("1. Enroll Student");
System.out.println("2. View Enrolled Students");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
4
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
enrollStudent(scanner, students);
break;
case 2:
viewEnrolledStudents(students);
break;
case 0:
continueEnrollment = false;
System.out.println("Exiting the program. Goodbye!");
break;
default:
System.out.println("Invalid choice. Please enter a valid option.");
}
}
scanner.close();
}
private static void enrollStudent(Scanner scanner, ArrayList<Student> students) {
System.out.println("Enter student details:");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Grade Level: ");
int gradeLevel = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Section: ");
String section = scanner.nextLine();
Student student = new Student(name, gradeLevel, section);
students.add(student);
System.out.println("Student enrolled successfully!");
}
private static void viewEnrolledStudents(ArrayList<Student> students) {
System.out.println("Enrolled Students:");
if (students.isEmpty()) {
System.out.println("No students enrolled yet.");
} else {
for (int i = 0; i < students.size(); i++) {
System.out.println((i + 1) + ". " + students.get(i));
}
}
}
}
5
class Student {
private String name;
private int gradeLevel;
private String section;
public Student(String name, int gradeLevel, String section) {
this.name = name;
this.gradeLevel = gradeLevel;
this.section = section;
}
@Override
public String toString() {
return "Name: " + name + ", Grade Level: " + gradeLevel + ", Section: " + section;
}
}
FILIPINO DISHES IN ARRAY STATEMENT
public class FilipinoDishesIngredients {
public static void main(String[] args) {
String[] adoboIngredients = {"Chicken/Pork", "Soy Sauce", "Vinegar", "Garlic", "Bay Leaves",
"Peppercorns"};
String[] sinigangIngredients = {"Pork Ribs/Prawns", "Tamarind", "Tomatoes", "String Beans",
"Spinach", "Radish"};
String[] kareKareIngredients = {"Oxtail", "Tripe", "Peanut Butter", "Eggplant", "String Beans",
"Banana Heart"};
String[] lechonKawaliIngredients = {"Pork Belly", "Salt", "Pepper", "Garlic", "Oil"};
String[] pancitCantonIngredients = {"Canton Noodles", "Chicken/Pork/Shrimp", "Carrots",
"Cabbage", "Bell Pepper", "Soy Sauce"};
String[][] dishesIngredients = {
adoboIngredients,
sinigangIngredients,
kareKareIngredients,
lechonKawaliIngredients,
pancitCantonIngredients
};
String[] dishNames = {"Adobo", "Sinigang", "Kare-Kare", "Lechon Kawali", "Pancit Canton"};
for (int i = 0; i < dishNames.length; i++) {
System.out.println("Dish: " + dishNames[i]);
System.out.println("Ingredients:");
for (String ingredient : dishesIngredients[i]) {
6
System.out.println("- " + ingredient);
}
System.out.println();
}
}
}