Lab Activity 2.1-1 A Letter Guessing Game. BufferedReader Lab Activity 2.1-2 Application of Event Controlled Loop.
Scanner
import java.io.*; import java.util.Scanner;
public class Balance {
public class GuessingGame { public static void main(String[] args) {
public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in);
char LETTER = 'M', guess = ' '; System.out.print("Enter Amount Borrowed: ");
String playerGuess; double balance = put.nextDouble();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int paymentNumber = 1;
System.out.println("I am thinking of a CAPITAL Letter"); while (balance > 0) {
System.out.print("What is your guess? "); System.out.print("Payment # " + paymentNumber + ": ");
playerGuess = reader.readLine(); double payment = scan.nextDouble();
guess = playerGuess.charAt(0);
if (payment > balance) {
while (guess != LETTER) { System.out.println("Payment exceeds balance. Adjusting to remaining balance.");
System.out.println("Your guess is incorrect."); payment = balance;
System.out.print("What is your guess? "); }
playerGuess = reader.readLine(); balance -= payment;
guess = playerGuess.charAt(0); System.out.println("Current Balance: " + balance);
} paymentNumber++;
}
System.out.println("Congratulations, you guessed my letter."); System.out.println("YOU ARE PAID!");
} }
} }
Lab Activity 2.1-2 Application of Event Controlled Loop. BufferedReader Lab Activity 2.2-1 (A Program that will enter a username and password). BufferedReader
import java.io.*;
import java.io.*;
public class Password {
public class Balance { public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException { String username, password;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); boolean login = false;
System.out.print("Enter Amount Borrowed: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
double balance = Double.parseDouble(read.readLine()); do {
int paymentNumber = 1; System.out.print("Please enter username: ");
while (balance > 0) { username = reader.readLine();
System.out.print("Payment # " + paymentNumber + ": "); System.out.print("Please enter password: ");
double payment = Double.parseDouble(read.readLine()); password = reader.readLine();
if (username.equals("Admin") && password.equals("admin")) {
if (payment > balance) { login = true;
System.out.println("Payment exceeds balance. Adjusting to remaining balance."); System.out.println("Login successful! Welcome, " + username + ".");
payment = balance; } else if (username.equals("Admin")) {
} login = false;
balance -= payment; System.out.println("Incorrect password. Please try again.");
System.out.println("Current Balance: " + balance); } else {
paymentNumber++; System.out.println("Incorrect username or password. Please try again.");
} }
System.out.println("YOU ARE PAID!"); } while (!login);
} }
} }
Lab Activity 2.2-2 Loop Application with Selection Structures .( TV Channel Survey Program). Scanner Lab Activity 2.2-2 Loop Application with Selection Structures .( TV Channel Survey Program). BufferedReader
import java.util.Scanner; import java.io.*;
public class Channel_survey { public class ChannelSurvey {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in); BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int absCbn = 0, ptv = 0, tv5 = 0, gma = 0; int absCbn = 0, ptv = 0, tv5 = 0, gma = 0;
System.out.print("Enter the number of respondents: "); System.out.print("Enter the number of respondents: ");
int respondents = scan.nextInt(); int respondents = Integer.parseInt(read.readLine());
System.out.println("Enter the votes (2 for ABS-CBN, 4 for PTV, 5 for TV5, 7 for GMA):"); System.out.println("Enter the votes (2 for ABS-CBN, 4 for PTV, 5 for TV5, 7 for GMA):");
for (int i = 0; i < respondents; i++) { for (int i = 0; i < respondents; i++) {
int vote = scan.nextInt(); int vote = Integer.parseInt(read.readLine());
switch (vote) { switch (vote) {
case 2: case 2:
absCbn++; absCbn++;
break; break;
case 4: case 4:
ptv++; ptv++;
break; break;
case 5: case 5:
tv5++; tv5++;
break; break;
case 7: case 7:
gma++; gma++;
break; break;
default: default:
System.out.println("Invalid vote! Please enter 2, 4, 5, or 7."); System.out.println("Invalid vote! Please enter 2, 4, 5, or 7.");
i--; i--;
break; break;
} }
} }
System.out.println("Survey Results:"); System.out.println("Survey Results:");
System.out.println("ABS-CBN 2: " + absCbn + " votes"); System.out.println("ABS-CBN 2: " + absCbn + " votes");
System.out.println("PTV 4: " + ptv + " votes"); System.out.println("PTV 4: " + ptv + " votes");
System.out.println("TV5: " + tv5 + " votes"); System.out.println("TV5: " + tv5 + " votes");
System.out.println("GMA 7: " + gma + " votes"); System.out.println("GMA 7: " + gma + " votes");
} }
} }
Lab Activity 2.3 Factorial of A Number. BufferedReader Lab Activity 5.1 Probability of Winning a Lottery. Scanner
import java.io.*; import java.util.Scanner;
public class Factorial { public class Lottery {
public static void main(String[] args) throws IOException { public static void main(String[] args) {
int ctr, num, factorial = 1; Scanner scan = new Scanner(System.in);
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the number of balls in the bin (n): ");
System.out.print("Please enter a number: "); int n = scan.nextInt();
num = Integer.parseInt(read.readLine()); System.out.print("Enter the number of balls selected (r): ");
int r = scan.nextInt();
for (ctr = 1; ctr <= num; ctr++) {
System.out.print(ctr); long result = 1;
if (num > 1 && ctr < num) for (int i = 0; i < r; i++) {
System.out.print("*"); result *= (n - i);
factorial = factorial * ctr; }
} for (int i = 1; i <= r; i++) {
System.out.println("\n" + num + "!=" + factorial); result /= i;
} }
} System.out.println("The odds of winning the lottery: " + result);
}
}
Lab Activity 5.1 Probability of Winning a Lottery. BufferedReader
import java.io.*;
public class Lottery {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of balls in the bin (n): ");
int n = Integer.parseInt(read.readLine());
System.out.print("Enter the number of balls selected (r): ");
int r = Integer.parseInt(read.readLine());
long result = 1;
for (int i = 0; i < r; i++) {
result *= (n - i);
}
for (int i = 1; i <= r; i++) {
result /= i;
}
System.out.println("The odds of winning the lottery: " + result);
}
}